one quick question

Hi everybody,

My first day with this and I have no prior experience in programming unfortunately. Have tried a couple of examples and they work nicely!

But I tried one project for myself and I would be happy if anyone could help me (or laugh at me) :slight_smile:

I use two buttons and two LEDs. Button one should blink both LEDs rapidly and button two should blink LED2 slowly. And that works for me, no problem. But what happens when both buttons are pressed? I like to control that så that only LED two should blink slowly. So I have issues with the "AND" command.

Any other comments about my code are very welcome!

Best regards
Wretan

const int buttonPin_1 = 1;  	//button1
const int buttonPin_2 = 2;  	//button2

const int ledPin1 =  3;      	// LED1
const int ledPin2 =  5; 	// LED2

int buttonState = 0;
int buttonState1 = 0;

void setup() 

{
  pinMode(ledPin1, OUTPUT);      
  pinMode(ledPin2, OUTPUT);

  pinMode(buttonPin_1, INPUT); 
  pinMode(buttonPin_2, INPUT);
}

void loop()

{
    buttonState = digitalRead(buttonPin_1);
    buttonState1 = digitalRead(buttonPin_2);

//When button 1 is pressed, LED1 and LED2 should blink rapidly;
  if (buttonState == HIGH)        
    {digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
  delay (250);
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
  delay (250);  
}
  else {
    //turn both LEDs off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }
  
    
//When button 2 is pressed, LED2 should blink;
    if (buttonState1 == HIGH)        
    {digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
  delay (500);
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
  delay (500);  
}
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
}


//When button 1 AND button 2 is pressed at the same time, LED2 still should blink -BUT I DONT GET THE AND TO WORK, HAVE TRIED & and &&;
    if (buttonState == HIGH) AND (buttonState1 == HIGH);      
    {digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
  delay (500);
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
  delay (500);  
}
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
}

}
    if (buttonState == HIGH) AND (buttonState1 == HIGH);

Does that ; belong there?

NOTHING goes after the {. Ever. (Unless you are initializing a struct or array.)

Learn to use Tools + Auto Format, if you can't use the tab key properly.

  if (buttonState == HIGH) AND (buttonState1 == HIGH);
sketch_may20d.ino: In function ‚Äėvoid loop()‚Äô:
sketch_may20d:63: error: ‚ÄėAND‚Äô was not declared in this scope
sketch_may20d:72: error: ‚Äėelse‚Äô without a previous ‚Äėif‚Äô

So I have issues with the "AND" command.

I gather you did not post the code you actually used. Grrr.

Hi,

Nick, Grr and thank you for your warm welcome!

except for the ; that¬īs exactly the code that I used.

Thank you PaulS for the tip with auto format!

Still got the same error as Nick mentioned above.

Removed the ; but still no progress.

Am I blind or what have I missed?

//Wretan

There are several hundred mature programming languages. Each one has its own syntax and set of key words. You can't just guess what they are. In this case, you can access the language reference from the help tab in the IDE. I'm not necessarily laughing at you, but it seems odd to me that you're trying to program without looking up the rules.

const int buttonPin_1 = 1; //button1
D1 is the TX pin for the Arduino

&&
See:

const byte buttonPin_1 = 3;   //button1  byte is enough for button pins... use pin 3 for example  (check one of your replies)
const byte buttonPin_2 = 2;   //button2

const byte ledPin1 =  3;       // LED1
const byte ledPin2 =  5; // LED2

byte buttonState = 0;   //  byte is enough for button states
byte buttonState1 = 0;

void setup()
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  pinMode(buttonPin_1, INPUT); // if you use INPUT_PULLUP(input=HIGH) here, you won't need to pull down anything with your resistors(just change your code below accordingly)
  pinMode(buttonPin_2, INPUT); // same here
}

void loop()
{
  buttonState = digitalRead(buttonPin_1);
  buttonState1 = digitalRead(buttonPin_2);

  //When button 1 is pressed, LED1 and LED2 should blink rapidly;
  if (buttonState == HIGH)
  { digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    delay (250);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    delay (250);
  }
  else {
    //turn both LEDs off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }


  //When button 2 is pressed, LED2 should blink;
  if (buttonState1 == HIGH)
  { digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    delay (500);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    delay (500);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }


  //When button 1 AND button 2 is pressed at the same time, LED2 still should blink -BUT I DONT GET THE AND TO WORK, HAVE TRIED & and &&;
  if (buttonState == HIGH && buttonState1 == HIGH)  // use && and remove ;
  { digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    delay (500);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    delay (500);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }

}
// for your reference
//http://www.arduino.cc/en/Reference/HomePage
// http://www.arduino.cc/en/Reference/Boolean

Also:

if (buttonState1 == HIGH)  
    digitalWrite(ledPin1, LOW);   
    digitalWrite(ledPin2, HIGH);    //  <==================AAA
    delay (500);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    delay (500);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }


  //When button 1 AND button 2 is pressed at the same time, LED2 still should blink -BUT I DONT GET THE AND TO WORK, HAVE TRIED & and &&;
  if (buttonState == HIGH && buttonState1 == HIGH)  // use && and remove ;
  { digitalWrite(ledPin1, LOW);   // <================BBB
    digitalWrite(ledPin2, HIGH);
    delay (500);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    delay (500);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }

}

AAA = BBB Why?

Fix your HIGH and LOW then.

Good luck.

aarg:
There are several hundred mature programming languages. Each one has its own syntax and set of key words. You can’t just guess what they are. In this case, you can access the language reference from the help tab in the IDE. I’m not necessarily laughing at you, but it seems odd to me that you’re trying to program without looking up the rules.

I agree.

Get a book or find an online tutorial and learn the basic C/C++ language syntax. It will save your time and ours, there is no point trying random nonsense and expecting to ‚Äúget lucky‚ÄĚ.

I blame modern technology.

30 years ago, if you bought something like a VCR, you were expected to read the manual.

Now devices come with interfaces that are supposedly ‚Äúintuitive‚ÄĚ, no manual required. Just rub your greasy fingers all over the phone until you ‚Äúfigure it out‚ÄĚ.

That doesn’t really work with programming.

Hi and thank you for your attempts to guide me. Special thanx to siutoejai, your solution really helped me.

I will try to learn this. I programmed some script with VBA earlier and there I used "AND".

sorry about the n00bie questions. Got to start somewhere.

Regards
Wretan