The project is simple : each time a same button is pressed, a different LED is turned on and stays on until the button is pressed again.
I used a counter which increases every time the button is pressed, then assigned a different led to the counter values. The counter resets when it reaches 5, because there are 4 modes (green, yellow, red, all)
The code kinda works, but it's random : I have to press it a bunch of time before it cycles to the next LED. But when I keep it pressed it cycles smoothly, at the same pace.
My understanding is that the counter keeps going during the small amount of time the button is pressed, so the value given is random. Is that correct ? What could I do ? Thanks
int counter = 0;
int butt = 2;
int vert = 13;
int jaune = 12;
int rouge = 11;
void setup() {
pinMode(butt,INPUT);
pinMode(vert,OUTPUT);
pinMode(jaune,OUTPUT);
pinMode(rouge,OUTPUT);
Serial.begin(9600);
}
void loop() {
//Serial.println(digitalRead(butt));
Serial.println(counter);
int sensorValue = butt;
if (digitalRead(sensorValue) == LOW)
{
counter++;
if (counter == 5)
counter = 1;
}
if (counter == 1)
{
digitalWrite(vert,HIGH);
digitalWrite(jaune,LOW);
digitalWrite(rouge,LOW);
delay(500);
}
if (counter == 2)
{
digitalWrite(vert,LOW);
digitalWrite(jaune,HIGH);
digitalWrite(rouge,LOW);
delay(500);
}
if (counter == 3)
{
digitalWrite(vert,LOW);
digitalWrite(jaune,LOW);
digitalWrite(rouge,HIGH);
delay(500);
}
if (counter == 4)
{
digitalWrite(vert,HIGH);
digitalWrite(jaune,HIGH);
digitalWrite(rouge,HIGH);
delay(500);
}
}
OK thanks for the replies. I followed a tutorial on debouncing a button, it works but I don't understand how exactly. Especially the toggle function (which is written "Toggle" in the int then "toggle" in the "void toggle()", how does that compiles ??). And what is this new void ?
I'm sorry but I'm very new to this, I've spent the last 6 days studying arduino and reading tons of articles and tutorials and I feel like my brain is melting. It's very frustrating to not understand how debouncing works, could someone be kind enough to comment the sketch and telling me what it does ? Only the part of the code that is used to debounce of course.
About the state change : I will study this once I understand debouncing, I can't handle all that information at once
Thank you very much, here's the code
int vert = 13;
int jaune = 12;
int butt = 2;
boolean buttpressed = false;
boolean Toggle = false;
int debounce = 0;
void setup() {
pinMode(vert, OUTPUT);
pinMode(jaune,OUTPUT);
pinMode (butt, INPUT);
digitalWrite(vert,HIGH);
digitalWrite(jaune,LOW);
Serial.begin(9600);
// put your setup code here, to run once:
}
void loop() {
if (digitalRead(butt) == HIGH)
{
debounce++;
} else
{
debounce = 0;
buttpressed = false;
Toggle = false;
}
if(debounce >= 5000){ //how long you have to wait before accepting that the butt is pressed
buttpressed = true;
}
if (buttpressed == true && Toggle == false){
Toggle = true;
toggle();
}
}
void toggle()
{
digitalWrite(vert, !digitalRead(vert));
digitalWrite(jaune, !digitalRead(jaune));
Another thing I noticed it is that you have the as pinMode(butt,INPUT). I do not know if you have an external pullup resistor. If you do not use it then the input will be floating consequently will give you erratic inputs values. You can try changing your setup from pinMode(butt,INPUT) to inMode(butt,INPUT_PULLUP). That way will setup the pin with the internal pullup resistor enable so you do not require an external resistor.
About the debouncing function some time it is hard to understand the programmer idea to do thing. Since some used tricks to make it more efficiently.
Another suggestion it is try to use the "switch case " function for the repeat use of if (counter == x). It will make easy for you for the switch condition selection..