#include <pitches.h>
#include <MemoryFree.h>
#include <Servo.h>
#define sensePin A2
//right sensor
#define trigRight 0
#define echoRight 1
//center sensor
#define trigCenter 2
#define echoCenter 10
//left sensor
#define trigLeft A4
#define echoLeft A5
#define melodyPin 8 //put the buzzer into pin 2
#define melodyPin2 12 //put the buzzer into pin 2
// as stated above,
// connect amp/speaker to FIRST_TONE_PIN and subsequent.
// for example, if FIRST_TONE_PIN is 8 and NUM_OF_VOICE is 9,
// connect amp/speaker to arduino pin 8 and pin 9.
// if using the same amp/speaker for both pins, have each
// pin go through a 1Kish resistor before being summed together
#define FIRST_TONE_PIN 8
#define NUM_OF_VOICES 2
int buffer, buffer2;
int melodyBuffer, harmonyBuffer;
boolean stop_song=false;
boolean check_darkness=false;
// notes in the melody:
int melody[] = {
NOTE_C6, NOTE_G5,NOTE_G5, NOTE_A5, NOTE_G5,0, NOTE_B5, NOTE_C6};
int tones[] = {
NOTE_C6, NOTE_D6,NOTE_E6, NOTE_F6, NOTE_G6,NOTE_A6, NOTE_B6,
NOTE_C7, NOTE_D7,NOTE_E7, NOTE_F7, NOTE_G7,NOTE_A7, NOTE_B7, NOTE_C8
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int tempo[] = {
8, 16, 16, 8,8,8,8,8 };
//int melody[] = {
// NOTE_C6, NOTE_D6,NOTE_E6, NOTE_F6, NOTE_G6,NOTE_A6, NOTE_B6, NOTE_C7};
//int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
// notes in the melody:
PROGMEM prog_uint16_t melody1[] = {
//intro
NOTE_E6, NOTE_E6,0,NOTE_E6,
0, NOTE_C6, NOTE_E6,0,
NOTE_G6,0,
NOTE_G5,0,
//verse 1
NOTE_C6, 0,NOTE_G5,
0,NOTE_E5,0,
0, NOTE_A5, 0, NOTE_B5,
0, NOTE_AS5, NOTE_A5, 0,
NOTE_G5, NOTE_E6, NOTE_G6,
NOTE_A6, 0, NOTE_F6, NOTE_G6,
0, NOTE_E6, 0,N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_D6, NOTE_B5, 0, NOTE_C6,
0, NOTE_E5, NOTE_F5, NOTE_G5,
0, NOTE_C5, NOTE_E5, NOTE_F5,
0, NOTE_GS5, 0,
0, NOTE_F5, 0,
NOTE_E5, 0,
0,
//ending
NOTE_GS5, NOTE_GS5, 0, NOTE_GS5,
0, NOTE_GS5, NOTE_AS5, NOTE_D6,
0, 0, 0, 0,
0, 0, 0, 0,
/*
NOTE_FS5, NOTE_FS5, 0, NOTE_FS5,
0, NOTE_FS5, NOTE_FS5, 0,
NOTE_B5, 0, 0, 0,
NOTE_G5, 0, 0, 0,
*/
//verse 2
};
//verse 2
PROGMEM prog_uint16_t mario_extended_melody[]={
//1
//intro
NOTE_E6, NOTE_E6,0,NOTE_E6,
0, NOTE_C6, NOTE_E6,0,
NOTE_G6,0,0,0,
NOTE_G5,0,0,0,
//2
//verse 1
NOTE_C6, 0, 0,NOTE_G5,
0, 0,NOTE_E5,0,
0, NOTE_A5, 0, NOTE_B5,
0, NOTE_AS5, NOTE_A5, 0,
//3
NOTE_G5, NOTE_E6, NOTE_G6,
NOTE_A6, 0, NOTE_F6, NOTE_G6,
0, NOTE_E6, 0,NOTE_C6,
NOTE_D6, NOTE_B5, 0,0,
//verse 2
//4
NOTE_C6, 0, 0,NOTE_G5,
0, 0,NOTE_E5,0,
0, NOTE_A5, 0, NOTE_B5,
0, NOTE_AS5, NOTE_A5, 0,
//5
NOTE_G5, NOTE_E6, NOTE_G6,
NOTE_A6, 0, NOTE_F6, NOTE_G6,
0, NOTE_E6, 0,NOTE_C6,
NOTE_D6, NOTE_B5, 0,0,
//6
//chorus1
NOTE_E6, NOTE_C6, 0, NOTE_G5,
0, 0, NOTE_GS5, 0,
NOTE_A5, NOTE_F6, 0, NOTE_F6,
NOTE_A5, 0, 0, 0,
//7
NOTE_B5, NOTE_A6, NOTE_A6,
NOTE_A6, NOTE_G6, NOTE_F6,
NOTE_E6, NOTE_C6, 0, NOTE_A5,
NOTE_G5, 0, 0, 0,
//8
//chorus2
NOTE_E6, NOTE_C6, 0, NOTE_G5,
0, 0, NOTE_GS5, 0,
NOTE_A5, NOTE_F6, 0, NOTE_F6,
NOTE_A5, 0, 0, 0,
//9
NOTE_B5, NOTE_F6, 0, NOTE_F6,
NOTE_F6, NOTE_E6, NOTE_D6,
NOTE_C6, NOTE_E5, 0,NOTE_E5,
NOTE_C5, 0, 0, 0,
};
PROGMEM prog_uint16_t mario_extended_melody2[]={
//ending
//10
NOTE_C6, NOTE_C6, 0, NOTE_C6,
0, NOTE_C6, NOTE_D6,0,
NOTE_E6, NOTE_C6, 0, NOTE_A5,
NOTE_G5, 0 , 0, 0,
//11
NOTE_C6, NOTE_C6, 0, NOTE_C6,
0, NOTE_C6, NOTE_D6, NOTE_E6,
0,0, 0, 0,
0, 0, 0, 0,
//12
NOTE_C6, NOTE_C6, 0, NOTE_C6,
0, NOTE_C6, NOTE_D6,0,
NOTE_E6, NOTE_C6, 0, NOTE_A5,
NOTE_G5, 0 , 0, 0,
//start from begining
//intro
//13
NOTE_E6, NOTE_E6,0,NOTE_E6,
0, NOTE_C6, NOTE_E6,0,
NOTE_G6,0,0,0,
NOTE_G5,0,0,0,
//14
NOTE_C6, 0, 0,NOTE_G5,
0, 0,NOTE_E5,0,
0, NOTE_A5, 0, NOTE_B5,
0, NOTE_AS5, NOTE_A5, 0,
//15
NOTE_G5, NOTE_E6, NOTE_G6,
NOTE_A6, 0, NOTE_F6, NOTE_G6,
0, NOTE_E6, 0,NOTE_C6,
NOTE_D6, NOTE_B5, 0,0,
//verse2
//16
NOTE_C6, 0, 0,NOTE_G5,
0, 0,NOTE_E5,0,
0, NOTE_A5, 0, NOTE_B5,
0, NOTE_AS5, NOTE_A5, 0,
//17
NOTE_G5, NOTE_E6, NOTE_G6,
NOTE_A6, 0, NOTE_F6, NOTE_G6,
0, NOTE_E6, 0,NOTE_C6,
NOTE_D6, NOTE_B5, 0,0,
};
PROGMEM prog_uint16_t mario_extended_harmony[]={
//intro 1
NOTE_D4, NO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, NOTE_C4,
0,0,NOTE_G3,0,
//12
NOTE_GS3, 0, 0, NOTE_DS4,
0, 0, NOTE_GS4, 0,
NOTE_G4, 0, 0, NOTE_C4,
0 , 0, NOTE_G3,0,
//start
//intro
//13
NOTE_D3, NOTE_D3,0,NOTE_D3,
0, NOTE_D3, NOTE_D3,0,
0,0,0,0,
NOTE_G4,0,0,0,
//verse 1
//14
NOTE_G3, 0, 0,NOTE_E3,
0, 0,NOTE_C3,0,
0, NOTE_F3, 0, NOTE_G3,
0, NOTE_FS3, NOTE_F3, 0,
//15
NOTE_E3, NOTE_C4, NOTE_E4,
NOTE_F4, 0, NOTE_D4, NOTE_E4,
0, NOTE_C4, 0,NOTE_A3,
NOTE_B3, NOTE_G3, 0,0,
//16
NOTE_G3, 0, 0,NOTE_E3,
0, 0,NOTE_C3,0,
0, NOTE_F3, 0, NOTE_G3,
0, NOTE_FS3, NOTE_F3, 0,
//17
NOTE_E3, NOTE_C4, NOTE_E4,
NOTE_F4, 0, NOTE_D4, NOTE_E4,
0, NOTE_C4, 0,NOTE_A3,
NOTE_B3, NOTE_G3, 0,0,
};
PROGMEM prog_uint16_t underworld_melody[] = {
NOTE_C5, NOTE_C6, NOTE_A4, NOTE_A5,
NOTE_AS4, NOTE_AS5, 0,
0,
NOTE_C5, NOTE_C6, NOTE_A4, NOTE_A5,
NOTE_AS4, NOTE_AS5, 0,
0,
NOTE_F4, NOTE_F5, NOTE_D4, NOTE_D5,
NOTE_DS4, NOTE_DS5, 0,
0,
NOTE_F4, NOTE_F5, NOTE_D4, NOTE_D5,
NOTE_DS4, NOTE_DS5, 0,
0, NOTE_DS5, NOTE_D5, NOTE_CS5,
NOTE_C5, NOTE_DS5,
NOTE_DS5, NOTE_GS4,
NOTE_G4, NOTE_CS5,
NOTE_C5, NOTE_FS5,NOTE_F5, NOTE_E4, NOTE_AS5, NOTE_A5,
NOTE_GS5, NOTE_DS5, NOTE_B4,
NOTE_AS4, NOTE_A4, NOTE_GS4,
0, 0, 0
};
PROGMEM prog_uint16_t underworld_melody1[] = {
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_C4, NOTE_C5, NOTE_A3, NOTE_A4,
NOTE_AS3, NOTE_AS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0,
NOTE_F3, NOTE_F4, NOTE_D3, NOTE_D4,
NOTE_DS3, NOTE_DS4, 0,
0, NOTE_DS4, NOTE_D4, NOTE_CS4,
NOTE_C4, NOTE_DS4,
NOTE_DS4, NOTE_GS3,
NOTE_G3, NOTE_CS4,
NOTE_C4, NOTE_FS4,NOTE_F4, NOTE_E3, NOTE_AS4, NOTE_A4,
NOTE_GS4, NOTE_DS4, NOTE_B3,
NOTE_AS3, NOTE_A3, NOTE_GS3,
0, 0, 0
};
//melody notes in miliseconds
PROGMEM prog_uint16_t underworld_tempo[] = {
200, 200, 200, 200,
200, 200, 400,
800,
200, 200, 200, 200,
200, 200, 400,
800,
200, 200, 200, 200,
200, 200, 400,
800,
200, 200, 200, 200,
200, 200, 400,
400, 133, 133, 133,
400, 400,
400, 400,
400, 400,
133, 133, 133,133, 133, 133,
267, 267, 267,
267, 267, 267,
800, 800, 800
};
PROGMEM prog_uint16_t mel1Durations[] = {
//intro
200, 200, 200,200,
200, 200, 200,200,
200, 600,
200, 600,
//verse1
200, 400,200,
400,200,200,
200, 200, 200,200,
200, 200, 200,200,
267, 267, 266,
200, 200,200,200,
200, 200, 200,200,
200, 200, 400,
//verse2
200, 400,200,
400, 200,200,
200, 200, 200,200,
200, 200, 200,200,
267, 267, 266,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 400,
//chorus
400, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
400, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 600,
//chorus2
400, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
400, 200,200,
200, 200, 400,
200, 600,
800,
//part 3
400, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
400, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 600,
//repeat
400, 200,200,
200, 200, 200, 200,
200, 200, 200, 200,
200, 200, 200, 200,
400, 200,200,
200, 200, 400,
200, 600,
800,
200, 200, 200,200,
200, 200, 200,200,
1600,
/*
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
*/
};
PROGMEM prog_uint16_t mario_extended_melody_durations[]={
//1
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//verse 1
//2
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//3
266, 267, 267,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//verse2
//4
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//5
266, 267, 267,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//chorus1
//6
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//7
266, 267, 267,
266, 267, 267,
200, 200, 200,200,
200, 200, 200,200,
//chorus2
//8
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//9
200, 200, 200,200,
267, 266, 267,
200, 200, 200,200,
200, 200, 200,200,
};
PROGMEM prog_uint16_t mario_extended_melody2_durations[]={
//outro
//10
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//11
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//12
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//ngulang
//13
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//14
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//15
267, 266, 267,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//16
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//17
267, 266, 267,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
};
PROGMEM prog_uint16_t mario_extended_harmony_durations[]={
//1
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//verse 1
//2
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//3
266, 267, 267,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//verse2
//4
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//5
266, 267, 267,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//chorus1
//6
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//7
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//chorus2
//8
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//9
200, 200, 200,200,
267, 266, 267,
200, 200, 200,200,
200, 200, 200,200,
};
PROGMEM prog_uint16_t mario_extended_harmony2_durations[]={
//outro
//10
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//11
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//12
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//ngulang
//13
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//14
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//15
266, 267, 267,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//16
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
//17
266, 267, 267,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
};
/*
PROGMEM prog_uint16_t harm1Durations[] = {
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
200, 200, 200,200,
267, 266, 267,
200, 200, 200,200,
200, 200, 200,200,
};*/
int song_counter = 1;
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 90; // variable to store the servo position
int turn_on =0;
String direction = “forward”;
String direction2 = “forward”;
char msgs[5][15] = {
“Up Key OK “,
“Left Key OK “,
“Down Key OK “,
“Right Key OK “,
“Stop Key OK” };
char start_msg[15] = {
“Start loop “};
int adc_key_val[5] ={
30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
boolean light_a_on = false;
boolean light_b_on = false;
//Standard PWM DC control
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control
int M2 = 7; //M1 Direction Control
///For previous Romeo, please use these pins.
//int E1 = 6; //M1 Speed Control
//int E2 = 9; //M2 Speed Control
//int M1 = 7; //M1 Direction Control
//int M2 = 8; //M1 Direction Control
bool w, x, a, d, q, e, z, c;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int distance_status=0;
int s=0;
//int led1 = 10;
int led2 = 9;
int led3 = 11;
int led4 = 3;
//photodiode
int lightPin = A3; //define a pin for Photo resistor
int ledPin=led3; //define a pin for LED
int ledPin2=led4;
float MIN = 972;
float MAX = 1023;
float ref = MAX – MIN;
float reading = 0;
void light1(int on){
//digitalWrite(13, HIGH);
//delay(20);
if(on==0){
//digitalWrite(led1, LOW);
//analogWrite(led1, 100);
//Serial.println(“led 1 off”);
light_a_on = false;
}else{
//digitalWrite(led1, HIGH);
//analogWrite(led1, 255);
//Serial.println(“led 1 on”);
light_a_on = true;
}
//digitalWrite(13, LOW);
}
void light2(int on){
//digitalWrite(13, HIGH);
//delay(20);
if(on==0){
analogWrite(led2, 150);
digitalWrite(led2, LOW);
//analogWrite(led2, 100);
light_a_on = false;
buzz(melodyPin, tones[7], 50);
}else{
analogWrite(led2, 200);
digitalWrite(led2, HIGH);
//analogWrite(led2, 255);
light_a_on = true;
buzz(melodyPin, tones[5], 50);
}
//digitalWrite(13, LOW);
}
void light3(int on){
//digitalWrite(13, HIGH);
//delay(20);
if(on==0){
digitalWrite(led3, LOW);
//Serial.println(“led 3 off”);
}else{
digitalWrite(led3, HIGH);
//Serial.println(“led 3 on”);
}
//digitalWrite(13, LOW);
}
void light4(int on){
//digitalWrite(13, HIGH);
//delay(20);
if(on==0){
digitalWrite(led4, LOW);
light_b_on = false;
buzz(melodyPin, tones[1], 50);
}else{
digitalWrite(led4, HIGH);
light_b_on = true;
buzz(melodyPin, tones[3], 50);
}
//digitalWrite(13, LOW);
}
void sing(int song) //Move forward
{
//stop_song=false;
//delay(1000);
if(song==0)
song=song_counter;
if(song_counter>6)
song_counter=1;
else
song_counter++;
if(song==1){
Serial.println(“Wistling\n’Do re mi'”);
int size = sizeof(tones) / sizeof(int);
analogWrite(led3, 0);
analogWrite(led4, 0);
for (int thisNote = 0; thisNote < size; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 30;
int d = thisNote*15;
analogWrite(led3, d);
analogWrite(led4, d);
buzz(melodyPin, tones[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 0;
delay(pauseBetweenNotes);
// stop the tone playing:
//buzz(melodyPin, 0,noteDuration);
}
analogWrite(led3, 255);
analogWrite(led4, 255);
}else if(song==2){
Serial.println(“Wistling\n’Do si la'”);
int size = sizeof(tones) / sizeof(int);
analogWrite(led3, 255);
analogWrite(led4, 255);
for (int thisNote = size; thisNote >=0 ; thisNote–) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 30;
int d = thisNote*15;
analogWrite(led3, d);
analogWrite(led4, d);
buzz(melodyPin, tones[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 0;
delay(pauseBetweenNotes);
// stop the tone playing:
//buzz(melodyPin, 0,noteDuration);
}
analogWrite(led3, 0);
analogWrite(led4, 0);
}else if(song==3){
Serial.println(“Wistling\n’Circus Tune'”);
int size = sizeof(melody) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/tempo[thisNote];
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
buzz(melodyPin, melody[thisNote],noteDuration);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.3;
delay(pauseBetweenNotes);
// stop the tone playing:
buzz(melodyPin, 0,noteDuration);
}
}else if(song==4){
Serial.println(“Singing\n’Mario Theme 1′”);
play_song(1);
}else if(song==5){
Serial.println(“Singing\n’Mario Theme 2′”);
play_song(2);
}else if(song==6){
Serial.println(“Singing\n’Mario Theme 3′”);
play_song(3);
}else{
Serial.println(“Singing\n’Underworld Theme'”);
play_song(4);
}
if(light_a_on){ //keep the light on after blinking
analogWrite(led2, 255);
}else{
analogWrite(led2, 0);
}
if(light_b_on){ //keep the light on after blinking
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
}else{
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
}
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
buzz(melodyPin, tones[1], 100);
delay(10);
buzz(melodyPin, tones[1], 100);
direction = “stop”;
}
void advance(int a,int b) //Move forward
{
buzz(melodyPin, tones[0], 100);
buzz(melodyPin, tones[4], 100);
analogWrite (E1,a); //PWM Speed Control
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
//Serial.print(x);
direction = “forward”;
}
void back_off (int a,int b) //Move backward
{
//if(checkDistance(trigPin2, echoPin2)>=23){ //give a bit of lee way than the 20cm
buzz(melodyPin, tones[4], 100);
buzz(melodyPin, tones[0], 100);
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
direction=”reverse”;
direction2=”center”;
//}else{
//emergencyStop();
//}
}
void turn_L (char a,char b) //Turn Left
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
buzz(melodyPin, tones[3], 100);
buzz(melodyPin, tones[5], 100);
}
void turn_R (char a,char b) //Turn Right
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
buzz(melodyPin, tones[4], 100);
buzz(melodyPin, tones[6], 100);
}
void turn_FL (char a,char b) //Forward Left
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
direction = “forward”;
direction2=”left”;
buzz(melodyPin, tones[5], 100);
buzz(melodyPin, tones[7], 100);
}
void turn_FR (char a,char b) //Forward Right
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
direction = “forward”;
buzz(melodyPin, tones[6], 100);
buzz(melodyPin, tones[8], 100);
}
void turn_RL (char a,char b) //Reverse Left
{
//if(checkDistance(trigPin1, echoPin1)>=19){
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
direction = “reverse”;
direction2=”left”;
buzz(melodyPin, tones[7], 50);
buzz(melodyPin, tones[9], 50);
//}else{
// emergencyStop();
//}
}
void turn_RR (char a,char b) //Reverse Right
{
// if(checkDistance(trigRight, echoRight)>=19){
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
direction = “reverse”;
direction2=”right”;
buzz(melodyPin, tones[10], 50);
buzz(melodyPin, tones[8], 50);
// }else{
// emergencyStop();
// }
}
void setup(void)
{
int i;
//pinMode(10, OUTPUT); //we’ll use the debug LED to output a heartbeat
for(i=3;i<=13;i++)
pinMode(i, OUTPUT);
myservo.attach(A1);
myservo.write(90);
int z=0;
int a=0;
/*
for (unsigned char i = 0; i < NUM_OF_VOICES; i++) {
pinMode(i+FIRST_TONE_PIN,OUTPUT);
Serial.println(i);
}
*/
/*pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(10, OUTPUT);
*/
//analog pins
pinMode(sensePin, INPUT);
pinMode(trigRight, OUTPUT);
pinMode(echoRight, INPUT);
pinMode(trigCenter, OUTPUT);
digitalWrite(trigCenter,HIGH); //super sensor URM37
pinMode(echoCenter, INPUT);
pinMode(trigLeft, OUTPUT);
pinMode(echoLeft, INPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600); //Set Baud Rate
Serial.println(“Run keyboard control”);
darkness_setup(true);
sing(1);
delay(50);
sing(2);
delay(100);
}
void loop()
{
adc_key_in = analogRead(A0); // read the value from the sensor
/* get the key */
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) { // if keypress is detected
delay(50); // wait for debounce time
adc_key_in = analogRead(A0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) {
oldkey = key;
if (key >=0){
Serial.println(adc_key_in, DEC);
Serial.println(msgs[key]);
Serial.println(key);
}
}
int val2 =key;
if(val2 != -1)
{
switch(val2)
{
case 0://Move Forward
advance (200,200); //move forward in half speed
break;
case 2://Move Backward
back_off (175,175); //move back in a bit slower
break;
case 1://Turn Left
turn_L (255,255);
break;
case 3://Turn Right
turn_R (255,255);
break;
case 4:
stop();
break;
}
}
else stop();
}
if(Serial.available()){
char val = Serial.read();
if(val != -1)
{
switch(val)
{
case ‘w’://Move Forward
if(w==false){
advance (128,128); //move forward in half (max speed 255)
w=true;
Serial.println(“Forward – Slow”);
}else{
advance (255,255);
w=false;
Serial.println(“Forward – Fast”);
}
break;
case ‘x’://Move Backward
if(x==false){
back_off(128, 128);
Serial.println(“Reverse – Slow”);
x=true;
}else{
back_off (255,255); //move back in slow spee
Serial.println(“Reverse – Fast”);
x=false;
}
break;
case ‘a’://Turn Left
turn_L (250,255);
Serial.println(“Left”);
break;
case ‘d’://Turn Right
turn_R(255, 250);
Serial.println(“Right”);
break;
case ‘q’://Turn Left
turn_FL (60,255);
Serial.println(“Forward Left”);
break;
case ‘e’://Turn Right
turn_FR (255,60);
Serial.println(“Forward Right”);
break;
case ‘z’://Turn Left
turn_RL (60,255);
Serial.println(“Reverse Left”);
break;
case ‘c’://Turn Right
turn_RR (255,60);
Serial.println(“Reverse Right”);
break;
case ‘h’:
Serial.println(“Hello”);
break;
case ‘s’:
stop();
Serial.println(“Stop!”);
break;
case ‘y’:
stop_song=false;
sing(0);
break;
case ‘1’:
tilt(1);
break;
case ‘2’:
tilt(2);
break;
case ‘3’:
tilt(3);
break;
case ‘u’:
light1(1);
light2(1);
Serial.println(“Body light ON”);
light_a_on=true;
break;
case ‘i’:
light3(1);
light4(1);
Serial.println(“Head light ON”);
light_b_on=true;
break;
case ‘o’:
light1(0);
light2(0);
Serial.println(“Body light OFF”);
light_a_on=false;
break;
case ‘p’:
light3(0);
light4(0);
Serial.println(“Head light OFF”);
light_b_on=false;
break;
case ‘l’:
Serial.println(“lcd”);
break;
case ‘t’:
get_temp();
break;
case ‘g’:
darkness_setup(check_darkness);
break;
}
}
}
if(direction==”reverse”){
s = distance_status %4;
if(checkDistance(trigCenter, echoCenter)<25){
Serial.print(“Obstacle\n at rear”);
emergencyStop();
distance_status=0;
}else if(checkDistance(trigLeft, echoLeft)<20){
Serial.print(“Obstacle\n at left”);
emergencyStop();
distance_status=0;
}else if(checkDistance(trigRight, echoRight)<20){
Serial.print(“Obstacle\n at right”);
emergencyStop();
distance_status=0;
}
}else
{
distance_status=0;
s=0;
}
if(check_darkness)
get_darkness();
}
void tilt(int t){
if(t==1){
myservo.write(60);
buzz(melodyPin, tones[9], 100);
buzz(melodyPin, tones[7], 100);
buzz(melodyPin, tones[5], 100);
}else if(t==2){
myservo.write(90);
buzz(melodyPin, tones[10], 100);
buzz(melodyPin, tones[9], 100);
buzz(melodyPin, tones[10], 100);
}else{
myservo.write(120);
buzz(melodyPin, tones[10], 100);
buzz(melodyPin, tones[12], 100);
buzz(melodyPin, tones[14], 100);
}
}
void emergencyStop(){
stop();
for(int s=0;s<2;s++){
light1(1);
light2(1);
delay(50);
light1(0);
light2(0);
delay(50);
}
if(light_a_on){ //keep the light on after blinking
light1(1);
light2(1);
}else{
light1(0);
light2(0);
}
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input <= adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
double checkDistance(int trig, int echo){
long duration, distance;
String sensor =””;
if(trig ==0)
sensor = “Right Sensor”;
else if(trig == 2)
sensor = “Center Sensor”;
else
sensor = “Left Sensor”;
digitalWrite(trig, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trig, HIGH);
// delayMicroseconds(1000); – Removed this line
delayMicroseconds(100); // Added this line
digitalWrite(trig, LOW);
if(trig==2)
duration = pulseIn(echo, LOW) ;
else
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 10){
if(s==0)
Serial.println(trig + “: Out of range”);
}
else {
if(s==0){
Serial.print(sensor);
Serial.print(” : “);
Serial.print(distance);
Serial.println(” cm”);
}
//output.print(distance );
//output.println(” cm”);
}
distance_status++;
delay(50);
return distance;
}
void buzz(int targetPin, long frequency, long length) {
digitalWrite(13,HIGH);
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second’s worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time…
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
digitalWrite(13,LOW);
/*
if(light_a_on){ //keep the light on after blinking
digitalWrite(led2, HIGH);
}else{
digitalWrite(led2, LOW);
}
if(light_b_on){ //keep the light on after blinking
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
}else{
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}*/
}
// no need to modify anything below for basic use
void playTune(prog_uint16_t melody[], int melLength, prog_uint16_t melDurations[], prog_uint16_t harmony[], int harmLength, prog_uint16_t harmDurations[])
{
unsigned int Counts[] = { 0, 0};
unsigned int Periods[] = { 0, 0};
unsigned char States[] = { 0, 0};
unsigned long stopTimeNote1 = 0;
unsigned long stopTimeNote2 = 0;
unsigned char TimedPin = 0;
unsigned int CurrentCount = 0;
unsigned char i;
unsigned char indexMel = 0, indexHarm = 0;
while (!stop_song) {
CurrentCount = Counts[0];
TimedPin = 0;
for (i=1;i<NUM_OF_VOICES;i++) {
if (Counts[i] < CurrentCount) {
TimedPin = i;
CurrentCount = Counts[i];
} // for
}
if (CurrentCount > 3)
delayMicroseconds(CurrentCount);
if (Periods[TimedPin] < 65535)
States[TimedPin] = !States[TimedPin];
if(TimedPin==0)
digitalWrite(led3, HIGH);
else
digitalWrite(led4, HIGH);
int thePin = 0;
if(TimedPin>0)
thePin = 4;
digitalWrite(FIRST_TONE_PIN+thePin,States[TimedPin]);
if(TimedPin==0)
digitalWrite(led3, LOW);
else
digitalWrite(led4, LOW);
if (millis() >= stopTimeNote1) {
if (indexMel >= melLength)
break;
buffer = pgm_read_word_near(melDurations + indexMel);
stopTimeNote1 = millis() + (int)buffer;
melodyBuffer = pgm_read_word_near(melody + indexMel++);
Periods[0] = 1000000 / melodyBuffer;
Serial.print(“Speaker: 1\tduration: “);
Serial.print(buffer);
Serial.print(“\tnote: “);
Serial.println(melodyBuffer);
}
if (millis() >= stopTimeNote2) {
if (indexHarm >= harmLength)
break;
buffer2 = pgm_read_word_near(harmDurations + indexHarm);
stopTimeNote2 = millis() + (int)buffer2;
harmonyBuffer = pgm_read_word_near(harmony + indexHarm++);
Periods[1] = 1000000 / harmonyBuffer;
Serial.print(“Speaker: 2\tduration: “);
Serial.print(buffer2);
Serial.print(“\tnote: “);
Serial.println(harmonyBuffer);
}
for (i = 0; i < NUM_OF_VOICES; i++) {
Counts[i] = Counts[i] – CurrentCount;
}
Counts[TimedPin] = Periods[TimedPin];
if(Serial.available()){
char val = Serial.read();
if(val != -1)
{
switch(val)
{
case ‘y’:
Serial.println(“Shuting up”);
stop_song=true;
break;
}
}
}
}
}
void playTune2(int melody[], int melLength, int melDurations[], int harmony[], int harmLength, int harmDurations[])
{
unsigned int Counts[] = { 0, 0};
unsigned int Periods[] = { 0, 0};
unsigned char States[] = { 0, 0};
unsigned long stopTimeNote1 = 0;
unsigned long stopTimeNote2 = 0;
unsigned char TimedPin = 0;
unsigned int CurrentCount = 0;
unsigned char i;
unsigned char indexMel = 0, indexHarm = 0;
while (1) {
CurrentCount = Counts[0];
TimedPin = 0;
for (i=1;i<NUM_OF_VOICES;i++) {
if (Counts[i] < CurrentCount) {
TimedPin = i;
CurrentCount = Counts[i];
} // for
}
if (CurrentCount > 3)
delayMicroseconds(CurrentCount);
if (Periods[TimedPin] < 65535)
States[TimedPin] = !States[TimedPin];
digitalWrite(FIRST_TONE_PIN+TimedPin,States[TimedPin]);
if (millis() >= stopTimeNote1) {
if (indexMel >= melLength)
break;
stopTimeNote1 = millis() + melDurations[indexMel];
Periods[0] = 1000000 / melody[indexMel++];
}
if (millis() >= stopTimeNote2) {
if (indexHarm >= harmLength)
break;
stopTimeNote2 = millis() + harmDurations[indexHarm];
Periods[1] = 1000000 / harmony[indexHarm++];
}
for (i = 0; i < NUM_OF_VOICES; i++) {
Counts[i] = Counts[i] – CurrentCount;
}
Counts[TimedPin] = Periods[TimedPin];
if(stop_song==true)
break;
}
}
void play_song(int song){
float mem = 0.0;
mem = (float)freeMemory()/2350 *100;
if(mem>100){
Serial.println(“No memory left, try EEPROM or PROGMEM”);
}else{
Serial.print(“freeMemory()=”);
Serial.print(freeMemory());
Serial.print(” (“);
Serial.print(mem);
Serial.println(“%)”);
}
if(song==1){
playTune(melody1, sizeof(melody1)/sizeof(int), mel1Durations, harmony1, sizeof(harmony1)/sizeof(int), mel1Durations);
}else if(song==2){
playTune(mario_extended_melody, sizeof(mario_extended_melody)/sizeof(int), mario_extended_melody_durations, mario_extended_harmony, sizeof(mario_extended_harmony)/sizeof(int), mario_extended_harmony_durations);
}else if(song==3){
playTune(mario_extended_melody2, sizeof(mario_extended_melody2)/sizeof(int), mario_extended_melody2_durations, mario_extended_harmony2, sizeof(mario_extended_harmony2)/sizeof(int), mario_extended_harmony2_durations);
}else{
playTune(underworld_melody, sizeof(underworld_melody)/sizeof(int), underworld_tempo, underworld_melody1, sizeof(underworld_melody1)/sizeof(int), underworld_tempo);
}
}
void get_temp()
{
float avgK = 0;
float avgC = 0;
int sensorValue = 0;
float kelvinValue= 0;
float celsiusValue = 0.0; // convert Kelvin to Celsius
int counter =0;
buzz(melodyPin, tones[2], 50);
Serial.print(“Curent Temp:\t”);
buzz(melodyPin, tones[4], 50);
for(int i=0;i<15;i++){ //get 10 samples
sensorValue += analogRead(sensePin); //reads voltage on Pin A2
//sensorValue +=(((sensorValue / 1023.0) * 5.0) * 100.0);
counter++;
delay(40); //give enough time, to read accurately
}
sensorValue /=counter;
kelvinValue +=(((sensorValue / 1023.0) * 5.0) * 100.0);
celsiusValue = (kelvinValue – 273.0); // convert Kelvin to Celsius
//Serial.println(sensorValue); //returns voltage on a 0 – 1023 scale (0 – 5 Volts)
Serial.print(celsiusValue);
Serial.println(” C”);
Serial.print(kelvinValue);
Serial.print(” K\t”);
float temperatureF = (celsiusValue * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(” F”);
buzz(melodyPin, tones[3], 50);
}
void darkness_setup(boolean s){
if(s==false){
Serial.println(“Light sensor ON”);
check_darkness = true;
buzz(melodyPin, tones[3], 50);
buzz(melodyPin, tones[4], 50);
reading = analogRead(lightPin);
if(MIN>reading)
MIN = reading;
ref = MAX-MIN;
Serial.print(“Range:”);
Serial.print(MIN);
Serial.print(” – “);
Serial.println(MAX);
}else{
Serial.println(“Light sensor OFF”);
check_darkness = false;
buzz(melodyPin, tones[4], 50);
buzz(melodyPin, tones[3], 50);
}
}
void get_darkness()
{
//buzz(melodyPin, tones[3], 50);
//buzz(melodyPin, tones[2], 50);
float avg = 0;
float value = 0;
int counter = 0;
//analogWrite(lightPin, 0);
//analogWrite(lightPin, 255);
//Serial.println(analogRead(lightPin));
for(int i=0;i<50;i++){ //sampling
value = ((float)analogRead(lightPin)-MIN)/ref; //take percentage of darkness
if(value>0&&value<=1.0){//ignore when its 0
avg+=value;
//Serial.print(” “);
//Serial.println(avg);
counter++;
}
delay(3);
}
avg/=counter;
Serial.print(“Darkness level “);
Serial.print(avg*100);
Serial.println(“%”); //Write the value of the photoresistor to the serial monitor.
//buzz(melodyPin, tones[7], 50);
int ledbrightness = 0;
if(avg>0.0300){//onnly turn it on, when it’s dark enough
ledbrightness=avg*255;
//light_b_on=true;
analogWrite(ledPin, ledbrightness); //send the value to the ledPin. Depending on value of resistor
analogWrite(ledPin2, ledbrightness);
}else{
analogWrite(ledPin, 0);
analogWrite(ledPin2,0);
//light_b_on=false;
}
//you have to divide the value. for example,
//with a 10k resistor divide the value by 2, for 100k resistor divide by 4.
//short delay for faster response to light.
//
}
The corresponding Node JS can be found here.
3 thoughts on “UPDATED Arduino Sketch for Robot Car that listens to NodeJS serial commands”
Comments are closed.
Hello!
Just a quick question, is the above code work for DFrobot Romeo V2? and is it support Ultrasonic sensor Obstacle avoidance?
Hi Fawei,
The above code was tested and ran succesfully on DFRobot Romeo V2. It supports simple Arduino-compatible ultrasonic sensor such as HC-SR04. It provides you with Arduino codes library that you can interact with.
Refer to:
http://letsmakerobots.com/node/30209
Hi Fawei,
The above code was tested and ran succesfully on DFRobot Romeo V2. It supports simple Arduino-compatible ultrasonic sensor such as HC-SR04. Refer to:
http://letsmakerobots.com/node/30209