Here is the source code:
#include <Servo.h> Servo zServo; //rotates about z Servo xServo; //rotates about x float zPos = 90; //keeps track of position. 90 degrees is resting. float xPos = 90; //keeps track of position. 90 degrees is resting. int milisecondsBetween = 20; int vMax = 300; int sensorPin = 0; // select the input pin for the potentiometer int sensorValue = 0; // variable to store the value coming from the sensor void setup() { zServo.attach(13); // attaches zServo object to pin 13 xServo.attach(12); // attaches xServo object to pin 12 Serial.begin(19200); } void loop() { goLeft(); goRight(); } void goLeft(){ float z = zPos; sensorValue = analogRead(sensorPin); while(sensorValue < vMax && z > 50){ z -= 10; moveTo(xPos, z); sensorValue = analogRead(sensorPin); } if(sensorValue >= vMax){ sayHi(); printV(sensorValue); } } void goRight(){ float z = zPos; sensorValue = analogRead(sensorPin); while(sensorValue < vMax && z < 130){ z += 10; moveTo(xPos, z); sensorValue = analogRead(sensorPin); } if(sensorValue >= vMax){ sayHi(); printV(sensorValue); } } void printV(int v){ Serial.print("v: "); Serial.print(v); Serial.print("\r\n"); } void sayHi(){ moveTo(90, zPos); moveTo(110, zPos); moveTo(90, zPos); } void moveTo(float x, float z){ while(xPos != x|| zPos != z){ if(xPos > x){ xPos -= 1; } else if(xPos < x){ xPos += 1; } if(zPos > z){ zPos -= 1; } else if(zPos < z){ zPos += 1; } zServo.write(zPos); xServo.write(xPos); delay(milisecondsBetween); } }
No comments:
Post a Comment