Advertisement
Guest User

FaceTracking.ino

a guest
Jun 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1.       #include <Servo.h>  //Used to control the Pan/Tilt Servos
  2.  
  3. //These are variables that hold the servo IDs.
  4. char tiltChannel=0, panChannel=1;
  5.  
  6. //These are the objects for each servo.
  7. Servo servoTilt, servoPan;
  8.  
  9. //This is a character that will hold data from the Serial port.
  10. char serialChar=0;
  11.  
  12. void setup(){
  13.   servoTilt.attach(2);  //The Tilt servo is attached to pin 2.
  14.   servoPan.attach(3);   //The Pan servo is attached to pin 3.
  15.   servoTilt.write(90);  //Initially put the servos both
  16.   servoPan.write(90);      //at 90 degress.
  17.  
  18.   Serial.begin(57600);  //Set up a serial connection for 57600 bps.
  19. }
  20.   void loop(){
  21.   while(Serial.available() <=0);  //Wait for a character on the serial port.
  22.   serialChar = Serial.read();     //Copy the character from the serial port to the variable
  23.   if(serialChar == tiltChannel){  //Check to see if the character is the servo ID for the tilt servo
  24.     while(Serial.available() <=0);  //Wait for the second command byte from the serial port.
  25.     servoTilt.write(Serial.read());  //Set the tilt servo position to the value of the second command byte received on the serial port
  26.   }
  27.   else if(serialChar == panChannel){ //Check to see if the initial serial character was the servo ID for the pan servo.
  28.     while(Serial.available() <= 0);  //Wait for the second command byte from the serial port.
  29.     servoPan.write(Serial.read());   //Set the pan servo position to the value of the second command byte received from the serial port.
  30.   }
  31.   //If the character is not the pan or tilt servo ID, it is ignored.
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement