#define MotorA1 6
#define MotorA2 7
#define LedA1 2
#define LedA2 12
#define LedB1 3
#define LedB2 4
#define horn 13
#define MotorB1 9
#define MotorB2 10
#include "pitches.h"
int melody[] = {
NOTE_E7, NOTE_E7, 0, 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, 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
};
int noteDurations[] = {
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
};
#define MotorA1 6
#define MotorA2 7
#define LedA1 2
#define LedA2 12
#define LedB1 3
#define LedB2 4
#define MotorB1 9
#define MotorB2 10
#define horn 13
//#define bulb A5
char command = 'S';
char prevCommand = 'A';
int velocity=0;
const int tonePin = horn;
unsigned long previousMillis = 0;
int counter = 0;
int numberOfNotes = sizeof(melody)/sizeof(int);
boolean outputTone = false;
void setup()
{
Serial.begin(9600); //Set the baud rate to that of your Bluetooth module.
pinMode(MotorA1 , OUTPUT);
pinMode(MotorA2 , OUTPUT);
pinMode(MotorB1 , OUTPUT);
pinMode(MotorB2 , OUTPUT);
pinMode (LedA1, OUTPUT);
pinMode (LedA2, OUTPUT);
pinMode (LedB1, OUTPUT);
pinMode (LedB2, OUTPUT);
//pinMode(bulb , OUTPUT);
pinMode(horn , OUTPUT);
// analogWrite(9, 255);
//analogWrite(10, 255);
}
void loop(){
unsigned long currentMillis = millis();
unsigned long noteDuration = 1000 / noteDurations[counter];
unsigned long pauseBetweenNotes = noteDuration * 1.30;
if (counter < numberOfNotes) {
if (outputTone && ((currentMillis - previousMillis) >= noteDuration)) {
previousMillis = currentMillis;
noTone(horn);
outputTone = false;
counter++;
}
else {
if ((currentMillis - previousMillis) >= pauseBetweenNotes) {
previousMillis = currentMillis;
Serial.println(melody[counter]);
if (melody[counter] == 0) {
noTone(horn);
} else {
tone(horn, melody[counter]);
}
outputTone = true;
}
}
}
if(Serial.available() > 0){
prevCommand = command;
command = Serial.read();
Serial.println(command);
//Change pin mode only if new command is different from previous.
if(command!=prevCommand){
//Serial.println(command);
switch(command)
{
case 'F':
digitalWrite(MotorA1, HIGH);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
digitalWrite (LedA1, HIGH);
digitalWrite (LedA2, LOW);
digitalWrite (LedB1, HIGH);
digitalWrite (LedB2, LOW);
break;
case 'B':
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, HIGH);
digitalWrite (LedA1, LOW);
digitalWrite (LedA2, HIGH);
digitalWrite (LedB1, LOW);
digitalWrite (LedB2, HIGH);
break;
case 'L':
digitalWrite(MotorA1, HIGH);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, HIGH);
digitalWrite (LedA1, HIGH);
digitalWrite (LedA2, HIGH);
digitalWrite (LedB1, LOW);
digitalWrite (LedB2, LOW);
break;
case 'R':
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
digitalWrite (LedA1, LOW);
digitalWrite (LedA2, LOW);
digitalWrite (LedB1, HIGH);
digitalWrite (LedB2, HIGH);
break;
case 'S':
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, LOW);
digitalWrite (LedA1, LOW);
digitalWrite (LedA2, LOW);
digitalWrite (LedB1, LOW);
digitalWrite (LedB2, LOW);
break;
case 'V':
digitalWrite(horn, HIGH);
break;
case 'v':
digitalWrite(horn, LOW);
break;
}
}
}
}
I'm trying to play this melody at case 'V' but when I push the on button it just plays a continuous beep which doesn't turn off when pressing the button. The current code plays the tune when the Arduino is powered up. Case V button has no effect on it.