//Rockband Iphone playing Robot //blog.ohbowz.com #include Servo myservo[4]; // create servo object to control a servo // a maximum of eight servo objects can be created int x; int y; int pos = 0; // variable to store the servo position int bufferSize = 50 ; //How many cycles to buffer guitar notes int bufferArray[51] [6] ; //Array to store note hits int outputDelay; //Variable for adding delay to output int bufferCycle = 0; //for cycling through the rows in the multidimensional array int delayCycle = 0; //same as the bufferCycle, but lagged behind. This is for the delay from when notes are read, until they reach the bottom of the screen boolean delayTrigger = false; long delayTimer; long delayInterval = 120; //How long to wait between a note being read, and played void setup() { myservo[0].attach(12); // attaches the servo on pin 12 to the servo object myservo[0].write(140); myservo[1].attach(9); // attaches the servo on pin 9 to the servo object myservo[1].write(50); myservo[2].attach(10); // attaches the servo on pin 10 to the servo object myservo[2].write(150); myservo[3].attach(11); // attaches the servo on pin 11 to the servo object myservo[3].write(130); Serial.begin(57600); // sets the serial port to 57600 } void loop() { ////////////////////////////////////////// //////Input from light sensors ///////////////////////////////////////// for(x=bufferCycle; x<(bufferCycle+1); x++) { for(y=0; y<4; y++) { bufferArray[bufferCycle] [y] = sensorInput(y); //get value of notes using sensorInput function } if(delayTrigger = false) { delayTimer = millis(); //set the timer } } ////wait for delay to be over without inturrupting the loop if ((millis() - delayTimer) > delayInterval) { delayTrigger = true; //Start playing the notes } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(delayTrigger) { ////Set outputs to HIGH or LOW for(y=0; y<4; y++) { if (bufferArray[delayCycle] [y] == 1 ) //If a note is seen { if(y==0) { myservo[0].write(120); } if(y==1) { myservo[1].write(90); } if(y==2) { myservo[2].write(120); } if(y==3) { myservo[3].write(150); } } if (bufferArray[delayCycle] [y] == 0 ) //If the note on the screen is gone { if(y==0) { myservo[0].write(140); } if(y==1) { myservo[1].write(50); } if(y==2) { myservo[2].write(155); } if(y==3) { myservo[3].write(130); } } } ///////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// delayCycle ++; //add to delayCycle if(delayCycle >= bufferSize) //if the delayCycle reaches the end of the array reset { delayCycle = 1; } } bufferCycle ++; //add to bufferCycle if(bufferCycle >= bufferSize) //if the bufferCycle has reached the end of the array reset { bufferCycle = 1; } } ////////////////////////////////////////////////////////////////////////////////////////////////////// //function for reading Notes on screen int sensorInput(int sensorPin) { long input[31]; //Raw input value of sensor long average; //Average int digital; //The absolute value of the sensor data, 1 or 0 int i; long sum; long inputx = 30 ; //Amount of samples to take from the input pin i=0; sum=0; for(i=0; i= 15)//note is on screen { digital = 1; } if (average <= 10)//no notes are seen { digital = 0; } return digital; }