/**
Mini piano for Arduino.
You can control the colorful buttons with your keyboard:
After starting the simulation, click anywhere in the diagram to focus it.
Then press any key between 1 and 8 to play the piano (1 is the lowest note,
8 is the highest).
Copyright (C) 2021, Uri Shaked. Released under the MIT License.
*/
#include "pitches.h"
#include <Servo.h>
Servo myservo;
Servo urservo;
#define SPEAKER_PIN 8
const uint8_t buttonPins[] = { 6, 5 };
//const int buttonTones[] = { NOTE_C4, NOTE_D4 };
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
int state = 0;
void setup() {
myservo.attach(4);
urservo.attach(2);
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
int pitch = 0;
if(digitalRead(buttonPins[0]) == LOW){
automatic();
}
if(digitalRead(buttonPins[1]) == LOW && state == 0) {
semi();
state =1;
}
else if(digitalRead(buttonPins[1]) == HIGH && state == 1){
state = 0;
}
// if (pitch) {
// tone(SPEAKER_PIN, pitch);
//} else {
// noTone(SPEAKER_PIN);
// }
}
void automatic(){
//tone(SPEAKER_PIN, NOTE_E4);
myservo.write(180);
urservo.write(0);
delay(1000);
// noTone(SPEAKER_PIN);
myservo.write(0);
urservo.write(180);
delay(1000);
}
void semi(){
//tone(SPEAKER_PIN, NOTE_C4);
myservo.write(180);
urservo.write(0);
delay(1000);
state = 1;
//noTone(SPEAKER_PIN);
myservo.write(0);
urservo.write(180);
delay(1000);
}