Introduction: EXO: Baby Don't Cry
Using Arduino to code part of the song, EXO's Baby Don't Cry.
Resources, references, and inspirations:
- Related course lectures from school.
- Music sheet: EXO - Baby Don't Cry
- http://pasted.co/d1a629c1
- https://www.arduino.cc/en/Tutorial/ToneMelody?from=Tutorial.Tone
Step 1: Materials
Step 2: Turn Bean+ Voltage to 5
Near A3, A4, A5: move the switch toward A5, then connect the Bean+ to Grove Board.
Step 3: Connect Vibration Motor, Magnetic Switch, and the Buzzer to Individual Wires
Step 4: Connect Wires to Grove Board
- Vibration Motor to D0/D1
- Magnetic Switch to D2/D3
- Buzzer to D3/D4
Step 5: You're Done!
Take the magnet and put it on or touch the Magnetic Switch.
Step 6: Code - EXO.ino
/**
* NOTE:
* 1. Bean needs to be on voltage of 5 (5v)
* 2. notes array contain names of available tones, not the actual tone for the song
* 3. Vibration Motor doesn't always stop when magnet is removed from the Magnetic Switch --> 99% of the time
* 4. Additional idea or WIP for Vibration Motor: able to vibrate along with the tones
*
* Material list and links
* 1. Lightblue Bean Plus (+): http://store.punchthrough.com/collections/bean-fa...
* 2. Grove Board: http://store.punchthrough.com/collections/bean-fa...
* 3. Vibration Motor: http://store.punchthrough.com/collections/bean-fa...
* 4. Magnetic Switch: http://store.punchthrough.com/collections/bean-fa...
* 5. Buzzer (Starter kit): http://store.punchthrough.com/collections/bean-fa...
* 6. Magnet: http://store.punchthrough.com/collections/bean-fa...
**/
int speakerPin = 3; //Buzzer: D3/D4
// int touchSensor = 1; //Touch Sensor or Light Sensor: D0/D1
int magneticSwitch = 2; // D2/D3
int vibrationMotor = 0; //D0/D1
int length = 107; //number of notes
char notes[] = "fecffecffabffceagfeffecffecffabffceaaaaaddcfg fgaaaagg aaaaddcfg fgaaafgabaaceeeeccaa aceeeeeccadc dcgggg c"; //a space is a rest, so it won't buzz at that point
int beats[] = {1,1,1,3,1,1,1,3,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,3,1,1,1,3,1,1,1,2,1,1,1,1,1,2,1,2,2,2,1,1,1,1,1,2,1,3,1,1,1,1,1,2,2,1,2,2,2,1,1,1,1,2,1,1,2,1,2,1,1,2,1,1,2,1,2,1,2,1,1,1,1,1,1,1,2,1,2,1,2,2,1,2,2,1,1,1,1,2,1};
int tempo = 150; // Speed at which the notes (and beats) are played
////variables for calculating tone
// int period;
// int toneFrequency;
struct color { uint8_t r; uint8_t g; uint8_t b; };
void playTone(int tone, int duration) {
int sensorState = digitalRead(magneticSwitch);
if (sensorState == 1) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
// Play tone through Buzzer
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
// Vibrate through Vibration Motor
digitalWrite(vibrationMotor, HIGH);
} //for
} else {
// Play tone through Buzzer
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
// Vibrate through Vibration Motor digitalWrite(vibrationMotor, LOW);
} //for
} //else
} //playTone
void playNote(char note, int duration) {
char names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D'}; //capital C for high c
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 587}; //http://pasted.co/d1a629c1
/*Tried to find ways to "create" more tones than the ones already present, such as high and low a instead of just the regular a*/
// For each of the different names, other than the general note names, corresponding tones are presented //char names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D','E','F','G','A'}; //capital C for high c
//int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 587, , , , }; //http://pasted.co/d1a629c1
//char names[] = {'C7', 'D6', 'E5', 'f', 'g', 'a', 'b', 'C', 'D'};
//int timeHigh = period / 2; // Calculating the tone using period value //int tones[] = {2093, 1175, 659}; //regular c = C7, lower c = C8, and higher c = C6 and so on; tones from: https://www.arduino.cc/en/Tutorial/ToneMelody?fro...
//colors for notes color
colors[] = {{255, 0, 0}, {244, 244, 66}, {66, 182, 244}, {0, 255, 0}, {143, 66, 244}, {244, 66, 241}, {146, 244, 66}, {0, 0, 255}, {64, 123, 235}}; //number don't actually matter, just each one need to be a different color
//set bead LED color
for (int i = 0; i < 9; i++) {
if (names[i] == note) {
Bean.setLed(colors[i].r, colors[i].g, colors[i].b);
playTone(tones[i], duration);
Bean.setLed(0, 0, 0);
}
}
}
void setup() {
Serial.begin(57600);
pinMode(magneticSwitch, INPUT);
pinMode(vibrationMotor,OUTPUT); //vibration motor has to be 0 or lower pin
pinMode(speakerPin, OUTPUT);
}
void loop() {
int sensorState = digitalRead(magneticSwitch);
Serial.println(sensorState);
for (int i = 0; i < length && digitalRead(magneticSwitch) == 1; i++) { //if we reached the end the song will stop playing
if (notes[i] == ' ') {
Bean.setLed(0, 0, 0); delay(beats[i]*tempo); //rest
} else {
playNote(notes[i], beats[i]*tempo);
} //pause between notes
delay(tempo / 2);
}
}
Comments
6 years ago
That is really cool. You could use this to give your robots their own ring tones and reaction sounds.