0
\$\begingroup\$

So I am working with Gizduino (Arduino clone), keypad and led. What am I doing is getting the value of keypressed and then light up the led according to the value (eg., when I pressed 2 on the keypad 2 LEDs will blink). I know this is just basic but please bear with me. I have this code (I will not include some really basic things like keymapping or something that is not related to my question).

int led1 = A0;
int led2 = A1;

void setup()
{


pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);


}

void loop()
{
   char keypressed = myKeypad.getKey();

   if(keypressed != NO_KEY)
   {

       if(keypressed > 47 && keypressed < 58)  // is between '0' and '9'
       {
           if(!mySwitch)
           {
               num1 = (num1 * 10) + (keypressed - 48);
           }
           else
           {
               num2 = (num2 * 10) + (keypressed - 48);
           }

           if(keypressed = '1')
           {
               digitalWrite(led1, HIGH);
               delay(500);
               digitalWrite(led1,LOW);
               delay(500);
           }
           if(keypressed = '2')
           {

               digitalWrite(led2, HIGH);
                delay(500);
               digitalWrite(led2,LOW);
               delay(500);

        }
    }
}

What's happening is that when I press '1' in the keypad the first led will blink and after that the second one will too. Why is this happening? Is there a problem in my code?

\$\endgroup\$
2
\$\begingroup\$

The equality testing operator is ==, the assignment operator is =.

You are using = instead of == so the if condition returns always true.

This line if(keypressed == '1') assigns character 1 to the keypressed variable, since keypressed becomes a non zero value the if condition is always true.

The code should be like:

if(keypressed == '1')
{
    digitalWrite(led1, HIGH);
    delay(500);
    digitalWrite(led1, LOW);
    delay(500);
}

if(keypressed == '2')
{

    digitalWrite(led2, HIGH);
    delay(500);
    digitalWrite(led2, LOW);
    delay(500);

}
\$\endgroup\$
  • \$\begingroup\$ @NewInEverything It's a very common mistake, and the more complex the code the harder to find. Usually the compiler gives a warning is cases where a = is used instead of == so it's a good idea to keep an eye on the compiler report. \$\endgroup\$ – alexan_e Mar 12 '14 at 17:35
  • \$\begingroup\$ The compiler dint give a warning though. \$\endgroup\$ – NewInEverything Mar 12 '14 at 17:37
  • \$\begingroup\$ @NewInEverything Indeed it doesn't, I just did a test in Arduino 1.5.5. I mainly write my code in AVRstudio and there I get a warning as expected warning: suggest parentheses around assignment used as truth value. \$\endgroup\$ – alexan_e Mar 12 '14 at 17:47
  • \$\begingroup\$ @NewInEverything According to stackoverflow.com/q/7803443/3080173 the warnings seem to be disabled (although it refers to an older version so maybe things are different). \$\endgroup\$ – alexan_e Mar 12 '14 at 17:51
  • \$\begingroup\$ Sorry for asking, but what will I do to a code should i copy it and paste it to one of the text file in the arduino folder, if it is what file should i replace? \$\endgroup\$ – NewInEverything Mar 12 '14 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.