Hello all,
For this lab I decided to get a little creative and try to have two servo motors interact with eachother. In the past couple of years I have been getting more interested in the concept of kinetics. Even though this lab isn’t really the best example of kinetics, I thought it would be fun create something similar. I wanted to have two servos interact with eachother, creating a continuous loop. Each servo was triggering the other’s photosensor which in turn would trigger the others motor. Its a little complicated to explain, as it was also difficult to code. But I think it is easier to show rather than tell. Just click the link below.
VIDEOS! (click links below)
CODE:
int servoPin = 11; // Control pin for servo motor
int servoPin2 = 3; // Control pin for servo motor
int minPulse = 500; // Minimum servo position
int maxPulse = 2500; // Maximum servo position
int pulse = 0; // Amount to pulse the servo
int minPulse2 = 500; // Minimum servo position
int maxPulse2 = 2500; // Maximum servo position
int pulse2 = 0; // Amount to pulse the servo
long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
long lastPulse2 = 0; // the time in milliseconds of the last pulse
int refreshTime2 = 20; // the time needed in between pulses
int analogValue = 0; // the value returned from the analog sensor
int analogPin = 1; // the analog pin that the sensor’s on
int analogValue2 = 0; // the value returned from the analog sensor
int analogPin2 = 0; // the analog pin that the sensor’s on
int ct;
int ct2;
int servoValue;
int servoValue2;
void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pinMode(servoPin2, OUTPUT); // Set servo pin as an output pin
pulse = minPulse; // Set the motor position value to the minimum
pulse2 = minPulse; // Set the motor position value to the minimum
Serial.begin(9600);
}
void loop() {
analogValue = analogRead(analogPin); // read the analog input
servoValue= analogValue;
//pulse = map(servoValue,0,1023,minPulse,maxPulse); // convert the analog value
// to a range between minPulse
// and maxPulse.
analogValue2 = analogRead(analogPin2); // read the analog input
//pulse2 = map(analogValue2,0,1023,minPulse2,maxPulse2); // convert the analog value
// to a range between minPulse
// and maxPulse.
Serial.print(“pin 0: “);
Serial.print(analogValue2, DEC);
Serial.print(” pin1: “);
Serial.println(analogValue, DEC); // print the pot value back to the debugger pane
delay(10); // wait 10 milliseconds before the next loop
if (analogValue < 400) {
pulse= 500;
pulse2= 2500;
}
if (analogValue2 < 400) {
pulse2= 500;
pulse= 2500;
}
/* ct++;
if (ct > 40){
pulse = map(500,0,1023,minPulse,maxPulse);
if (ct > 60){
pulse = map(0,0,1023,minPulse,maxPulse);
if (ct > 70){
ct=0;
}
}*/
// }
/*if (analogValue2 > 100) {
ct2++;
if (ct2 < 30){
pulse2 = map(500,0,1023,minPulse,maxPulse);
if (ct2 < 60){
pulse2 = map(0,0,1023,minPulse,maxPulse);
if (ct2 < 70){
ct2=0;
}
}
}
}
*/
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() – lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
if (millis() – lastPulse2 >= refreshTime2) {
digitalWrite(servoPin2, HIGH); // Turn the motor on
delayMicroseconds(pulse2); // Length of the pulse sets the motor position
digitalWrite(servoPin2, LOW); // Turn the motor off
lastPulse2 = millis(); // save the time of the last pulse
}
}

