To make the hand, I need to use a servo that is able to rotate 360°, so I’m using a parallax continuous rotation servo. After some quick reading I realized this has to be coded differently than the standard servo that I’m used to, with a few added lines of code. The servo can be made to move clockwise, counterclockwise, or be made to stop based on the right combination of values. A certain value can make it go one way, and a different value the other way, and as you move slowly from one value toward the other, the servo runs slower, then stops, then slowly gains speed in the other direction.
I need to make the servo respond to a distance sensor, where it will rotate continuously unless someone is in close range to the sensor. I found this helpful code here. I played around with the numbers and had to tweak the values slightly to make the servo come to a full stop. The first for loop makes it stop, the second makes it rotate right:
int servoPin = 7;
void setup(){
pinMode(servoPin,OUTPUT);}
void loop(){
int i;
for (i = 0; i <= 200; i++){
digitalWrite(servoPin,HIGH);
delayMicroseconds(1498); // 1.5ms
digitalWrite(servoPin,LOW);
delay(20); // 20ms}
for (i = 0; i <= 200; i++){
digitalWrite(servoPin,HIGH);
delayMicroseconds(1200); // 1.2ms
digitalWrite(servoPin,LOW);
delay(50); // 20ms}
}
I took this code and added a sensor that would make the servo rotate unless someone was standing in front of the sensor, which is when it would come to a full stop:
#include <Servo.h>
int servoPin = 7;
void setup() {
Serial.begin (9600); // (speed) sensor
pinMode(servoPin,OUTPUT);}
void loop() {
int distance = analogRead(0);
if (distance > 400) {
Serial.println(distance);
stopTap(); }
else {
tap(); }
}
void stopTap() {
digitalWrite(servoPin,HIGH);
delayMicroseconds(1498);
digitalWrite(servoPin,LOW);delay(20);
}
void tap() {
digitalWrite(servoPin,HIGH);
delayMicroseconds(1200);
digitalWrite(servoPin,LOW);
delay(50);
}