Arduino working unpredictably

Discussion in 'Embedded Systems and Microcontrollers' started by Aerb, Jun 20, 2015.

  1. Aerb

    Thread Starter New Member

    Jul 9, 2014
    12
    4
    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: Jun 20, 2015
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    I think you need to read about pinMode, and put it in the setup() func....

    And a scematic would be nice.
     
  3. WBahn

    Moderator

    Mar 31, 2012
    17,747
    4,796
    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 (Text):
    1.  
    2.  
    3. int potValue=0;
    4. int outValue=0;
    5. int highValue=0;
    6. int lowValue=0;
    7.  
    8. void setup()
    9. {
    10.    Serial.begin(9600);
    11. }
    12.  
    13. // NOTE: I'm having to guess at the following
    14. // Channel 10 - Red LED
    15. // Channel 11 - Yellow LED
    16. // Channel 12 - Green LED
    17. // Output 0 to turn off and 255 to turn on
    18.  
    19. void loop()
    20. {
    21.    potValue=analogRead(A0);
    22.    outValue=map(potValue, 0, 1023, 0, 255);
    23.    analogWrite(11,outValue);     // Yellow tracks potValue
    24.    if(outValue<127)
    25.    {
    26.       analogWrite(10,0);         // Turn off Red LED
    27.       analogWrite(12,255);       // Turn on Green LED
    28.       lowValue=analogRead(12);
    29.       delay(100);
    30.    }
    31.    else
    32.       analogWrite(12,0);         // Turn off Green LED
    33.  
    34.    analogWrite(10,255);          // Turn on Red LED
    35.    highValue=analogRead(10);
    36.  
    37.    delay(100);
    38.    Serial.print(outValue);
    39.    Serial.print("\t");
    40.    Serial.print(highValue);
    41.    Serial.print("\t");
    42.    Serial.println(lowValue);
    43. }
    44.  
    45.  
    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.
     
Loading...