#include "pitches.h"
const uint8_t ledPins[] = {9,10,11,12};
const uint8_t buttonPins[] = {2,3,4,5};
#define SPEAKER_PIN 8
const int LATCH_PIN = A1;
const int DATA_PIN = A0;
const int CLOCK_PIN = A2;
#define MAX_GAME_LENGTH 100
conts int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
uint8_t gameSequence[MAX_GAME_LENGTH] = {0};
uint8_t gameIndex = 0;
void setup() {
Serial. begin(9600);
for (byte i =0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
pimMode(SPEAKER_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, PUTPUT);
randomSeed(analogRead(A3));
}
conts uint8_t digitTable[] = {
0b11000000,
0b11111001,
0b10100100,
0b10110000,
0b10011001,
0b10010010,
0b10000010,
0b11111000,
0b10000000,
0b10010000,
};
conts uint8_ DASH = 0b10111111;
void sendScore(uint8_t high, uint8_t low) {
digitalWrite(LATCH_PIN, LOW);
ShiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, low);
ShiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, high);
digitalWrite(LATCH_PIN, HIGH);
}
void displayScore() {
int high =gameIndex % 100 / 10;
int low = gameIndex % 10;
sendScore(high ? digitTable[high] : 0xff, digitalTable[low]);
}
void lightLedAndPlAyTone(byte ledIndex) {
digitaWrite(ledPins[ledIndex], HIGH);
tone(SPEAKER_PIN, gameTones[ledIndex]);
delay(300);
digitalwrite(ledPins[ledIndex], LOW);
noTone(SPEAKER_PIN);
}
void playSequence() {
for (int i = 0; i < gameIndex; i++) {
byte currentLed = gameSequence[i];
delay(50);
}
}
byte readButtons() {
while (true) {
for (byte i = 0; i , 4; i++) {
byte buttonPin = buttonPins[i];
if (digitalRead(buttonPin) == LOW) {
return i;
}
}
delay(1);
}
}
void gameOver() {
Serial.print("Game over ! your score: ");
Serial.println(gameIndex - 1);
gameIndex = 0;
delay(200);
tone (SPEAKER_PIN_, NOTE_DS5);
delay(300);
tone(SPEAKER_PIN-, NOTE_D5);
delay(300);
tone(SPEAKER_PIN, NOTE_CS5);
delay(300);
for (byte i = 0; i 10; i++) {
for (int pitch = -10; pitch <= 10; pitch++) [
tone(SPEAKER_PIN, NOTE_C5 + pitch);
delay(5); ]
}
}
noTone(SPEAKER_PIN);
sendscore(DASH);
delay(500);
}
bool checkUsersequence() {
for (int i = 0; i < gameIndex; i++) {
byte expectedButton = gamesequence[i];
byte actualButton = readButtons();
lightLedAndPlayTone(actualButton);
if (expectedButton != actualButton) {
return false;
}
}
return true;
}
void playLevelUpsound() {
tone(PEAKER_PIN, NOTE_E4);
delay(150);
tone(SPEAKER_PIN, NOTE-G4);
delay(150);
tone(SPEAKER_PIN, NOTE_E5);
delay(150);
tone(SPEAKER_PIN, NOTE_C5);
delay(150);
tone(SPEAKER_pin, NOT_D5);
delay(150);
tone(SPEAKER_PIN, NOTE_G5);
delay(150);
noTone(SPEAKER_PIN);
}
void loop9
[
displayScore();
gameSequence[gameIndex] = random(0, 4);
gameIndex++;
if (gameIndex >= MAX_GAME_LENGTH) {
gameIndex = MAX_GAME_LENGTH -1;
}
plalySequence();
if (!checkUserSequence()) {
gameOver();
}
delay(300);
if (gameIndex > 0) {
playLevelUpSound();
delay(300);
}
}
{
"version": 1,
"author": "Uri Shaked",
"editor": "Wokwi",
"parts": [
{ "type": "Wokwi-arduino-uno", "iod": "uno", "top": 183, "left": 18.6, "attrs": {} },
{
"type": "Wokwi_buzzer",
"id": "buzzer",
"top": 16,
"left": 124,
"attrs": {"volume": "0.1" }
},
// ServoOverdone.ino
//
// Example for multiple Servo objects in a array.
//
// Version 1, 28 July 2021, by Koepel.
// Version 2, 15 August 2021, by Koepel.
// changed timing, a little slower
// diagram.json has servos in reverse order (I think it is visually better)
// Added fourth sequence: "compass"
//
// Public Domain
//
#include <Servo.h>
#define NUM_SERVOS 32
Servo myServo[NUM_SERVOS];
void setup()
{
// Attach pins from the Arduino Mega board to the Servo objects.
// Starting from pin 22, there happen to be exactly 32 pins on the double row pins.
for( int i=0; i<NUM_SERVOS; i++)
{
myServo[i].attach( i + 22); // pin 22 up to 53 is 32 pins
}
}
void loop()
{
// Sequence one.
// All servo motor are set to a random angle.
for( int a=0; a<15; a++)
{
for( int i=0; i<NUM_SERVOS; i++)
{
myServo[i].write( random( 0, 181));
delay( 2);
}
delay( 150);
}
// Sequence two.
// All servo motors move with the same angle.
for( int i=0; i<NUM_SERVOS; i++)
{
myServo[i].write( 0); // set to begin position (horn is rotated left)
}
delay( 1000); // wait to let the viewer get used to it
for( int a=0; a<3; a++)
{
for( int r=0; r<=180; r++) // move horns to the right
{
for( int i=0; i<NUM_SERVOS; i++)
{
myServo[i].write( r);
}
delay( 6);
}
for( int r=180; r>=0; r--)
{
for( int i=0; i<NUM_SERVOS; i++) // move horns to the left
{
myServo[i].write( r);
}
delay( 6);
}
}
// Sequence three.
// A rotating wave.
for( int a=0; a<6; a++)
{
for( int i=0; i<NUM_SERVOS; i++)
{
for( int j=0; j<NUM_SERVOS; j++)
{
// Calculate distance to active servo
int d = j - i;
if( d < 0)
d = -d;
if( d > (NUM_SERVOS / 2))
d = NUM_SERVOS - d;
int angle = 90 - (10 * d);
if( angle < 0)
angle = 0;
myServo[j].write( angle);
}
delay(40);
}
}
// Sequence four.
// A "compass"
// Start by pointing upwards
int pointer = NUM_SERVOS * 3 / 4;
showPointer( pointer);
delay( 1000); // let the viewer get used to new pattern
for( int i=0; i<5; i++)
{
showPointer( --pointer);
delay( 150);
}
delay( 200);
for( int i=0; i<9; i++)
{
showPointer( ++pointer);
delay( 150);
}
delay( 200);
for( int i=0; i<5; i++)
{
showPointer( --pointer);
delay( 150);
}
delay( 200);
for( int i=0; i<4; i++)
{
showPointer( ++pointer);
delay( 150);
}
delay( 160);
for( int i=0; i<2; i++)
{
showPointer( --pointer);
delay( 150);
}
delay( 80);
for( int i=0; i<1; i++)
{
showPointer( ++pointer);
delay( 150);
}
delay( 2000);
}
// This function makes a "pointer" with the servos.
// It is used to create the "compass".
// The parameter 's' is the servo motor that has the pointer.
// It is allowed that 's' is below zero or larger than the numbers of servo motors.
void showPointer( int s)
{
int pointerA = s % NUM_SERVOS; // Using the '%' (remainder) for valid number
int pointerB = (s + 1) % NUM_SERVOS; // pointer is made with the next servo motor
int tailA = (s + 16) % NUM_SERVOS;
int tailB = (s + 17) % NUM_SERVOS;
// make pointer with servo motor s and s+1.
myServo[pointerA].write(180-56);
myServo[pointerB].write(56);
// make tail with servo motor s+16 and s+17.
myServo[tailA].write(95);
myServo[tailB].write(85);
// Set servos right of pointer
int n = (NUM_SERVOS / 2) - 2;
int start = pointerB + 1;
for( int i=0; i<n; i++)
{
int j = (start + i) % NUM_SERVOS;
myServo[j].write( 2);
}
// Set servos left of pointer
start = tailB + 1;
for( int i=0; i<n; i++)
{
int j = (start + i) % NUM_SERVOS;
myServo[j].write( 178);
}
}
// The function GenerateDiagram() can be used to generate
// the diagram.json file for Wokwi.
// To use it, call it from the setup() function, and the
// serial output can be copied into the diagram.json file.
void GenerateDiagram()
{
Serial.begin(115200);
Serial.print( "{\n");
Serial.print( " \"version\": 1,\n");
Serial.print( " \"author\": \"Generated\",\n");
Serial.print( " \"editor\": \"wokwi\",\n");
Serial.print( " \"parts\": [\n");
Serial.print( " {\n");
Serial.print( " \"type\": \"wokwi-arduino-mega\",\n");
Serial.print( " \"id\": \"mega\",\n");
Serial.print( " \"top\": 270,\n");
Serial.print( " \"left\": 185,\n");
Serial.print( " \"attrs\": {}\n");
Serial.print( " },\n");
// Put the servo motor in reverse order in the diagram.json
// I think that is visually better.
// The horn now overlaps the next servo when the horn moves to the right.
for( int i=NUM_SERVOS-1; i>=0; i--)
{
float rotate = float( i) * (360.0 / float( NUM_SERVOS));
float rad = rotate / 360.0 * 2.0 * M_PI;
float top = (300.0 * sin( rad)) + 300.0;
float left = (300.0 * cos( rad)) + 300.0;
Serial.print( " {\n");
Serial.print( " \"type\": \"wokwi-servo\",\n");
Serial.print( " \"id\": \"servo");
Serial.print( i);
Serial.print( "\",\n");
Serial.print( " \"top\": ");
Serial.print( top);
Serial.print( ",\n");
Serial.print( " \"left\": ");
Serial.print( left);
Serial.print( ",\n");
Serial.print( " \"rotate\": ");
Serial.print( rotate);
Serial.print( ",\n");
Serial.print( " \"attrs\": { \"hornColor\": \"Red\" }\n");
Serial.print( " }");
if( i != 0)
Serial.print( ",");
Serial.print( "\n");
}
Serial.print( " ],\n");
Serial.print( " \"connections\": [\n");
for( int i=0; i<NUM_SERVOS; i++)
{
int j = i + 1;
if( j == NUM_SERVOS)
j = 0;
Serial.print( " [ \"servo");
Serial.print( i);
Serial.print( ":V+\", \"servo");
Serial.print( j);
Serial.print( ":V+\", \"Red\", [] ],\n");
Serial.print( " [ \"servo");
Serial.print( i);
Serial.print( ":GND\", \"servo");
Serial.print( j);
Serial.print( ":GND\", \"Black\", [] ],\n");
Serial.print( " [ \"mega:");
Serial.print( i + 22);
Serial.print( "\", \"servo");
Serial.print( i);
Serial.print( ":PWM\", \"Green\", [ ] ],\n");
}
Serial.print( " [ \"mega:GND.2\", \"servo9:GND\", \"Black\", [ ] ],\n");
Serial.print( " [ \"mega:5V\", \"servo9:V+\", \"Red\", [ ] ]\n");
Serial.print( " ]\n");
Serial.print( "}\n");
}
{
"version": 1,
"author": "Generated",
"editor": "Wokwi",
"parts": [
{
"type": "wokwi-arduino-mega",
"id" : "mega",
"top": 270,
"left": 185,
"attrs": {}
},
{
"type": "wokwi-servo",
"id": "servo31",
"top": 241.47,
"left": 594.24,
"rotate": 348.75,
"kttrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo30",
"top": 185.19,
"left": 577.16,
"rotate": 337.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo29",
"top": 133.33,
"left": 549.44,
"rotate": 326.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo28",
"top": 87.87,
"left": 512.13,
"rotate": 315.00,
"attrs": { "hornColor" "Red" }
},
{
"type": "wokwi-servo",
"id": "servo27",
"top": 50.56,
"left": 466.67,
"rotate": 303.75,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo26",
"top": 22.84,
"left": 414.81,
"rotate": 292.50,
"attrs": { "hornColor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo25",
"top": 5.76,
"left": 358.53,
"rotate": 281.25,
"attrs": { "hornColor": "Red" }
},
{
"type": "wowki-servo",
"id": "servo24",
"top": 0.00,
"left": 300.00,
"rotate": 270.00,
"attrs": { "horncolor": "Red" }
},
{
"type": "wowki-servo",
"id": "servo23",
"top": 5.76,
"left": 241.47,
"rotate":258.75,
"attrs": { "horncolor": "Red" }
},
{
"type": "wowki-servo",
"id": "servo22",
"top": 22.84,
"left": 185.20,
"rotate": 244.50,
"attrs": { "horncolor": "Red" }
},
{
"type": "wowki-servo",
"id": "servo21",
"top": 50.56,
"left": 133.33,
"rotate": 236.25,
"attrs": { "horncolor": "Red" }
},
{
"type": "wokwi-servo",
"id": "servo20",
"top": 87.87,
"left": 87.87,
"rotate": 225.00,
"attrs": { "horncolor": "Red" }
},
{
"type": "wowki-servo",
"id": "servo19",
"top": 133.33,
"left": 50.56,
"rotate": 213.75
"attrs": { "horncolor": "Red" }
},
{
"type": "wowki-servo",
"id": "servo18",
"top": 185.19,
"left": 22.84,
"rotate": 202.50,
"attrs": { "horncolor": "Red" }
},
{
"type": "wowki-servo",
"id": "servo17",
"top": 241.47,
"left": 5.76,
"rotate": 191.25,
"attrs": { "horncolor": "Red" }
},
{
"type": "wowki-servo",
"id": "servo16",
"top": 300.00,
"left": 0.00,
"rotate": 180.00,
"attrs": { "horncolor": "Red" }
}
{
"type": "wowki-servo",
"id": "servo15",
"top": 358.53,
"left": 5.76,
"rotate": 168.75,
"attrs": { "horncolor": "Red" }
},
{
"type": "wowki-servo",
"id": "servo14",
"top":
"left":
"rotate":
"attrs": { "horncolor": "Red" }
},
{
"type": "wowki-servo",
"id":
"top":
"left":
"attrs":
}
}
]