// Generated by Proteus Visual Designer for Arduino
// Peripheral Configuration Code (Do Not Edit)
//---CONFIG_BEGIN---
#include <Servo.h>
#include <Stepper.h>
#include <IRremote.h>
#define motorpin1 10 //digotal pins used for the stepper motor
#define motorpin2 11
#define motorpin3 12
#define motorpin4 13
#define delaytime 90
Stepper stepper(4,motorpin1,motorpin2,motorpin3,motorpin4); //instantiate stepper motor object
IRrecv irrecv(A1); //instantiate a IR receiver object
//decode_results results; //instantiate a decode_results object.
int btn_value = 0;
Servo myservo[8] = {Servo(), Servo(), Servo(), Servo(), Servo(), Servo(), Servo(), Servo()}; //array of servo objects
//row 1 is servo 1, row 2 servo 2, etc...
//1=1/8,2=1/4,3=3/8(dotted 1/4),4=1/2
byte song1[2][104] ={{0x01,0x20,0x10,0x08,0x01,0x01,0x01,0x01,0x20,0x10,0x08,0x02,0,0x02,0x40,0x20,0x10,0x04,0,0x80,0x80,0x40,0x10,0x20,0,0x01,0x20,0x10,0x08,0x01,0,0x01,0,0,0,0x01,0x01,0x01,0x40,0x20,0x10,0x80,0x80,0x80,0x80,0x80,0x80,0x40,0x10,0x08,0x80,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x80,0x08,0x10,0x20,0,0x40,0x40,0x40,0x40,0x40,0x20,0x20,0x20,0x20,0x20,0x10,0x10,0x20,0x10,0x80,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x80,0x08,0x10,0x20,0,0x40,0x40,0x40,0x40,0x40,0x20,0x20,0x20,0x20,0x80,0x80,0x40,0x10,0x08}, //song1 = Jingle Bells, right hand melody.
/* {1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0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[2][36] = {{0x02,0x02,0x03,0x10,0,0x80,0x80,0x40,0x20,0,0x01,0x04,0x04,0x08,0x80,0x20,0x80,0x20,0x10,0,0x08,0,0x04,0,0x40,0x80,0x40,0x20,0x20,0x10,0x04,0x10,0x04,0,0x04,0x02}, //song3 = Lucky by Jason Mraz, right hand melody.
/* {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //finger 1 starts on middle c.
{0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},*/
{1,1,1,3,1,1,1,1,4,1,1,1,1,1,4,1,1,1,2,1,2,1,2,0,3,1,1,1,1,2,3,1,3,1,1,1}};
int song1steps[4] = {220, 250, 300 ,300}; //amount of steps to move stepper motor.
int song2steps[4] = {150, 250, 425 ,60};
int song3steps[4] = {100, 350, 300 ,300};
byte flag =1; //intialize flag variable used for delay timing later
byte m = 0; //intialize counter variable m for later use
// Flowchart Routines
//void chart_SETUP() {cpu.pinMode(13,OUTPUT);}
//void chart_LOOP() {cpu.digitalWrite(13,false);delay(1000);cpu.digitalWrite(13,true);delay(1000);}
// Entry Points and Interrupt Handlers
//void setup () { peripheral_setup(); chart_SETUP(); }
//void loop () { peripheral_loop(); chart_LOOP(); }
void setup() {
Serial.begin(9600); //begin serial communication
for (int ii=0; ii < 8; ii++) { //attach all the servos. uses digital pins 2-9
myservo[ii].attach(2+ii);
}
for (int k = 0; k < 8; k++) { //reset all servos
myservo[k].write(107);
delay(15);
}
irrecv.enableIRIn(); //start the ir receiver
pinMode(motorpin1, OUTPUT); //pins used for the stpper motor
pinMode(motorpin2, OUTPUT);
pinMode(motorpin3, OUTPUT);
pinMode(motorpin4, OUTPUT);
stepper.setSpeed(4000); //sets stpper speed. found experimentally to work
}
/*==============================================*/
void loop() {
//if (irrecv.decode(&results))
if (irrecv.decode())
{
// Serial.println(results.value);
btn_value = irrecv.decodedIRData.command;
Serial.println(btn_value);
//*****************************************************
//if (results.value == 16724175) { //#1 on remote
if (btn_value == 48) { //#1 on remote
for (int i = 0; i < 105; i++) { //for loops cycle through the song array to find the values of the notes
for (int j=0; j < 8; j++) { //for (int j=0; j < 9; j++) {
if ((song1[0][i]|| 0x01 << j)== 1) {
//code to play the notes. different angles because of the different finger heights.
//if (song1[0][i] == 1) {
//if (j == 0 || j == 2 || j == 4 || j == 6) { //lower arm
if (!(j & 0x01)) { //lower arm
myservo[j].write(55);
}
//if (j == 1 || j == 3 || j == 5 || j == 7) { //Upper arm
else{
myservo[j].write(35);
}
}
}
//code to delay to get proper note timing. adjust according to the tempo of the song.
//if (j == 1) { //8
delay(song1[1][i]*150); // delay(song1[j][i]*150);
//}
//a zero in the last row indicates that the stepper has to move
// if (song1[1][i] == 0) {
//move stepper
// }
for (int k = 0; k < 8; k++) { //reset all servos. 107 value found experimentally
myservo[k].write(107);
}
delay(90);
}
}
//*****************************************************
//if (results.value == 16718055) { //#2 on remote
if (btn_value == 24) { //#2 on remote
for (int i = 0; i < 12; i++) { //for loops cycle through the song array to find the values of the notes
for (int j=0; j < 8; j++) { //for (int j=0; j < 9; j++) {
if ((song2[0][i]|| 0x01 << j)== 1) {
//code to play the notes. different angles because of the different finger heights.
//if (song2[0][i] == 1) {
//if (j == 0 || j == 2 || j == 4 || j == 6) { //lower arm
if (!(j & 0x01)) { //lower arm
myservo[j].write(55);
}
//if (j == 1 || j == 3 || j == 5 || j == 7) { //Upper arm
else{
myservo[j].write(35);
}
}
}
//code to delay to get proper note timing. adjust according to the tempo of the song.
//if (j == 1) { //8
delay(song2[1][i]*100); // delay(song1[j][i]*150);
//}
//a zero in the last row indicates that the stepper has to move
// if (song2[1][i] == 0) {
//move stepper
// }
for (int k = 0; k < 8; k++) { //reset all servos
myservo[k].write(107);
}
delay(90);
}
}
//*****************************************************************
//if (results.value == 16743045) { //#3 on remote
if (btn_value == 122) { //#3 on remote
for (int i = 0; i < 37; i++) { //for loops cycle through the song array to find the values of the notes
for (int j=0; j < 8; j++) { //for (int j=0; j < 9; j++) {
if ((song3[0][i]|| 0x01 << j)== 1) {
//code to play the notes. different angles because of the different finger heights.
//if (song3[0][i] == 1) {
//if (j == 0 || j == 2 || j == 4 || j == 6) { //lower arm
if (!(j & 0x01)) { //lower arm
myservo[j].write(55);
}
//if (j == 1 || j == 3 || j == 5 || j == 7) { //Upper arm
else{
myservo[j].write(35);
}
}
}
//code to delay to get proper note timing. adjust according to the tempo of the song.
//if (j == 1) { //8
delay(song3[1][i]*350); // delay(song1[j][i]*150);
//}
//a zero in the last row indicates that the stepper has to move
// if (song3[1][i] == 0) {
//move stepper
for (int k = 0; k < 8; k++) { //reset all servos
myservo[k].write(107);
}
delay(90);
}
}
//*****************************************************************
irrecv.resume();
}
}