#include <ezButton.h>
#include <Servo.h>
#include "pitches.h"
Servo servo1;
int pos = 0;
ezButton b1(11);
ezButton b2(12);
const byte led1 = 8;
const byte led2 = 9;
const byte led3 = 2;
//const byte piezo1 = 7;
//const byte piezo2 = 10;
unsigned long previousMillis = 0;
unsigned long currentMillis;
int b1State;
int b1LastState;
int b2State;
int b2LastState;
bool led1State;
bool led2State;
bool gameStarted;
bool gameOver=false;
bool b2Pressed;
bool piezo1State;
bool piezo2State;
bool button1State;
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo1.attach(5);
b1.setDebounceTime(50);
b2.setDebounceTime(50);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
Serial.println("setup complete");
}
void loop() {
// put your main code here, to run repeatedly:
b1.loop();
b2.loop();
//button1State=false;
currentMillis = millis();
if ((currentMillis - previousMillis >= 3000) &&(gameOver==true)){
// save the last time you blinked the LED
previousMillis = currentMillis;
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
gameOver=false;
gameStarted=false;
}
if (b2Pressed==true){
for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
for (pos = 90; pos <= 0; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
// tell servo to go to position in variable 'pos'
b2Pressed=false;
gameOver==true;
//digitalWrite(led1, HIGH);
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(7, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(7);
}
}
if ((b1.isPressed()) && (gameOver==false)) {
digitalWrite(led1, HIGH);
gameStarted=true;
Serial.println("The button state is true");
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(7, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(7);
}
button1State=false;
Serial.println("The button state is false");
delay(3000);
if (gameStarted==true){
while (gameStarted == true) {
blink();
}
gameOver=true;
digitalWrite(led2, LOW);
Serial.println("game over");
//currentMillis = millis();
}
}
/*
tone(7, NOTE_C4, 8);
tone(10, NOTE_C4, 8);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
*/
}
void blink() {
b2.loop();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (led2State == LOW) {
led2State = HIGH;
} else {
led2State = LOW;
}
if (piezo2State == LOW) {
piezo2State = HIGH;
} else {
piezo2State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(led2, led2State);
}
if (piezo2State == HIGH){
for (int thisNote = 0; thisNote < 2; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 100 / noteDurations[thisNote];
tone(10, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(10);
}
}
if (b2.isPressed()){
Serial.println("button 2 pressed");
//digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
noTone(10);
b2Pressed=true;
gameStarted=false;
Serial.println("game started is false");
}
}