// Must include for servos to work #include // Elements is the amount of numbers you want to dedicate to the running average // Increase the number for a slower reaction #define elements 5 // Variables for loops int i = 0; int j = 0; // The Analog Pins - C:Centre R:Right L:Left U:Up D:Down int pinC = 0; int pinR = 1; int pinL = 2; int pinU = 3; int pinD = 4; // Variables to store the data from the photo-resisitors int analogValueC; int analogValueL; int analogValueR; int analogValueU; int analogValueD; // The change in position from the last reading float posX = 0; float posY = 0; // The running average readings // - Each element is made up of the difference between opposite photorestors int x[elements], y[elements]; // Servos - X is rotation/spin, Y is the tilt servo Servo servoX; Servo servoY; // Common servo setup values int minPulse = 600; // minimum servo position, us (microseconds) int maxPulse = 2400; // maximum servo position, us void setup() { // Turn on the pins, program doesn't work without it *shrug* pinMode(10, OUTPUT); pinMode(11, OUTPUT); digitalWrite(10, HIGH); digitalWrite(11, HIGH); delay(200); // Attach each Servo object to a digital pwm pin servoY.attach(10, minPulse, maxPulse); servoX.attach(11, minPulse, maxPulse); delay(200); // Sanity check! servoX.write(90); servoY.write(90); // Start all the running average values to zero for(i=0;i "); Serial.println(analogValueR); Serial.println(" \\/"); Serial.print(" "); Serial.println(analogValueD); Serial.println(); Serial.println(); } // Class will display the change in position void avgDisplay(){ Serial.print(" "); if(posY>0){ Serial.println(posY); Serial.println(servoY.read()); } else { Serial.println(" "); } Serial.println(" /\\"); if(posX>0){ Serial.print(posX); Serial.println(servoX.read()); } else { Serial.print(" "); } Serial.print(" <= "); if((posX==0)&&(posY==0)){ Serial.print("C"); } else { Serial.print(" - "); } Serial.print(" => "); if(posX<0){ Serial.println(-posX); Serial.println(servoX.read()); } else { Serial.println(" "); } Serial.println(" \\/"); Serial.print(" "); if(posY<0){ Serial.println(-posY); Serial.println(servoY.read()); } else { Serial.println(" "); } Serial.println(); Serial.println(); } // Named so, because I am planning on making a more complicated verson void simpleChangePos(){ // Variables to store the current position of the servos int readX = servoX.read(); int readY = servoY.read(); // If there is a change: if(posX!=0){ // If the servo is going to change position past its range of motion if((readX+posX)>180){ if(readX!=180){ servoX.write(180); } } else if((readX+posX)<0){ if(readX!=0){ servoX.write(0); } } else { // If the change is a non-zero and not past the servo's limit, change the position servoX.write((readX+posX)); } delay(15); } if(posY!=0){ if((readY+posY)>180){ if(readY!=180){ servoY.write(180); } } else if((readY+posY)<0){ if(readY!=0){ servoY.write(0); } } else { servoY.write((readY+posY)); } delay(15); } }