#include <Servo.h>
#define RED 13
#define GREEN 12
#define BLUE 11
#define YELLOW 10
#define SPEAKER 9
#define RED_TONE 310
#define GREEN_TONE 415
#define BLUE_TONE 209
#define YELLOW_TONE 252
#define GAME_OVER 100
#define TONE_DURATION 500
#define DELAY_TIME 300
int GameOver = 0;
int GameMode = 0;
int Sequence[50];
int SequenceLength;
int Buttons[4] = {RED, GREEN, BLUE, YELLOW};
int Tones[4] = {RED_TONE, GREEN_TONE, BLUE_TONE, YELLOW_TONE};
Servo servoMotor; // Create a servo object to control the servo motor
void SetupAsOutputs();
void SetupAsInputs();
void RunSequence();
void ReadSequence();
void PlayTone(int value, int duration);
void RotateServo();
void setup() {
GameMode = 0;
GameOver = 0;
SequenceLength = 0;
randomSeed(analogRead(A0));
pinMode(SPEAKER, OUTPUT);
servoMotor.attach(8); // Attaching the servo motor to pin 8
}
void loop() {
while (GameOver == 0) {
if (GameMode == 0) {
// Play back sequence
RunSequence();
} else {
// Read Sequence
ReadSequence();
}
}
delay(DELAY_TIME); // Delay a little bit to improve simulation performance
}
void SetupAsOutputs() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(YELLOW, OUTPUT);
}
void SetupAsInputs() {
pinMode(RED, INPUT);
pinMode(GREEN, INPUT);
pinMode(BLUE, INPUT);
pinMode(YELLOW, INPUT);
}
void RunSequence() {
SetupAsOutputs();
// Run through the current sequence
if (SequenceLength > 0) {
for (int index = 0; index < SequenceLength; ++index) {
digitalWrite(Sequence[index], HIGH);
PlayTone(Tones[Sequence[index] - 10], TONE_DURATION);
digitalWrite(Sequence[index], LOW);
delay(DELAY_TIME);
}
}
// Add a new item to sequence
int newValue = random(10, 13 + 1);
Sequence[SequenceLength++] = newValue;
digitalWrite(newValue, HIGH);
PlayTone(Tones[newValue - 10], TONE_DURATION);
digitalWrite(newValue, LOW);
delay(DELAY_TIME);
GameMode = 1;
// Check for score of 10 and activate the servo motor
if (SequenceLength >= 10) {
RotateServo();
}
}
void ReadSequence() {
SetupAsInputs();
int inputButton = 0;
int expectedButton;
for (int index = 0; index < SequenceLength; ++index) {
expectedButton = Sequence[index];
while (inputButton == 0) {
for (int i = 0; i < 4; ++i) {
if (digitalRead(Buttons[i]) == HIGH) {
inputButton = Buttons[i];
break;
}
}
delay(50); // Add a small delay for stability
}
if (inputButton == expectedButton) {
PlayTone(Tones[inputButton - 10], TONE_DURATION);
digitalWrite(inputButton, HIGH);
delay(300); // Keep the button lit for a short duration
digitalWrite(inputButton, LOW);
} else {
GameOver = 1;
PlayTone(GAME_OVER, TONE_DURATION * 3);
break;
}
inputButton = 0; // Reset input for the next iteration
delay(300); // Pause between button presses for stability
}
if (!GameOver) {
delay(1000); // Delay before adding a new item to the sequence
GameMode = 0;
}
}
void PlayTone(int value, int duration) {
tone(SPEAKER, value, duration);
delay(duration);
}
void RotateServo() {
for (int angle = 0; angle <= 180; angle += 2) {
servoMotor.write(angle);
delay(10); // Smaller delay for smoother movement
}
delay(500); // Wait for half a second at 180 degrees
for (int angle = 180; angle >= 0; angle -= 2) {
servoMotor.write(angle);
delay(10); // Smaller delay for smoother movement
}
delay(500); // Wait for half a second at 0 degrees
}