#include <ezButton.h>
#define RED_LED_PIN 26
#define YELLOW_LED_PIN 27
#define GREEN_LED_PIN 14
#define RED_BUTTON_PIN 32
#define YELLOW_BUTTON_PIN 35
#define GREEN_BUTTON_PIN 25
#define BUZZER_PIN 33
#define TOTAL_LED 3
int pattern[TOTAL_LED];
int randomNumber=0;
int runBuzzer=0;
int pressButtonIndex=0;
int buttonPressed[3];
unsigned long previousMillis = 0;
const long interval = 1000;
const long led_interval = 1000;
const long led_blink_delay = 3000;
ezButton button1(GREEN_LED_PIN);
int pin[] = {RED_LED_PIN,YELLOW_LED_PIN,GREEN_LED_PIN};
int GlastState = HIGH;
int RlastState = HIGH;
int YlastState = HIGH;
void generatePattern(int pattern[], int numLeds) {
// for (int i = 1; i <= numLeds; i++) {
// randomSeed(i);
// randomNumber= patternrandom(1,3);
// }
}
void shuffleArray(int * array, int size)
{
int last = 0;
int temp = array[last];
for (int i=0; i<size; i++)
{
int index = random(size);
array[last] = array[index];
last = index;
}
array[last] = temp;
}
void setup() {
pinMode(RED_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
//pinMode(GREEN_BUTTON_PIN, INPUT_PULLUP);
button1.setDebounceTime(50);
pinMode(YELLOW_BUTTON_PIN, INPUT_PULLUP);
pinMode(RED_BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
// Initially, set all LEDs to OFF
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
}
void Buzzer(){
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
}
int randNumber =0;
void loop() {
button1.loop();
int btn1State = button1.getState();
printf("button 1 state: %d\n",btn1State);
if(button1.isPressed())
printf("The button 1 is pressed");
if(button1.isReleased())
printf("The button 1 is released");
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
}
// int greenbutton = digitalRead(GREEN_BUTTON_PIN);
// if (GlastState != greenbutton) {
// GlastState = greenbutton;
// if (greenbutton == HIGH) {
// printf(" GREEN\n");
// }
// // if (greenbutton == LOW) {
// // // printf(" pressed\n");
// // }
// }
// //shuffleArray(pin,3);
// printf("rand =======%d=%d=%d\n",pin[0],pin[1],pin[2]);
// int greenbutton = digitalRead(GREEN_BUTTON_PIN);
// int yellowbutton = digitalRead(YELLOW_BUTTON_PIN);
// int redbutton = digitalRead(RED_BUTTON_PIN);
// if (GlastState != greenbutton) {
// GlastState = greenbutton;
// if (greenbutton == HIGH) {
// printf(" GREEN\n");
// }
// // if (greenbutton == LOW) {
// // // printf(" pressed\n");
// // }
// }
// if (YlastState != yellowbutton) {
// YlastState = yellowbutton;
// if (yellowbutton == HIGH) {
// printf(" YELLO\n");
// }
// // if (greenbutton == LOW) {
// // // printf(" pressed\n");
// // }
// }
// if (RlastState != redbutton) {
// RlastState = redbutton;
// if (redbutton == HIGH) {
// printf(" RED\n");
// }
// // if (greenbutton == LOW) {
// // // printf(" pressed\n");
// // }
// }
/*****************************/
// if (digitalRead(GREEN_BUTTON_PIN)==HIGH && buttonPressed[pressButtonIndex-1]!=GREEN_LED_PIN){
// printf("GREEN_BUTTON_PIN \n");
// buttonPressed[pressButtonIndex]=GREEN_LED_PIN;
// pressButtonIndex++;
// }else if (digitalRead(YELLOW_BUTTON_PIN)==HIGH && buttonPressed[pressButtonIndex-1]!=YELLOW_BUTTON_PIN){
// printf("YELLOW_BUTTON_PIN \n");
// buttonPressed[pressButtonIndex]=YELLOW_LED_PIN;
// pressButtonIndex++;
// }else if (digitalRead(RED_BUTTON_PIN)==HIGH && buttonPressed[pressButtonIndex-1]!=RED_BUTTON_PIN){
// printf("RED_BUTTON_PIN \n");
// buttonPressed[pressButtonIndex]=RED_LED_PIN;
// pressButtonIndex++;
// }
// if (pressButtonIndex>2){
// //match led patteren
// pressButtonIndex =0; //reset to 0
// if (buttonPressed[0]==pin[0] && buttonPressed[1]==pin[1] && buttonPressed[2]==pin[2]){
// //success and turn off buzzer
// runBuzzer=0;
// printf("SUCCESS\n");
// }else{
// runBuzzer=1;
// //failed
// printf("FAILED\n");
// //beep different tone and keep buzzer on
// }
// }else{
// runBuzzer=1;
// }
// if (runBuzzer==1){
// Buzzer();
// }else{
// delay(500);
// }
// printf("BUTTON BIT =======%d\n",pressButtonIndex);
//delay(500);
}