#include <Servo.h>
#include <Stepper.h>
#include "pitches.h"
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
// a b c d e f g
int digits[10][7] = { {1,1,1,1,1,1,0}, // Digit 0
{0,1,1,0,0,0,0}, // Digit 1
{1,1,0,1,1,0,1}, // Digit 2
{1,1,1,1,0,0,1}, // Digit 3
{0,1,1,0,0,1,1}, // Digit 4
{1,0,1,1,0,1,1}, // Digit 5
{1,0,1,1,1,1,1}, // Digit 6
{1,1,1,0,0,0,0}, // Digit 7
{1,1,1,1,1,1,1}, // Digit 8
{1,1,1,1,0,1,1}, // Digit 9
};
int d = 0;
unsigned long startTime_display = 0;
unsigned long interval_display = 1000;
int valX = 0; // Variable to read the value from the analog pin A0
int valY = 0; // Variable to read the value from the analog pin A1
int dwSWT = 0; // Variable to read the status of downward button
int upSWT = 0; // Variable to read the status of upward button
int marbleSW = 0; // Variable to read the status of marble detection switch
int joyXpin = A0; // Analog pin A0 used to connect X of potentiometer
int joyYpin = A1; // Analog pin A1 used to connect Y of potentiometer
int downButt = 12; // Assign pin 12 for downward button
int upButt = 6; // Assign pin 6 for upward button
int marbleGate = 7; // Assign pin 7 for marble detection switch
int marbleCount = 0;
int melody[] = {
NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0,
NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0,
NOTE_C7,
};
int noteDurations[] = {
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
18, 18, 18, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
24 };
Servo myXservo; // Create servo object to control X servo
Servo myYservo; // Create servo object to control Y servo
Stepper myStepper(2038, 8, 9, 10, 11);
void setup() {
myStepper.setSpeed(60);
Serial.begin(9600); // Initialise serial port
for(int i = 37; i >= 31; i--) // Set all LED segment pins to OUTPUT
pinMode(i, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
myXservo.attach(3); // Attach the X servo on pin 3 to the servo object
myYservo.attach(5); // Attach the Y servo on pin 5 to the servo object
pinMode(joyXpin, INPUT);
pinMode(joyYpin, INPUT);
pinMode(downButt, INPUT);
pinMode(upButt, INPUT);
pinMode(marbleGate, INPUT);
for (int thisNote = 0; thisNote < 45; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(2, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(2);
}
}
void loop() {
unsigned currentTime = millis();
valX = analogRead(joyXpin); // Read the value of the potentiometer (value between 0 and 1023)
valY = analogRead(joyYpin);
valX = map(valX, 0, 1023, 120, 60); // Scale it to use it with the servo (value between 120 and 60)
valY = map(valY, 0, 1023, 120, 60);
myXservo.write(valX); // Set the servo position according to the scaled value
myYservo.write(valY);
upSWT = digitalRead(upButt);
dwSWT = digitalRead(downButt); // Read Button state of BUTT from Joystick and store into dwSWT
marbleSW = digitalRead(marbleGate);
if (marbleSW == 1) marbleCount++;
if (marbleCount > 9) marbleCount = 0;
for (int i = 37; i >= 31; i--) {
digitalWrite(i, digits[marbleCount][37-i]); }
delay(75);
Serial.print("Left-Right = ");
Serial.print(valX, DEC);
Serial.print(", Up-Down = ");
Serial.print(valY, DEC);
Serial.print(", DW Butt: ");
Serial.print(dwSWT);
Serial.print(", UP Butt: ");
Serial.print(upSWT);
Serial.print(", MB Switch: ");
Serial.print(marbleSW);
Serial.print(", MB Count: ");
Serial.println(marbleCount);
marbleSW = 0;
if (upSWT == 1) {
myStepper.step(510/4);
upSWT = 0; }
else if (dwSWT == 1) {
myStepper.step(-510/4);
dwSWT = 0; }
}