Untitled
unknown
plain_text
3 years ago
5.2 kB
13
Indexable
// music
int songState = 0;
int melody[] = {
NOTE_F4, NOTE_E4, NOTE_D4, NOTE_CS4,
NOTE_C4, NOTE_B3, NOTE_AS3, NOTE_A3,
NOTE_G3, NOTE_A3, NOTE_AS3, NOTE_A3,
NOTE_G3, NOTE_C4, 0,
NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
NOTE_GS3, NOTE_A3, NOTE_F4, NOTE_C4,
NOTE_C4, NOTE_A3, NOTE_AS3, NOTE_AS3,
NOTE_AS3, NOTE_C4, NOTE_D4, 0,
NOTE_AS3, NOTE_G3, NOTE_G3, NOTE_G3,
NOTE_FS3, NOTE_G3, NOTE_E4, NOTE_D4,
NOTE_D4, NOTE_AS3, NOTE_A3, NOTE_A3,
NOTE_A3, NOTE_AS3, NOTE_C4, 0,
NOTE_C4, NOTE_A3, NOTE_A3, NOTE_A3,
NOTE_GS3, NOTE_A3, NOTE_A4, NOTE_F4,
NOTE_F4, NOTE_C4, NOTE_B3, NOTE_G4,
NOTE_G4, NOTE_G4, NOTE_G4, 0,
NOTE_G4, NOTE_E4, NOTE_G4, NOTE_G4,
NOTE_FS4, NOTE_G4, NOTE_D4, NOTE_G4,
NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_C4,
NOTE_B3, NOTE_C4, NOTE_B3, NOTE_C4, 0
};
int tempo[] = {
8, 16, 8, 16,
8, 16, 8, 16,
16, 16, 16, 8,
16, 8, 3,
12, 16, 16, 16,
8, 16, 8, 16,
8, 16, 8, 16,
8, 16, 4, 12,
12, 16, 16, 16,
8, 16, 8, 16,
8, 16, 8, 16,
8, 16, 4, 12,
12, 16, 16, 16,
8, 16, 8, 16,
8, 16, 8, 16,
8, 16, 4, 16,
12, 17, 17, 17,
8, 12, 17, 17,
17, 8, 16, 8,
16, 8, 16, 8, 1
};
// non blocking setup
// free play
unsigned long previousMillis1 = 0; // time words last changed
const long interval1 = 1500; // interval between changing
// music
unsigned long previousMillis2 = 0; // time last changed
const long interval2 = 100; // interval between notes
int displayStatus = 0; // keep track of what's displayed
int mode = 0; // keep track of game mode -- change to 0 or 1 for different modes
bool countdown = false;
unsigned long previousMillis3 = 0; // time last changed
const long interval3 = 1000; // interval between countdown
int count = 20; // challenge mode timer
const int buzzerAnthony = 10;
const int metalAnthony = 2;
void setup() {
// put your setup code here, to run once:
pinMode(metalAnthony, INPUT); // setup circuit
pinMode(buzzerAnthony, OUTPUT); // setup buzzer
}
void buzz(int targetPin, long frequency, long length) {
tone(targetPin, frequency, length);
delay(length);
noTone(targetPin);
}
void loop() {
// put your main code here, to run repeatedly:
if(mode == 0) {
// challenge mode
if(digitalRead(2) == HIGH) {
delay(25);
if(digitalRead(2) == HIGH) {
countdown = true; // stop the countdown
}
else {
countdown = false; // stop the countdown
}
}
if(countdown) {
showCountdown(); // advance countdown
}
}
else {
// free play
toggleFreePlay();
}
if(digitalRead(metalAnthony) == HIGH) {
buzz(buzzerAnthony, 1000, 500);
}
else {
noTone(buzzerAnthony);
}
}
void showNumber(int number) {
// show numbers (maximum 99) on display
_display->display(0, 25); // write - to segment 1
_display->display(3, 25); // write - to segment 4
// write number to middle of display
if(number == 10) {
_display->display(1,1);
_display->display(2,0);
}
else if(number > 9) {
_display->display(1,1);
int newVal = number - 10;
_display->display(2, newVal);
}
else {
_display->display(1,0);
_display->display(2,number);
}
}
void toggleFreePlay() {
// scroll between words without blocking
unsigned long currentMillis = millis(); // current time
if (currentMillis - previousMillis1 >= interval1) {
previousMillis1 = currentMillis;
if(displayStatus == 1)
showPlay();
else
showFree();
}
}
void showPlay() {
// write "PLAY" to the display
_display->display(0, 21); // write P to segment 1
_display->display(1, 18); // write L to segment 2
_display->display(2, 10); // write A to segment 3
_display->display(3, 4); // write Y to segment 4
displayStatus = 2;
}
void showFree() {
// write "Free" to the display
_display->display(0, 15); // write F to segment 1
_display->display(1, 23); // write r to segment 2
_display->display(2, 14); // write E to segment 3
_display->display(3, 14); // write E to segment 4
displayStatus = 1;
}
void showCountdown() {
// show countdown on display
unsigned long currentMillis = millis();
if (currentMillis - previousMillis3 >= interval3) {
previousMillis3 = currentMillis;
showNumber(count); // show countdown
count--; // decrease countdown
if(count == 0) {
countdown = false; // stop countdown when finished
}
}
}
// CHANGES
// - Moved the `buzzer()` and `tester()` functions outside of the `buzz()` function,
// and removed the `buzzer()` function altogether since it was not being used.
// - Removed the `buzz1()` and `buzz2()` functions since they were not being used.
// - Added code to the `loop()` function to check if the metal ring is touching the
// electrical wire, and if so, to buzz the buzzer.
// - Removed the `previousButtonState` variable and instead checked the state of the
// metal ring in the `loop()` function directly.
// - Updated the pin numbers used for the metal ring and buzzer to match the pin
// numbers in the `setup()` function.
Editor is loading...