/*
* Record & Play Button Presses Template
* "records" button presses and plays them back sequentially using LEDs.
* Requires 6X buttons and 6X LED's 4X
* Full tutorial available on
* on www.learnrobotics.org/blog
*
* February 20, 2019
* Liz Miller
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int arr[10];
boolean previous = LOW;
unsigned long time1 = 0;
int debounce = 200;
int index_state = 0;
boolean empty = false;
int button[4] = {11,10,9,8};
int leds[4] = {5,4,3,2};
int play_led = 7;
int play_button=13;
int record_led = 6;
int record_button=12;
int record_count = 0;
int state = HIGH;
bool full = false;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
for(int i = 0;i<5 ;i++){
pinMode(button[i],INPUT);
pinMode(leds[i],OUTPUT);
}
for(int n=0;n<10;n++) arr[n] = 7;//initialization
pinMode(record_led, OUTPUT);
pinMode(play_led, OUTPUT);
digitalWrite(play_led,HIGH);
empty=true;
}
void blink_record(int repeat){
for(int i=0; i<repeat; i++){
digitalWrite(record_led, HIGH);
delay(100);
digitalWrite(record_led,LOW);
delay(100);
}
}
/*
* Shuts off 4 LED's
* Sequence array is NOT empty
*/
void all_off(){
for(int i=0;i<5;i++){
digitalWrite(leds[i],LOW);
}
empty=false;
}
/*
* Prints out the sequence array to Serial
*/
void show_array(){
for (int i = 0; i<10; i++){
lcd.print(arr[i]);
}
lcd.println();
}
/*
* Erases the sequence array by filling with arbitrary number we'll never use
* Sequence array is empty
*/
void erase_stored(){
for(int n=0;n<10;n++) arr[n] = 7; //random number we'll never use
empty=true;
}
/*
* Plays back the sequence routine when triggered
* Erases the sequence array upon completion
*/
void playback(){
digitalWrite(play_led,HIGH);
digitalWrite(record_led,LOW);
for(int i = 0; i<10; i++){
if (arr[i]!=7){
digitalWrite(leds[arr[i]],HIGH);
delay(250);
digitalWrite(leds[arr[i]],LOW);
delay(250);
}
}
digitalWrite(play_led,LOW);
erase_stored(); //erases array for the next time
delay(1000);
digitalWrite(play_led,HIGH);
show_array();
index_state=-1;
}
/*
* Records and plays back a sequence using button presses
* Record button must be triggered to record a sequence
* Play button triggers the playback
*/
void play_record(){
boolean mode = digitalRead(button[0]) || digitalRead(button[1]) || digitalRead(button[2]) || digitalRead(button[3]) || digitalRead(record_button) || digitalRead(play_button);
//wait here till one of the pushbutton goes high;
while(mode == LOW && record_count <= 1){
mode = digitalRead(button[0]) || digitalRead(button[1]) || digitalRead(button[2]) || digitalRead(button[3]) || digitalRead(record_button) || digitalRead(play_button);
if( mode == HIGH && previous == LOW && millis() - time1 > debounce){
//find out which one is high and store that value in array;
/*
* EXAMPLE CASE for single button.
* Add additional readings to check additional buttons
*/
if(digitalRead(button[0]) && record_count ==1) {
arr[index_state] = 0; //set the button 0 to the current index
digitalWrite(leds[0],HIGH); //turn on the respective LED
delay(50); //briefly turn on LED to show which button was pressed
all_off(); //make sure all LEDs are OFF
}
//**** add your button conditions here ****
else;
//print the value and the index;
lcd.print(arr[index_state]);
lcd.print(" Stored in index: ");
lcd.println(index_state);
/*
* once we reach an index of 9 we need to reset the
* index so that we don't overflow the sequence array
*/
if(index_state < 9){
index_state = (index_state + 1);
}
else{ //wait for PLAY PRESS?
all_off();
blink_record(5); //optionally blink the record LED
index_state = 0; //reset
}
state = !state;
//update value of time1 for button debouncing
time1 = millis();
}
}
}
void loop(){
play_record();
}