Arduino working unpredictably

Thread Starter

Aerb

Joined Jul 9, 2014
12
Hi all,

Thanks to AAC, I decided to buy an Arduino, Mega2560. I've been playing with it couple of days now and trying to learn basics. Now that I started to write my own codes, rather than uploading examples and observe, I came across a situation, where I have no idea why the Arduino acts this way.
On the breadboard, I have 3 LEDs, 3 current limiting resistances (1k each) and a potentiometer. I adjust the brigthness of the yellow RED according to the analog value being read from pot. Using an 'if' statement, I want green LED to be lit when the analog value from the pot is less than '127'. And when the value is above '127', I want the red LED to be lit.

int potValue=0;
int outValue=0;
int highValue=0;
int lowValue=0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
potValue=analogRead(A0);
outValue=map(potValue, 0, 1023, 0, 255);
analogWrite(11,outValue);
if(outValue<127){
analogWrite(10,0);
analogWrite(12,255);
lowValue=analogRead(12);
delay(100);
}
else
analogWrite(12,0);
analogWrite(10,255);
highValue=analogRead(10);

delay(100);
Serial.print(outValue);
Serial.print("\t");
Serial.print(highValue);
Serial.print("\t");
Serial.println(lowValue);
}

When I upload this code, the yellow LED lights according to the analog value from the pot. When the analog value is above '127', red LED lights as well, however, when the analog value is below '127', green LED lights and red LED blinks continuously. And this is where my confusion begins.

Any help?

Edit: All sorted out guys, thanks anyways.
 
Last edited:

WBahn

Joined Mar 31, 2012
30,060
Hi all,

Thanks to AAC, I decided to buy an Arduino, Mega2560. I've been playing with it couple of days now and trying to learn basics. Now that I started to write my own codes, rather than uploading examples and observe, I came across a situation, where I have no idea why the Arduino acts this way.
On the breadboard, I have 3 LEDs, 3 current limiting resistances (1k each) and a potentiometer. I adjust the brigthness of the yellow RED according to the analog value being read from pot. Using an 'if' statement, I want green LED to be lit when the analog value from the pot is less than '127'. And when the value is above '127', I want the red LED to be lit.
This is a classic case of expecting readers to have crystal balls with which to read your mind. How are we supposed to know which code controls which color LED? How are we supposed to know whether a 0 turns the LED on or whether 255 does?

The code is also horribly formatted and not a single comment.

Just formatting the code and providing minimal comments goes a long way to finding the problem:

Code:
int potValue=0;
int outValue=0;
int highValue=0;
int lowValue=0;

void setup()
{
   Serial.begin(9600);
}

// NOTE: I'm having to guess at the following
// Channel 10 - Red LED
// Channel 11 - Yellow LED
// Channel 12 - Green LED
// Output 0 to turn off and 255 to turn on

void loop()
{
   potValue=analogRead(A0);
   outValue=map(potValue, 0, 1023, 0, 255);
   analogWrite(11,outValue);     // Yellow tracks potValue
   if(outValue<127)
   {
      analogWrite(10,0);         // Turn off Red LED
      analogWrite(12,255);       // Turn on Green LED
      lowValue=analogRead(12);
      delay(100);
   }
   else
      analogWrite(12,0);         // Turn off Green LED

   analogWrite(10,255);          // Turn on Red LED
   highValue=analogRead(10);

   delay(100);
   Serial.print(outValue);
   Serial.print("\t");
   Serial.print(highValue);
   Serial.print("\t");
   Serial.println(lowValue);
}
When I upload this code, the yellow LED lights according to the analog value from the pot. When the analog value is above '127', red LED lights as well, however, when the analog value is below '127', green LED lights and red LED blinks continuously. And this is where my confusion begins.
Notice how your problem now jumps out at you and that it is a simple matter of not enclosing in curly braces the statements you wanted to include in the 'else' block.

Also note that adding simple comments make it so that not only is the logic of your code readily apparent, but you aren't relying on the reader to understand what the Arduino-specific functions do. I don't need to know what analogWrite() does or how it does it, because the comment tells me what purpose it accomplishes in that particular line.
 
Top