I am still very new to programming an Arduino. I have this code that changes states, but it does not repeat. It works once and I then need to go through all the counts again to repeat. How can I keep each state repeating without having to go through the count again.
Any help would be appreciated.
Code below.
int button = 4;
int pin2 = 2;
int pin7 = 7;
int oldstate = LOW;
int count = HIGH;
void setup() {
pinMode(button, INPUT);
pinMode(pin2, OUTPUT);
pinMode(pin7, OUTPUT);
}
void loop() {
int buttonstate = digitalRead(button);
if (buttonstate == oldstate && buttonstate == HIGH){
if (count == 1){
digitalWrite(pin2,HIGH);
delay(400);
digitalWrite(pin2,LOW);
delay(400);
digitalWrite(pin2,HIGH);
delay(400);
digitalWrite(pin2,LOW);
delay(400);
}
if (count == 2){
digitalWrite(pin7,HIGH);
delay(400);
digitalWrite(pin7,LOW);
delay(400);
digitalWrite(pin7,HIGH);
delay(400);
digitalWrite(pin7,LOW);
delay(400);
}
if (count < 2);
else
count = 0;
count++;
delay(50);
}
oldstate = buttonstate;
}
Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
When I add the if (buttonstate == oldstate != buttonstate == HIGH){
It no longer toggles through the states. It just moves back and forth.
I need to control the count, but also loop state 1 or 2.
The led on pin 7 blinks.
When I use the if (buttonstate == oldstate && buttonstate == HIGH){
it stays on the state. It will run each command in the assigned state 1 or 2 depending on if I press the button once or twice, but when I change the && to != if (buttonstate == oldstate != buttonstate == HIGH){
then it does not change from the button press it moves through each state by its self.
The toggle works perfectly. But the state only runs once. I am trying to get the state to repeat over and over again. So if I push the button once, and I go to state one
this is still an unsufficient description of the functionality that you want to have.
As the number of things that happen is pretty small it will be no problem for you to describe
FOUR
things:
the times when you the user presses a button
what the code shall do after a press
writing additional lines what happens if you do not press the button
writing down a new line what will happen then
so that all describing lines add up to a description that shows all the different things that shall happen or that will happen if you press a button or you don't press a button.
A description what a bytstanding person could describe by simply watching you using the microcontroller to demonstrate what the program does.
Split you sketch into 2 stages like this pseudo code
start of loop()
//button handling
if the button becomes pressed
increment count
if count greater than 2
reset count to zero
end if
end if
//act on current state value
if state equals 0
code for state 0
end if
else
if state equals 1
code for state 1
end if
else
if state equals 2
code for state 2
end if
end of loop()
I would like to press it once for state 1. Twice for state 2.
When pressed once I would like it to run the state 1 code I have written and then loop it till I press the button a second time. When pressed the second time I would like it to run the code in state 2 and loop it.
3.If I press the button once and state one comes on, I want it to stay on sate one and loop over and over again. So if it is a blinking led for state one. It must stay on state one and loop state one forever, or till I press the button again to move to state two. When it moves to state two it then flashes the second led continuously till I press the pin to go back to sate one.
It must maintain its state and loop forever.
Please let me know if these 3 answer any questions. Thank you.
Is this a better description?
I want to build a light, but I would like it to have many modes like Christmas tree lights. So if I have 10 led's, I would like to click the button once and it moves to a list of coding on the first increment then the lights flash on and off, and repeat the same way until I press the button again and move to another the second state where it will flash in a different until I then press the button again and it moves back to its original state. Just like a set of Christmas tree lights with a controller.