#include <IRremote.h>
#include <avr/io.h>
//We connect the IR receiver to Arduino and begin the communication with the remote
int IR_Recv = 5;
IRrecv irrecv(IR_Recv);
decode_results results;
const int buzz = 4; //buzzer pin
//LED strips pins and their states
const int purple_strip = 3;
const int white_strip = 2;
int state_purple_strip = LOW;
int state_white_strip = LOW;
//Interruption sensor pin and the states
int interruption_sens = 7;
int curent = 0;
int last = 0;
int credit = 0;
//Tempo of songs
int tempo = 150;
int length1 = 26; //jingle bells
char notes1[] = "eeeeeeegcde fffffeeeeddedg";
int beats1[] = {2, 2, 4, 2, 2, 4, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2,2,2,2,2,2,2,2,2, 4,4};
int length2 = 29; //while shephards watched
char notes2[] = "faagfvvagaCCbCaDCvagfeagffef ";
int beats2[] = { 2,3,1,2,2,2,2,2,2,2,2,2,2,6,2,3,1,2,2,2,2,2,2,2,2,2,2,6,2 };
int length3 = 52; //we wish u a merry x mas
char notes3[] = "cffgfedddggagfeccaabagfdccdgefcfffeefedcgagfCcccdgef";
int beats3[] = {2,2,1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2,1,1,1,1,2,2,1,1,2,2,2,4,2,2,2,2,4,2,2,2,2,4,2,2,2,2,2,2,1,1,2,2,2,4};;
int length4 = 40; //jingle bells rock
char notes4[] = "CCCbbbabaeabacgabafdfegagdefg agagaaee";
int beats4[] ={1,1,2,1,1,2,1,1,2,4,1,1,2,2,2,1,1,2,4,2,1,1,2,1,1,1,1,2,4,2,1,1,1,1,2,2,2,4};
//This function will toggle the lights ON/OFF
void toggle_lights() {
if (state_purple_strip == HIGH) {
digitalWrite(purple_strip, LOW);
state_purple_strip = LOW;
}
else {
digitalWrite(purple_strip, HIGH);
state_purple_strip = HIGH;
}
if (state_white_strip == HIGH) {
digitalWrite(white_strip, LOW);
state_white_strip = LOW;
}
else {
digitalWrite(white_strip, HIGH);
state_white_strip = HIGH;
}
}
//A function that resets the lights before the light games
void reset_lights()
{
digitalWrite(purple_strip, HIGH);
digitalWrite(white_strip, LOW);
state_purple_strip = HIGH;
state_white_strip = LOW;
}
//The next two functions - toggle the light strips individually
void toggle_lights1() {
if (state_purple_strip == HIGH) {
digitalWrite(purple_strip, LOW);
state_purple_strip = LOW;
}
else {
digitalWrite(purple_strip, HIGH);
state_purple_strip = HIGH;
}
}
void toggle_lights2() {
if (state_white_strip == HIGH) {
digitalWrite(white_strip, LOW);
state_white_strip = LOW;
}
else {
digitalWrite(white_strip, HIGH);
state_white_strip = HIGH;
}
}
//This function is playing a Tone for a duration, a frequency
void playTone(int tones, int duration)
{
for (long i = 0; i < duration * 1000L; i += tones * 2)
{
digitalWrite(buzz, HIGH);
delayMicroseconds(tones);
digitalWrite(buzz, LOW);
delayMicroseconds(tones);
}
}
//This function will determine which note should be played and for how long time
void playNote(char note, int duration)
{
char names[] = { 'c', 'd', 'e', 'f', 's', 'g', 'a', 'v', 'b', 'C', 'D', 'E' };
int tones[] = { 1915, 1700, 1519, 1432, 1352, 1275, 1136, 1073, 1014, 956, 852, 758 };
for (int i = 0; i < 12; i++)
{
if (names[i] == note) playTone(tones[i], duration);
}
}
//Here is where the songs are played
void playSong(int song) {
reset_lights(); //before playing a song, the strips will reset their states
//and the RGB led will turn red (not accepting money - is busy)
analogWrite(A0,0);
analogWrite(A1,255);
analogWrite(A2, 255);
//here is made the selection of the song played
if (song == 3) {
for (int k = 0; k< length1; k++){
if (notes1[k] == ' ') delay(beats1[k] * tempo);
else playNote(notes1[k], beats1[k] * tempo);
toggle_lights();
delay(tempo / 2);
}
}
if (song == 1) {
for (int k = 0; k< length2; k++){
if (notes2[k] == ' ') delay(beats2[k] * tempo);
else playNote(notes2[k], beats2[k] * tempo);
toggle_lights();
delay(tempo / 2);
}
}
if (song == 2) {
for (int k = 0; k< length3; k++){
if (notes3[k] == ' ') delay(beats3[k] * tempo);
else playNote(notes3[k], beats3[k] * tempo);
toggle_lights();
delay(tempo / 2);
}
}
if (song == 0) {
for (int k = 0; k< length4; k++){
if (notes4[k] == ' ') delay(beats4[k] * tempo);
else playNote(notes4[k], beats4[k] * tempo);
toggle_lights();
delay(tempo / 2);
}
}
}
void setup(){
irrecv.enableIRIn(); // Starts the receiver
//and setup the pins of Arduino
pinMode(purple_strip, OUTPUT);
pinMode(white_strip, OUTPUT);
pinMode(buzz, OUTPUT);
pinMode(interruption_sens, INPUT);
Serial.begin(9600);
}
void loop(){
int x = random(1,100); //this variable will help in getting a random value for the tracks played
if (irrecv.decode(&results)){
Serial.println(results.value, HEX);
if (results.value == 1) //if the button 1 is pressed will try to play 1st song
{
if (credit == 1) { playSong(x%4); credit=0;}
if (credit >= 2) {playSong(0); credit = 0;}
}
if (results.value == 2) //if the button 2 is pressed will try to play 2nd song
{
if (credit == 1) { playSong(x%4); credit=0;}
if (credit >= 2) {playSong(1); credit = 0;}
}
if (results.value == 3) //if the button 3 is pressed will try to play 3rd song
{
if (credit == 1) { playSong(x%4); credit=0;}
if (credit >= 2) {playSong(2); credit = 0;}
}
if (results.value == 4) //if the button 4 is pressed will try to play 4th song
{
if (credit == 1) { playSong(x%4); credit=0;}
if (credit == 2) {playSong(3); credit = 0;}
}
if (results.value == 5) //if the button 5 is pressed, both strips will toggle
toggle_lights();
if (results.value == 6) //if the button 6 is pressed, one of the strips will toggle
toggle_lights1();
if (results.value == 7) //if the button 7 is pressed, the other strip will toggle
toggle_lights2();
irrecv.resume();
}
//we have to check if the money passed through the sensor
curent = digitalRead(interruption_sens);
if (curent == 1 && last == 0)
{
credit++; //when receiving money, the credit is increased
}
last = curent;
//if the credit is 0, it will not play anything, just the lights can be controlled
if (credit == 0) //credit = 0 => blue
{
analogWrite(A0,255);
analogWrite(A1, 0);
analogWrite(A2, 255);
}
//having 1 credit, you can play a track, but it will be a random one, not the desired one
if (credit == 1) //credit = 1 => yellow
{
analogWrite(A0,0);
analogWrite(A1, 255);
analogWrite(A2, 0);
}
//if you have more than 2 credit, you can play the track you want
//anything exceeding 2 credit will be lost once played the track
if (credit > 2) //credit = 2 => green
{
analogWrite(A0,255);
analogWrite(A1, 255);
analogWrite(A2, 0);
}
}
Comments
Please log in or sign up to comment.