#include <IRremote.h>
#include <Stepper.h>
#include <Servo.h>
#include <Adafruit_NeoPixel.h>
//________________________________________________________
// CONSTANTS
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define F_NUM_PIXELS 2
#define R_NUM_PIXELS 2
/*
REMOTE
162 226
34 02 194
224 168 144
104 152 176
48 24 122
16 56 90
66 74 82
*/
//________________________________________________________
// PINS
#define HORN A2
#define IR A3
#define S1 9
#define S2 8
#define S3 7
#define S4 6
#define SERVO A4
#define F_PIXEL A1
#define R_PIXEL 2
//________________________________________________________
// GLOBAL VARIABLES
int melodyP[] = {
NOTE_E5, NOTE_D5, NOTE_C5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_C5,
NOTE_D5, NOTE_C5, NOTE_A4, 0, NOTE_A4, NOTE_C5, NOTE_DS5,
NOTE_D5, NOTE_D5, NOTE_E5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_C5,
};
// note durations: 1 = whole note, 2 = half note, 4 = quarter note, 8 = eighth note, etc.
int durationP[] = {
4, 8, 8, 4, 8, 8, 8,
6, 8, 2, 8, 8, 4, 8,
8, 2, 8, 8, 4, 8, 4
};
int melodyN[] = {
NOTE_GS4, NOTE_FS4, NOTE_A4, NOTE_FS4, NOTE_GS4, NOTE_E4, NOTE_FS4,
NOTE_E4, NOTE_GS4, NOTE_FS4, NOTE_A4, NOTE_FS4, NOTE_GS4, NOTE_E4, NOTE_FS4,
0
};
int durationN[] = {
8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8,
4
};
// IR values
IRrecv receiver(IR);
int button = 0, power = 0;
// Stepper Motor
// change this to fit the number of steps per revolution:
const int stepsPerRevolution = 5;
// set the speed:
int rpm = 1000000;
// initialize the stepper library
Stepper myStepper(stepsPerRevolution, S1, S2, S3, S4);
// Servo
Servo arm;
// Variable where the arm's position will be stored (in degrees):
float pos = 90.0;
// Variable used for the arm's position step:
float step = 2.0;
// Neo pixel
Adafruit_NeoPixel FrontLight(F_NUM_PIXELS, F_PIXEL, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel RearLight(R_NUM_PIXELS, R_PIXEL, NEO_GRB + NEO_KHZ800);
uint32_t WhiteColor = FrontLight.Color(255, 249, 229);
uint32_t NoColor = FrontLight.Color(0, 0, 0);
uint32_t RedColor = FrontLight.Color(255, 10, 10);
uint32_t YellowColor = FrontLight.Color(255, 194, 10);
uint32_t TempColor;
// Brakes
int brake = 0;
//________________________________________________________
// METHODS
// Horn
void song(int melody[], int noteDurations[], int totalNotes, int ms) {
for (int thisNote = 0; thisNote < totalNotes; thisNote++) {
// bpm where 1 sec = 60 bpm
int bpm = ms / noteDurations[thisNote];
tone(HORN, melody[thisNote], bpm);
// to distinguish the notes, set a minimum delay between them.
int pauseBetweenNotes = bpm * 1.4;
delay(pauseBetweenNotes);
// stop the tone playing
noTone(HORN);
}
}
void horn1() {
// length of the array = totalSize/singleSize
int arrLen = sizeof(melodyP) / sizeof(*melodyP);
song(melodyP, durationP, arrLen, 1200);
}
void horn2() {
int arrLen = sizeof(melodyN) / sizeof(*melodyN);
song(melodyN, durationN, arrLen, 50);
}
int arrLen1 = sizeof(melodyN) / sizeof(*melodyN), thisNote = 0;
void horn() {
int bpm = 1000 / durationN[thisNote];
tone(HORN, melodyN[thisNote], bpm);
int pauseBetweenNotes = bpm * 1.4;
delay(pauseBetweenNotes);
noTone(HORN);
if (thisNote == arrLen1 - 2) {
thisNote = 0;
receiver.start();
button = 0;
} else {
thisNote++;
}
}
// Stepper Motor
// forward
void forward() {
myStepper.step(stepsPerRevolution);
}
// backward
void backward() {
myStepper.step(-stepsPerRevolution);
}
// Servo
// direction
void direction(char dir) {
if (dir == 'l')
{
if (pos > 0) // Check that the position won't go lower than 0°
{
arm.write(pos); // Set the arm's position to "pos" value
pos -= step; // Decrement "pos" of "step" value
delay(5); // Wait 5ms for the arm to reach the position
}
}
if (dir == 'r') // Check for the blue button input
{
if (pos < 180) // Check that the position won't go higher than 180°
{
arm.write(pos); // Set the arm's position to "pos" value
pos += step; // Increment "pos" of "step" value
delay(5); // Wait 5ms for the arm to reach the position
}
}
}
// Neopixel LEDs
void headLight() {
if (power) {
TempColor = WhiteColor;
} else {
TempColor = NoColor;
}
for (int leds = 0; leds < F_NUM_PIXELS; leds++) {
FrontLight.setPixelColor(leds, TempColor);
FrontLight.show();
}
}
void RearSet(uint32_t rc) {
for (int leds = 0; leds < R_NUM_PIXELS; leds++) {
RearLight.setPixelColor(leds, rc);
RearLight.show();
}
}
void SingleRearSet(uint32_t rc, int led) {
RearLight.setPixelColor(led, rc);
RearLight.show();
}
void brakeLight() {
if (brake) {
TempColor = RedColor;
} else {
TempColor = NoColor;
}
RearSet(TempColor);
}
void leftLight() {
}
void rightLight() {
}
// IR
boolean Power() {
if (button == 162) {
power = abs(1 - power);
button = 0;
headLight();
}
return power;
}
void translateIR()
{
// do{
// Serial.println(button);
// Serial.println(prevButton);
// receiver.stop(); //receiver.disableIRIn();
switch (button) {
case 162:
break;
case 226:
break;
case 34:
break;
case 2:
break;
case 194:
break;
case 224:
break;
case 168:
break;
case 144:
break;
case 104:
brake = abs(1 - brake);
brakeLight();
button = 0;
break;
case 152:
break;
case 176:
break;
case 48:
break;
case 24:
if (!brake)forward();
break;
case 122:
break;
case 16:
SingleRearSet(YellowColor, 0);
direction('l');
SingleRearSet(NoColor, 0);
break;
case 56:
receiver.stop();
horn1();
receiver.start();
button = 0;
break;
case 90:
SingleRearSet(YellowColor, 1);
direction('r');
SingleRearSet(NoColor, 1);
break;
case 66:
break;
case 74:
if (!brake) {
receiver.stop();
backward();
RearSet(WhiteColor);
horn();
RearSet(NoColor);
}
break;
case 82:
break;
default:
break;
}
// delay(50);
// receiver.resume();
// }while(prevButton==button);
}
//________________________________________________________
// MAIN
void setup() {
// Serial.begin(9600);
receiver.start(); //receiver.enableIRIn();
FrontLight.begin();
FrontLight.setBrightness(255);
FrontLight.show();
RearLight.begin();
RearLight.setBrightness(255);
RearLight.show();
myStepper.setSpeed(rpm);
arm.attach(SERVO);
arm.write(pos);
}
void loop() {
// forward();
// direction('l');
// horn2();
delay(10);
if (receiver.decode()) {
button = receiver.decodedIRData.command;
// receiver.stop();
receiver.resume(); // Receive the next value
}
if ((button != 0) && (Power())) {
translateIR();
// Serial.print(button);
}
// receiver.start();
// receiver.resume();
}