I have 3 push buttons and I want to record the sequence in which they are pressed in array of size 10,i.e, I want to record 10 values for example {1,1,2,2,3,3,1,1,1,3}. I want to blink an array of 3 LED in the same pattern in which the buttons were pressed. I have two extra push buttons for putting the arduino in record and play modes respectively. If any one can suggest how I can store the values in the array I will really be very thankful. I am also facing the problem because it's very difficult to manage the bouncing of the buttons simultaneously.
2 Answers
You can create an array like this, make it global to easy access it:
int states[10];
And the current index:
int index_state = 0;
And initialize the array (in setup):
for (int n = 0; n < 10; n++)
{
states[n] = 0; // Unpressed
}
When a new button press should be stored:
states[index_state] = state;
And add one to the index state, if 10 is reached, start over (this means the last 10 are stored).
index_state = (index_state + 1) % 10;
And to check the states you just iterate from index_state.
Hope this helps you get going.
Note this is called a ring buffer.
-
thanks a lot. It helped me store the button sequence. If you can please suggest me how can I blink the LEDs in same sequence as stored in the array with an delay of 500 millisecond. I am posting my code in the answer section please have a look. Commented Jan 26, 2018 at 18:22
-
The delay of 500 ms might be tricky. That means that for every button you press, you also have to store the time. So you can check if 500 ms have been passed since that press and blink the corresponding LED. Maybe it helps do create the program first for one button and one LED (with 500 ms delay) and than add more buttons/LEDs. Mostly it's better to gradually extend the program instead of going directly to the end goal. Commented Jan 26, 2018 at 22:54
this is my code that stores the button sequence but it will be great if someone can help me blink the LED sequence an put the micro-controller in play or record mode when desired. Also this code faces some issue as some times it fails to take inputs or takes two inputs at a time:
int arr[10];
boolean previous = LOW;
unsigned long time1 = 0;
int debounce = 200;
unsigned long time2 = 0;
int index_state = 0;
void setup()
{
for(int i = 3;i<6 ;i++)
pinMode(i,INPUT);
for(int n=0;n<10;n++) arr[n] = 0;//initialisition
Serial.begin(9600);
}
void loop(){
boolean mode = digitalRead(3) || digitalRead(4) || digitalRead(5);
while(mode == LOW) mode = digitalRead(3) || digitalRead(4) || digitalRead(5);//wait here till one of the pushbutton goes high;
//Serial.println("value");
if( mode == HIGH && previous == LOW && millis() - time1 > debounce){
//find out which one is high and store that value in array;
if(digitalRead(3)) arr[index_state] = 3;
else if(digitalRead(4)) arr[index_state] = 4;
else if(digitalRead(5)) arr[index_state] = 5;
else ;
//print the value and the index;
Serial.print(arr[index_state]);
Serial.print(" Stored in index: ");
Serial.println(index_state);
index_state = (index_state + 1) % 10;
//update value of time1 for button debouncing
time1 = millis();
}
//update the value of previous button state
previous = digitalRead(3) || digitalRead(4) || digitalRead(5);
}
button1[3]=readAndDebounce(PIN_BUTTON1);
?