Hi!
I have 4 push buttons, rather I have that remote with 4 buttons. When I press button A it goes to digital pin 2 of Arudino Uno and it does something (turns on and off LED 5 times). If I press it again it will do the same thing but I don't want that, I want it to do something else, for example blink second LED 4 times and to do this every next time I do it until I press any off the other buttons. That is when I press the button A for the first time it does something but when I press it the second, third, tenth time it will do something else until one of other three buttons are pressed.
Example: when a water sensor senses rain it will close for example blinds but it wont repeat it over and over because it is still raining, it will wait for me to push another button to "unlock" it so when the rain starts again it will close the blinds.
Probably this is very easy but I couldn't find something ti guide me how to do it.
Thanks!
You can set up a 'state machine'. You keep track of the current state you are in, and depending on the button push you go to a different state and do something else.
For example:
byte state;
byte button0 = 2;
byte button1 = 3;
void setup(){
state = 0;
}
void loop()
// determine if the machine state is to change
if (digitalRead(button0)==0 && state == 0){
state = 1;
delay (100); // lazy button debounce for example
}
if (digitalRead(button0) == 0 && state ==1){
state = 2;
delay(100);
}
if (digitalRead(button1) == 0 || (digitalRead(button0) == 0 && state == 2){
state = 0;
delay (100);
}
// now do actions based on the state
switch(state){
case 0:
// code for state = 0
break;
case 1:
// code for state = 1;
break;
case 2:
// code for state = 2;
break;
}
}
Then each case can be as simple or as complicated as you want, say with LED blinks based on blink without delay, keeping track of the numbers of times a button is pushed, etc.
Hmmm will try that, it looks really easy to incorporate in my program. I am making a central locking for my car, because it will be connected to power windows, I will try to make some rain detector so when it rains it will close the windows automatically and keep it closed until I for example lock or unlock the car. But the main thing is that I will use the state when I lock it will lock the car but when I lock again it will do some other state to tell me that the car is locked already.
With this I can also do some other thing which is when the door is opened, car's central locking will send signal back to arduino as a digital read (push button) and I then I could lock it again when I close the door because it will wait other buttons to be pushed.
I will get back to you 
Thanks!
Hello again! I have tried to put it in my code but don't know where! I am using library for one click-long click button and using void as function for different button pushes. I don't know where should I put void_loop things from you example. Can you help me? I watched some example for state machine, it looks simple but where I am now I cannot incorporate it. This is just a testing code on the table, yes I know that I need to change it ti millis and to add two more buttons. There is a photo resistor in the circuit (otpornik)
[code]
#include "OneButton.h"
//otpornik
const int otpornik = A0;
int value;
const int dugmeA = 2;
const int dugmeB = 3;
byte state;
byte buttonA = 2;
byte buttonB = 3;
OneButton dugmeAA(dugmeA, false);
OneButton dugmeBB(dugmeB, false);
int relejcent = 13;
int relejzmig = 12;
int zvuk = 11;
void setup() {
state = 0;
dugmeAA.attachLongPressStart(dugiklikA);
dugmeAA.attachClick(jedanklikA);
dugmeBB.attachLongPressStart(dugiklikB);
dugmeBB.attachClick(jedanklikB);
pinMode(dugmeA, INPUT);
pinMode(dugmeB, INPUT);
pinMode(relejcent, OUTPUT);
pinMode(relejzmig, OUTPUT);
pinMode(zvuk, OUTPUT);
pinMode(otpornik, INPUT);
}
void loop() {
value = analogRead(otpornik);
dugmeAA.tick();
dugmeBB.tick();
delay(1);
}
void jedanklikA(){
if (value > 0.001){
//dan
digitalWrite(relejcent, HIGH);
digitalWrite(relejzmig, HIGH);
delay(2000);
digitalWrite(relejzmig, LOW);
digitalWrite(relejcent, LOW);
}
else
//mrak
{
digitalWrite(relejcent, HIGH);
delay(2000);
digitalWrite(relejcent, LOW);
}
}
void dugiklikA() {
digitalWrite(relejcent, HIGH);
digitalWrite(relejzmig, HIGH);
delay(4000);
digitalWrite(relejcent, LOW);
digitalWrite(relejzmig, LOW);
}
void jedanklikB(){
if (value > 0.001){
//dan
digitalWrite(relejcent, HIGH);
digitalWrite(relejzmig, HIGH);
delay(500);
digitalWrite(relejzmig, LOW);
digitalWrite(relejcent, LOW);
}
else
//mrak
{
digitalWrite(relejcent, HIGH);
delay(500);
digitalWrite(relejcent, LOW);
}}
void dugiklikB() {
digitalWrite(relejcent, HIGH);
digitalWrite(relejzmig, HIGH);
delay(1000);
digitalWrite(relejcent, LOW);
digitalWrite(relejzmig, LOW);
}
// End
[/code]