Sorry to bother you but I have been having issues getting my a motor array to output in this code here.
// initialise motors
// Motor 1
//int in1 = 4;
//int in2 = 2 ;
// Motor 2
//int in3 = 8;
//int in4 = 7;
// Motor 3
//int in5 = 12;
//int in6 = 13;
// Motor 4
//int in7 = 9;
//int in8 = 10;
// Array of PWM's
int thisPin = 0;
uint8_t pwms[] = {3,5,6,11};
byte numberPwms = 4;
// Array for output channels
// Motor 1 (4 = High, 2 = Low)
// Motor 2 (8 = High, 7 = Low)
// Motor 3 (12 = High, 13 = Low)
// Motor 4 (9 = High, 10 = Low)
int mtrpin = 0;
uint8_t motors[] = {4,2,8,7,12,13,9,10};
byte motorPin = 8;
void setup()
{
// pinMode(in1, OUTPUT);
// pinMode(in2, OUTPUT);
// pinMode(in3, OUTPUT);
// pinMode(in4, OUTPUT);
// pinMode(in5, OUTPUT);
// pinMode(in6, OUTPUT);
// pinMode(in7, OUTPUT);
// pinMode(in8, OUTPUT);
for (pwmPin = 0; pwmPin < numberPwms; pwmPin++){
for (mtrPin = 0; mtrpin < motorPin; mtrPin++){
pinMode(motors[mtrPin], OUTPUT);
pinMode(pwms[pwmPin], OUTPUT);
};
};
}
void motorLoop(){
// setting the direction to turn and speed
// digitalWrite(in1, HIGH); // Motor 2
// digitalWrite(in2, LOW);
// digitalWrite(in3, HIGH); // Motor 2
// digitalWrite(in4, LOW);
// digitalWrite(in5, HIGH); // Motor 3
// digitalWrite(in6, LOW);
// digitalWrite(in7, HIGH); // Motor 4
// digitalWrite(in8, LOW);
// Set the speed for the Motors
for(int pwmPin = 0; pwmPin < numberPwms; pwmPin++){
for (mtrPin = 0; mtrpin < motorPin; mtrPin++){
pinMode(motors[mtrPin], LOW);
analogWrite(pwms[pwmPin], 200);
};
};
};
void loop()
{
motorLoop();
delay(500);
}
I have managed to get the initial call to work but I am now turning it all into arrays.
I have managed to get it to work for the PWMs but now after adding the motors array it has stopped working and gives this error message.
In Function 'Void setup()';
41:8: error: 'pwmPin' was not declared in this scope
42:11: error: 'mtrPin' was not declared in this scope
In function 'void motorLoop()';
74:8: error: 'mtrPin' was not declare in this scope.
I know the error will probably be really simple but I can't find to save my life.
Also the for loops have been called as they are now and separate and I am still getting the same errors.
You can see this code at https://www.tinkercad.com/things/fFQKRTjhDrb-motor-initial-turn/editel.
Any help on this would be greatly appreciated :)
In Function 'Void setup()'; 41:8: error: 'pwmPin' was not declared in this scope 42:11: error: 'mtrPin' was not declared in this scope In function 'void motorLoop()'; 74:8: error: 'mtrPin' was not declare in this scope.
As the compiler says: mtrPin has not been declared, and yet, you are using it.
Declarations introduce names in a program, like the name of your variable mtrPin. Other examples are namespaces, functions and classes. A name must be declared before it can be used. You cannot refer to a name which is declared later in the code.
int mtrPin; // this is a (uninitialized) declaration -> mtrPin can be used
int mtrPin { 0 }; // this is a (value) initialization -> mtrPin can be used
Related
We have a project that were running using C and a PICkit 3. Our issu is that we remain getting the same error message and arent sure whta the issue is. Any help / advice would be great.
void main (void)
{
ANSEL = 0; //turn off all other analog inputs
ANSELH = 0;
ANSELbits.ANS0 = 1; // turn on RA0 analog
ADCON0 = 1; // justify right
ADC_Init(); // Call ADC_Init function
ADC_Convert();
Delay1TCYx(1); //delay 1 x 1 cycles
int reading0 = ADRESL + (ADRESH * 256); //convert result to 10 bits
if (reading0 = 0b1000000011)
{
readingsamples();
}
while(1);
}
I have a question about arduino considering a setup with 8 leds and an potentiometer. I want to let 1 led light up, which is the led that matches the value returned by the potentiometer, and the rest of the leds should be turned off. Furthermore, when I change the position of the potentiometer, the leds should change accordingly. So far ive got this:
for(int i = 0; i)
{
if (i = draaiKnopStand)
{
status[i] = HIGH;
}
else
{
status[i] = LOW;
}
digitalWrite(draaiKnopStand, status[i]);
}
I'm considering this is Arduino Uno Front
I have no Arduino here in my job. I did this using a simulator. Please try it:
int _potentiometer = 9; // Potentiometer - Analog Pin
int _val = 0;
int _borderLineVal = 0;
int ledPins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers to which LEDs are attached
int pinCount = 6; // the number of pins (i.e. the length of the array)
void setup() {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
_val = analogRead(_potentiometer); //reading the Potentiometer value interval: 0 - 1023
_borderLineVal = (int)(1023 / pinCount);
Serial.println(_val);
// turn all leds off
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], LOW);
}
// turn the select led on
if(_val > 0){
_pinHigh = (int)(1023 / _borderLineVal);
digitalWrite(ledPins[_pinHigh], HIGH); // turn the pin on
}
}
Hello Stackoverflow users!
It's the line trafiksignal(redLed && yellowLed, 1000); I can't get to work, I'm trying to create a trafic signal using a function, and my idea was to use this picture as a guide: http://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Traffic_lights_4_states.png/220px-Traffic_lights_4_states.png
(can't post pictures due to rep < 10)
My code: http://pastebin.com/MTGsYeXs
/*
* #Author: Kristian Nymann
* #Date: 2014-09-25 22:46:39
* #Last Modified by: Kristian Nymann
* #Last Modified time: 2014-09-25 23:23:19
* #Description: Lav et program der får de tre lysdioder til at skifte som et trafiklys. Du kan brugeprogrammet "Blink" som eksempel.
*/
const byte greenLed = 2;
const byte yellowLed = 3;
const byte redLed = 4;
void setup() {
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
}
void loop() {
trafiksignal(redLed, 3000);
trafiksignal(redLed && yellowLed, 1000);
trafiksignal(greenLed, 3000);
trafiksignal(yellowLed, 1000);
}
void trafiksignal(byte pin, unsigned int duration)
{
digitalWrite(pin, HIGH);
delay(duration);
digitalWrite(pin, LOW);
}
Right now what's going on is:
Red led turns on for 3 sec.. then green led turns on for 3 sec, then yellow turns on for 1 sec..
So how can I make the yellow and red Led turn on at the same time? (why doesn't trafiksignal(redLed && yellowLed, 1000); work?)
As far as I am aware && in C is for logical comparison only.
trafiksignal(redLed && yellowLed, 1000);
What this line is basically doing is checking if redLed and yellowLed are true or false, and if they're both true it will send a 1 or if either are false it will send a 0. Since both of them are not 0 it should treat them as both true so you're actually most likely telling pin 1 to go high for 1 second instead of pin 3 and 4.
A few simple work around would be to either create a separate function for turning the red and yellow light off or putting a quick if clause inside trafiksignal functio. Bellow is a quick a dirty if statement to add to trafiksignal that should get you up and running with your current code.
if (pin == 1) {
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
delay(duration)
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
else {
digitalWrite(pin, HIGH);
delay(duration);
digitalWrite(pin, LOW);
}
Typed the code from my ipad so I would proof read it before copy and pasting.
edited from my computer:
A better solution would be to send the pins to the function as an array, i'm not super experienced in plain C so I'm not sure if what i've done bellow is 100% accurate but it should be fairly close.
const byte greenLed = 2;
const byte yellowLed = 3;
const byte redLed = 4;
const byte redYellow[2] = {redLed, yellowLed} // create an array of 2 pins
void setup() {
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
}
void loop() {
// the middle number is the number of LEDs the function will need to control.
trafiksignal(redLed, 1, 3000);
trafiksignal(redYellow, 2, 1000);
trafiksignal(greenLed, 1, 3000);
trafiksignal(yellowLed, 1, 1000);
}
void trafiksignal(byte pin[], unsigned int numberOfLeds, unsigned int duration)
{
for (int i =0; i < numberOfLeds; ++i {
digitalWrite(pin[i], HIGH);
}
delay(duration);
for (int i = 0; i < numberOfLeds; ++i) {
digitalWrite(pin[i], LOW);
}
}
I am measuring light, temperature and humidity on a arduino uno, and have programmed the loop to run every minute. Monitoring the values on the serial monitor.
However, what i would like is the code to run once to get the values, then wait, or pause, until one of the values change, and then output this on the serial monitor.
I want to get informed of changes in the sensors immediately, not wait for a minute for the loop to run. Is there a way to do this?
Thanks.
So I would need to add an if/else function to the following code?
int lightPin = A5;
int lightok = 9;
int lighthigh = 10;
void setup()
{
Serial.begin(9600);
pinMode(lightok,OUTPUT);
pinMode(lighthigh,OUTPUT);
}
void loop()
{
delay(600000);
int lightlevel = analogRead(lightPin);
lightlevel = map(lightlevel, 0, 1023, 0, 255);
lightlevel = constrain(lightlevel, 0, 255);
Serial.print("Lightlevel: ");
Serial.println(lightlevel);
//led control for light levels
if (lightlevel < 15 || lightlevel > 125) {
digitalWrite(lighthigh, HIGH);
digitalWrite(lightok, LOW);
} else {
digitalWrite(lighthigh, LOW);
digitalWrite(lightok, HIGH);
}
}
You have to ways of doing this:
you can run your loop faster, say every second or half-second and only output a value to serial if the value is different from the previous one or if the difference is bigger than a specific value
you can setup interrupts run code only when something happens. But I need to know the sensor you are using to be more specific on this one.
Hope this helps! :)
EDIT
Okay, I'd do something like that :
int lightSensorPin = A5;
int lightOkPin = 9;
int lightHighPin = 10;
int currentLightLevel = 0;
int previousLightLevel = 0;
int delta = 0;
int deltaValue = 10; // needs to be changed to suit your needs
void setup() {
Serial.begin(9600);
pinMode(lightOkPin, OUTPUT);
pinMode(lightHighPin, OUTPUT);
}
void loop() {
currentLightLevel = analogRead(lightSensorPin); //read the sensor
currentLightLevel = map(currentLightLevel, 0, 1023, 0, 255); // map the value
currentLightLevel = constrain(currentLightLevel, 0, 255); // not sure this is useful
delta = abs(previousLightLevel - currentLightLevel); // calculate the absolute value of the difference btw privous and current light value
if (delta >= deltaValue) { // if the difference is higher than a threshold
Serial.print("currentLightLevel: ");
Serial.println(currentLightLevel);
//led control for light levels
if (currentLightLevel < 15 || currentLightLevel > 125) {
digitalWrite(lightHighPin, HIGH);
digitalWrite(lightOkPin, LOW);
}
else {
digitalWrite(lightHighPin, LOW);
digitalWrite(lightOkPin, HIGH);
}
}
previousLightLevel = currentLightLevel;
delay(1000);
}
I'm programming Arduino. I have a problem with the IR sensor. It only detects this ball (equiped with IR LEDS) in range of 0,5m and I would need at least 2m. This is the ball:
"http://drgraeme.net/drgraeme-free-nxt-g-tutorials/Ch108/SoccerGenIINXTG/Soccer%20Ball/HiTechnicRCJ05V2.jpg"
and this is my arduino code:
// digital pin 2 has a pushbutton attached to it. Give it a name:
int IR = 2;
int i = 0;
int ii = 0;
int led = 0;
void setup()
{
Serial.begin(9600);
pinMode(IR, INPUT_PULLUP);
pinMode(led, OUTPUT);
}
void loop() {
i = 0;
ii = 0;
do
{
i = i + 1;
int STANJE1 = digitalRead(IR);
if(STANJE1 < 1)
{
ii = ii + 1;
}
}while(i<1000);
if(ii > 1)
{Serial.println("IS");}
else{Serial.println("IS NOT");}
}
IR sensor is 38kHZ and the balls LEDs are 40kHZ, but I found a program that allowed me to detect the ball 10m away from sensor, but It was ment for something else, and I didn't understand it, so that is not the problem.
This is the code that works further (That's because it doesn't use "DigitalRead()")
/* Raw IR decoder sketch!
This sketch/program uses the Arduno and a PNA4602 to
decode IR received. This can be used to make a IR receiver
(by looking for a particular code)
or transmitter (by pulsing an IR LED at ~38KHz for the
durations detected
Code is public domain, check out www.ladyada.net and adafruit.com
for more tutorials!
*/
// We need to use the 'raw' pin reading methods
// because timing is very important here and the digitalRead()
// procedure is slower!
//uint8_t IRpin = 2;
// Digital pin #2 is the same as Pin D2 see
// http://arduino.cc/en/Hacking/PinMapping168 for the 'raw' pin mapping
#define IRpin_PIN PIND
#define IRpin 2
// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 65000
// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
#define RESOLUTION 20
// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2]; // pair is high and low pulse
uint8_t currentpulse = 0; // index for pulses we're storing
void setup(void) {
Serial.begin(9600);
Serial.println("Ready to decode IR!");
}
void loop(void) {
uint16_t highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
// while (digitalRead(IRpin)) { // this is too slow!
while (IRpin_PIN & (1 << IRpin)) {
// pin is still HIGH
// count off another few microseconds
highpulse++;
delayMicroseconds(RESOLUTION);
// If the pulse is too long, we 'timed out' - either nothing
// was received or the code is finished, so print what
// we've grabbed so far, and then reset
if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
printpulses();
currentpulse=0;
return;
}
}
// we didn't time out so lets stash the reading
pulses[currentpulse][0] = highpulse;
// same as above
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
printpulses();
currentpulse=0;
return;
}
}
pulses[currentpulse][1] = lowpulse;
// we read one high-low pulse successfully, continue!
currentpulse++;
}
void printpulses(void) {
Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
for (uint8_t i = 0; i < currentpulse; i++) {
Serial.print(pulses[i][0] * RESOLUTION, DEC);
Serial.print(" usec, ");
Serial.print(pulses[i][1] * RESOLUTION, DEC);
Serial.println(" usec");
}
}