#include "pitches.h"
#define NUM_SONGS 3
#define STOPPED 0
#define FORWARD 1
#define RIGHT 2
#define LEFT 3
#define REVERSE 4
const unsigned char PIN_HBRIDGE_1A = 5;
const unsigned char PIN_HBRIDGE_2A = 3;
const unsigned char PIN_HBRIDGE_3A = 6;
const unsigned char PIN_HBRIDGE_4A = 9;
const unsigned char ledPin = 13;
const unsigned char speakerPin = 8;
const unsigned char buttonPin = 2;
const unsigned char int0 = 0;
long ledInterval = 500;
int ledState = LOW;
long ledPreviousMillis = 0;
unsigned char prompt = 1;
int inByte = 0;
unsigned int phase = STOPPED;
int melodyLength[NUM_SONGS];
int* melody[NUM_SONGS];
int* noteDurations[NUM_SONGS];
int melodyLength0 = 8;
int melody0[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
int noteDurations0[] = {
4, 8, 8, 4,4,4,4,4 };
int melodyLength1 = 1;
int melody1[] = {
NOTE_C4};
int noteDurations1[] = {
2};
int melodyLength2 = 16;
int melody2[] = {
NOTE_A4,NOTE_G4,NOTE_G4,NOTE_G4,NOTE_A4,
NOTE_D5,NOTE_E5,NOTE_F5,NOTE_E5,NOTE_D5,NOTE_C5,NOTE_D5,NOTE_E5,
NOTE_D5,NOTE_C5,NOTE_B4};
int noteDurations2[] = {
1,1,8,8,1,8,8,1,8,8,1,8,8,1,2,2};
void setup() {
pinMode(PIN_HBRIDGE_1A, OUTPUT);
pinMode(PIN_HBRIDGE_2A, OUTPUT);
pinMode(PIN_HBRIDGE_3A, OUTPUT);
pinMode(PIN_HBRIDGE_4A, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
attachInterrupt(int0, ButtonInterrupt, RISING);
Serial.begin(9600);
CreateMelodies();
PlayMelody(0);
Serial.println("Starting tank...");
}
void ButtonInterrupt() {
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
if(interruptTime - lastInterruptTime > 200) {
Serial.println("Interrupt!!");
if (++phase == 5)
phase = STOPPED;
}
lastInterruptTime = interruptTime;
}
void loop() {
if (prompt) {
PromptKeyPress();
}
if (Serial.available() > 0) {
ReadKeyPress();
}
switch(phase) {
case STOPPED:
TankStop();
Serial.println("Tank stop");
break;
case FORWARD:
TankForward();
Serial.println("Tank forward");
break;
case RIGHT:
TankRight();
Serial.println("Tank right");
break;
case LEFT:
TankLeft();
Serial.println("Tank left");
break;
case REVERSE:
TankReverse();
Serial.println("Tank reverse");
break;
default:
TankStop();
Serial.println("Tank stop");
break;
}
FlashLed();
}
void CreateMelodies() {
Serial.println("Creating melodies...");
melodyLength[0] = melodyLength0;
melody[0] = melody0;
noteDurations[0] = noteDurations0;
melodyLength[1] = melodyLength1;
melody[1] = melody1;
noteDurations[1] = noteDurations1;
melodyLength[2] = melodyLength2;
melody[2] = melody2;
noteDurations[2] = noteDurations2;
Serial.println("Done.");
}
void PlayMelody(int s) {
Serial.println("Playing melody...");
for (int thisNote = 0; thisNote < melodyLength[s]; thisNote++) {
int noteDuration = 1000/noteDurations[s][thisNote];
tone(speakerPin, melody[s][thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
}
Serial.println("Done.");
}
void PromptKeyPress() {
Serial.println("Press a number");
prompt = 0;
}
void ReadKeyPress() {
inByte = Serial.read();
switch (inByte)
{
case '2':
phase = REVERSE;
break;
case '4':
phase = LEFT;
break;
case '5':
phase = STOPPED;
break;
case '6':
phase = RIGHT;
break;
case '8':
phase = FORWARD;
break;
default:
Serial.println("Invalid command");
break;
}
prompt = 1;
}
void FlashLed() {
if (millis() - ledPreviousMillis > ledInterval) {
ledPreviousMillis = millis();
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}
void TankForward() {
MotorLeftForward();
MotorRightForward();
}
void TankStop() {
MotorLeftStop();
MotorRightStop();
}
void TankReverse() {
MotorLeftReverse();
MotorRightReverse();
}
void TankLeft() {
MotorLeftForward();
MotorRightReverse();
}
void TankRight() {
MotorLeftReverse();
MotorRightForward();
}
void MotorRightStop() {
digitalWrite(PIN_HBRIDGE_1A, LOW);
digitalWrite(PIN_HBRIDGE_2A, LOW);
}
void MotorRightForward() {
digitalWrite(PIN_HBRIDGE_1A, HIGH);
digitalWrite(PIN_HBRIDGE_2A, LOW);
}
void MotorRightReverse() {
digitalWrite(PIN_HBRIDGE_1A, LOW);
digitalWrite(PIN_HBRIDGE_2A, HIGH);
}
void MotorLeftStop() {
digitalWrite(PIN_HBRIDGE_3A, LOW);
digitalWrite(PIN_HBRIDGE_4A, LOW);
}
void MotorLeftForward() {
digitalWrite(PIN_HBRIDGE_3A, LOW);
digitalWrite(PIN_HBRIDGE_4A, HIGH);
}
void MotorLeftReverse() {
digitalWrite(PIN_HBRIDGE_3A, HIGH);
digitalWrite(PIN_HBRIDGE_4A, LOW);
}