User Tools

Site Tools


robotservosensor

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

robotservosensor [2014/02/08 23:47] (current)
djacobs created
Line 1: Line 1:
 +The rough code program for the single [[https://​www.sparkfun.com/​products/​10333|servo]] and the [[https://​www.sparkfun.com/​products/​242|infrared proximity sensor]].
  
 +<code c>
 +#include <​Servo.h>​
 +
 +Servo myServo;
 +//Servo RearServo;
 +const unsigned int BAUD_RATE = 9600;
 +int delayTime = 5000;
 +int sensorPin = 0; //analog pin 0
 +int pos = 0;
 +
 +void setup() {
 +  myServo.attach(9);​
 +  //​RearServo.attach(3);​
 +  Serial.begin(BAUD_RATE);​
 +}
 +
 +void loop() {
 +  int val = analogRead(sensorPin);​
 +  Serial.println(val);​
 +  delay(delayTime);​ //If set too low (fast) the Arduino freezes
 +  ​
 +  for(pos = 0; pos < 162; pos += 1)  // goes from 0 degrees to "​180"​ degrees
 +  {                                  // in steps of 1 degree
 +    myServo.write(pos); ​             ​
 +    delay(15); ​                      // waits 15ms for the servo to reach the position
 +  }
 +  for(pos = 162; pos>=1; pos-=1) ​    // goes from "​180"​ degrees to 0 degrees
 +                                     // Set to 162 degrees because of a funny issue with the servo
 +  {                                ​
 +    myServo.write(pos); ​             ​
 +    delay(15); ​                      // waits 15ms for the servo to reach the position
 +  }  ​
 +  ​
 +  ​
 +  if (Serial.available() > 0) {
 +    int command = Serial.read();​
 +    if (command == '​r'​) { // "​Try"​ to reset the servo position if it gets out of whack
 +      myServo.write(0);​
 +      delay(10);
 +      Serial.println("​Reset"​);​
 +
 +    } else if (command == '​1'​) {
 +        myServo.write(81);​
 +        Serial.println("​Turn 90 degrees"​);​
 +    ​
 +    } else if (command == '​2'​) {
 +        myServo.write(152);​
 +        Serial.println("​Turn 155 degrees"​);​
 +    ​
 +    } else if (command == '​3'​) {
 +        Serial.println(val);​
 +    }
 +  }
 +}
 +</​code>​
robotservosensor.txt ยท Last modified: 2014/02/08 23:47 by djacobs