Hello, i'm new learning about arduino. I made a simple project, where in this project to bring up the word "green" or "red" when one of the buttons is pressed and will appear repeatedly. I've tried my sketch, when i press button1 the serial output will appear "green" and it will repeat. But when I press the button2, the word "green" & "red" appears alternately and repeatedly. Can you help me so that when one of the buttons is pressed it only raises one word, and repeats? Thank you very much.
This my code :
int Pressed1 = 0;
int Pressed2 = 0;
/* BUTTON */
int buttonPin1 = 2;
int buttonPin2 = 3;
void setup() {
// whatever goes here
Serial.begin(9600);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
tombol1();
tombol2();
}
void tombol1() {
if(Pressed1 == 0) { // button has never been pressed
// detect button press
if (digitalRead(buttonPin1) == HIGH) {
Pressed1 = 1;
}
} else { // button has been pressed, do this instead
// your loop
Serial.println("GREEN");
delay(1000);
}
}
void tombol2() {
if(Pressed2 == 0) { // button has never been pressed
// detect button press
if (digitalRead(buttonPin2) == HIGH) {
Pressed2 = 1;
}
} else { // button has been pressed, do this instead
// your loop
Serial.println("RED");
delay(1000);
}
}
Output from the code :
<button1 pressed>
GREEN
GREEN
<when button2 pressed>
GREEN
RED
GREEN
RED
Output what i expected :
<button1 pressed>
GREEN
GREEN
<when button2 pressed>
RED
RED
RED
Thank you very much for your help
*Sorry for my english is not good