#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 piezo1 = 7;
//const byte piezo2 = 10;
unsigned long previousMillis = 0;
int b1State;
int b1LastState;
int b2State;
int b2LastState;
bool led1State;
bool led2State;
bool gameStarted = false;
bool b2Pressed;
bool button1State = false;
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(100);
b2.setDebounceTime(100);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.println("setup complete");
}
void loop() {
// put your main code here, to run repeatedly:
b1.loop();
{
if(b1.isPressed()){
button1State=true;
Serial.println("The button is pressed");
} else {
button1State=false;
}
}
if (button1State==true){
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);
}
delay(1000);
button1State=false;
Serial.println("The button state is false");
if (gameStarted==true){
while (gameStarted == true) {
blink();
}
}
}
/*
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;
}
// set the LED with the ledState of the variable:
digitalWrite(led2, led2State);
}
if (b2.isPressed()){
Serial.println("button 2 pressed");
gameStarted=false;
}
}