(An Attempt at a Musical See Saw)
The premise of the demo was to create a See Saw that teeters to and fro that can play music when on one end and be silent on the other. This project attempts to explore why and how my example came to be.
The project had its many ups and downs. The code initially did not work for the Servo, so I had to reorganize for that. The major issue however, is not one of coding or electrical, I lacked a heavy enough ball to press in the switch to allow the rest of the code to work in harmony.
#include <Servo.h> Servo rocker; const int SWITCH_PIN = 9; const int SPEAKER_PIN = 5; void setup() { Serial.begin(115200); Serial.println("Hello!"); rocker.attach(7); } const float note_table[] = { 329.63, 392.00, 293.66, 261.63, 293.66, 329.63, 392.00, 293.66, 329.63, 392.00, 587.33, 523.25, 392.00, 349.23, 329.63, 293.63, -1 }; const float freq_table[] = { 1000, 500, 1000, 250, 250, 1000, 500, 1000, 1000, 500, 1000, 500, 1000, 250, 250, 1000}; int nextnote = 0; void loop() { while(digitalRead(SWITCH_PIN) == 0) { // busywait for the switch to be pressed float freq = note_table[nextnote]; Serial.print("Note: "); Serial.println(freq); tone(SPEAKER_PIN, freq); delay(freq_table[nextnote]); nextnote = nextnote + 1; if (note_table[nextnote] < 0) nextnote = 0; rocker.write(45); } while(digitalRead(SWITCH_PIN) != 0) {// busywait for the switch to be released noTone(SPEAKER_PIN); rocker.write(0); } }
This is the code that I used in order to create the flicking aspect of the See Saw and the creation of the song to play while the switch is pressed in.
Leave a Reply
You must be logged in to post a comment.