Neal Cole- Servo Motors
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- animefruit
- Posts: 332
- Joined: Tue Feb 25, 2020 1:04 pm
Neal Cole- Servo Motors
This is Neal Cole. I am trying to get 2 servo motors to turn the same direction when they sweep. When I put them side by side, facing towards each other, they turn opposite directions. What code would I use for them to sweep different directions so that when they face towards each other they sweep the same direction? If you could answer that would be great.
- animefruit
- Posts: 332
- Joined: Tue Feb 25, 2020 1:04 pm
Re: Neal Cole- Servo Motors
Here is a picture of how I have my servo motors facing:
- Attachments
-
- 819E88E0-A88C-4FBB-B3E1-7FBC22BE15CF.jpeg (334.96 KiB) Viewed 139 times
- Franklin97355
- Posts: 23912
- Joined: Mon Apr 21, 2008 2:33 pm
Re: Neal Cole- Servo Motors
What code are you using to sweep the servos? You need to reverse the code to make them rotate opposite
- animefruit
- Posts: 332
- Joined: Tue Feb 25, 2020 1:04 pm
Re: Neal Cole- Servo Motors
Here is my code:
The bottom part is my main code for the servo when it says:
How should I reverse this code?
Code: Select all
#include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);
Servo myservo;
const int knockSensor = A0;
//const int led = 13;
const int threshold = 10;
int health=500; // added this
int sensorReading = 0;
//int reading;
//int reading2;
int pos;
//const unsigned long eventInterval = 4000;
//unsigned long previousTime = 0;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
//pinMode(led, OUTPUT);
//digitalWrite(led, HIGH);
Serial.begin(9600);
Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
// lcd.begin (16 ,2);
lcd.begin (20 ,4);
lcd.clear();
// put your setup code here, to run once:
Serial.begin(9600);
myservo.attach(9); //servo at digital pin 9
myservo.write(0); //initial point for servo
//myservo2.attach(11);
//myservo2.write(0);
}
void loop()
{
do
{
// unsigned long currentTime = millis();
myservo.detach();
//myservo2.detach();
//-+delay(50);
//reading=analogRead(A0);
//reading = analogRead(A0); //attached to analog 0
//reading2=analogRead(A1);
//lcd.setCursor(0, 1);
sensorReading = analogRead(knockSensor);
//if(sensorReading < threshold){ //was 900
//reading=0;
//}
lcd.setCursor(0, 1);
Serial.println(health);
lcd.println(health);
if (sensorReading > threshold) {
//String packet = String("NP,") + millis() + String(',') + sensorReading;
//Serial.println(packet);
//digitalWrite(led, LOW);
Serial.println("Knock!");
health=health-sensorReading;
//health=health-reading-reading2;
Serial.print(sensorReading);
lcd.print(sensorReading);
//Serial.print("Sensor Value=");
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1);
}
//delay(100);
// if(reading2 < 800){
//reading2=0;
//}
/*if(reading > 0){ //was 900
Serial.println(sensorReading);
//delay(100);
lcd.println(sensorReading);
//if (currentTime - previousTime >= eventInterval) {
// reading>=280;
}
//if(reading2 > 800){
// Serial.println(reading2);
// lcd.println(reading2);
*/
//}
/*
Serial.print("Sensor Value=");
health=health-sensorReading;
//health=health-reading-reading2;
lcd.setCursor(0, 1);
Serial.println(health);
lcd.println(health);
*/
} while(health>1);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
myservo.attach(9);
//myservo2.attach(11);
// attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(1);
// myservo2.write(1);
// sets the servo position according to the scaled value
delay(1000);
// waits for it to get to the position
myservo.detach();
// myservo2.detach();
delay(6000);//how long it pauses
myservo.attach(9);
//myservo2.attach(11);
// attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(179);
//myservo2.write(179);
// sets the servo position according to the scaled value
delay(1000); // waits for it to get to the position
myservo.detach();
//myservo2.detach();
delay(1000);
health=500;
}
The bottom part is my main code for the servo when it says:
Code: Select all
} while(health>1);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
myservo.attach(9);
//myservo2.attach(11);
// attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(1);
// myservo2.write(1);
// sets the servo position according to the scaled value
delay(1000);
// waits for it to get to the position
myservo.detach();
// myservo2.detach();
delay(6000);//how long it pauses
myservo.attach(9);
//myservo2.attach(11);
// attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(179);
//myservo2.write(179);
// sets the servo position according to the scaled value
delay(1000); // waits for it to get to the position
myservo.detach();
//myservo2.detach();
delay(1000);
health=500;
- adafruit_support_carter
- Posts: 29168
- Joined: Tue Nov 29, 2016 2:45 pm
Re: Neal Cole- Servo Motors
There's no "reverse" setting or anything like that. You'll have to do it with math. Something like:
But the angles and synchronization won't be perfect between the two servos. Hobby servos just aren't that precise. So you may need to do some additional tweaking.
Code: Select all
myservo1.write(angle)
myservo2.write(180-angle)
Please be positive and constructive with your questions and comments.