I'm trying to combine two sketches: One blinks three leds and the other one plays melody. They work well separately, but when I put them into the same sketch, they function one after the other. Not at the same time, how I like it to work. How could I do that?
Here's the sketch:
int redPin = 12; // Red LED connected to digital pin 12
int greenPin = 11; // Green LED connected to digital pin 11
int yellowPin = 10; // Yellow LED connected to digital pin 10
int speakerPin = 8;
int length = 40; // the number of notes
char notes[] = "cccedddfeeddc eeeegfddddfecccedddfeeddc "; // a space represents a rest
int beats[] = { 1,1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,1,2,2,1,1,1,1,2,2,1,1,1,1,1,1,1,1,1,1,1,1,3,1};
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names == note) {
_ playTone(tones*, duration);_
_ }_
_ }_
_}_
void setup() {
_ pinMode(speakerPin, OUTPUT);_
_ pinMode(redPin, OUTPUT); // sets the digital pin as output*_
* pinMode(greenPin, OUTPUT); // sets the digital pin as output*
* pinMode(yellowPin, OUTPUT); // sets the digital pin as output*
}
void loop() {
* for (int i = 0; i < length; i++) {*
_ if (notes == ' ') {
delay(beats * tempo); // rest
* } else {_
playNote(notes, beats _ tempo);
* }
// pause between notes*
* delay(tempo / 2);
}
digitalWrite(redPin, HIGH); // sets the Red LED on*
* digitalWrite(greenPin, LOW); // sets the Green LED off*
* digitalWrite(yellowPin, LOW); // sets the Yellow LED off*
* delay(500); // waits for half a second*
* digitalWrite(redPin, LOW); // sets the Red LED off*
* digitalWrite(greenPin, HIGH); // sets the Green LED on*
* digitalWrite(yellowPin, LOW); // sets the Yellow LED off*
* delay(500); // waits for half a second*
* digitalWrite(redPin, LOW); // sets the Red LED off*
* digitalWrite(greenPin, LOW); // sets the Green LED off*
* digitalWrite(yellowPin, HIGH); // sets the Yellow LED on*
* delay(500); // waits for half a second*
* digitalWrite(redPin, LOW); // sets the Red LED off*
* digitalWrite(greenPin, LOW); // sets the Green LED off*
* digitalWrite(yellowPin, LOW); // sets the Yellow LED off*
* delay(500); // waits for half a second*
* digitalWrite(redPin, HIGH); // sets the Red LED on*
* digitalWrite(greenPin, HIGH); // sets the Green LED on*
* digitalWrite(yellowPin, HIGH); // sets the Yellow LED on*
* delay(1000); // waits for a second*
* digitalWrite(redPin, LOW); // sets the Red LED off*
* digitalWrite(greenPin, LOW); // sets the Green LED off*
* digitalWrite(yellowPin, LOW); // sets the Yellow LED off*
* delay(500); // waits for half a second*
}_