I am using a Arduino Nano.
I am using Continous micro servos
I actually figured it out. Pin 10 wasn't soldered to the other servo. That is why that servo wasn't moving. Now it does work.
But when I ran the code the servos still do not go in opposite directions. They go in the same directions.
The reason to get these servos to move in opposite directions is because they are going to be facing towards each other.
And my real code I want to use has these sensors that are hit and after they are touched to much the servos move.
I managed to use that code with servos only using one digital pin.
The reason why I use 2 digital pins in this project is to tell the other servo to go the opposite direction while the 2 other servos move in the same direction.
Here is my code for using 1 digital pin:
Code: Select all
#include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
Adafruit_LiquidCrystal lcd(0);
Servo myservo;
const int knockSensor = A0;
const int threshold = 50;
int health=500;
int sensorReading = 0;
int pos;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
lcd.begin (20 ,4);
lcd.clear();
Serial.begin(9600);
myservo.attach(9);
myservo.write(0);
}
void loop()
{
do
{
myservo.detach();
sensorReading = analogRead(knockSensor);
lcd.setCursor(0, 1);
Serial.println(health);
lcd.println(health);
if (sensorReading > threshold) {
Serial.println("Knock!");
health=health-sensorReading;
Serial.print(sensorReading);
lcd.print(sensorReading);
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
} while(health>1);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
myservo.attach(9);
delay(15);
myservo.write(1);
delay(1000);
myservo.detach();
delay(6000);
myservo.attach(9);
delay(15);
myservo.write(179);
delay(1000);
myservo.detach();
delay(1000);
health=500;
}
I attempted to do the code for 2 servos having the same digital pin and the other servo having a different digital pin.
When I tried this the other servo motor did not move.
Here is that code ( I will have comments in slashes):
Code: Select all
#include <Servo.h>
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
Adafruit_LiquidCrystal lcd(0);
Servo myservo1;
Servo myservo2;//other servo for other digital pin
const int knockSensor = A0;
const int threshold = 50;
int health=500;
int sensorReading = 0;
int pos;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println("Nerf Target v0.0.1, github.com/mcoms/nerf-target, 2014.");
lcd.begin (20 ,4);
lcd.clear();
Serial.begin(9600);
myservo1.attach(9);
myservo1.write(0);
myservo2.attach(10);//connected other servo to pin 10
myservo2.write(180);
}
void loop()
{
do
{
myservo1.detach();
myservo2.detach();
sensorReading = analogRead(knockSensor);
lcd.setCursor(0, 1);
Serial.println(health);
lcd.println(health);
if (sensorReading > threshold) {
Serial.println("Knock!");
health=health-sensorReading;
Serial.print(sensorReading);
lcd.print(sensorReading);
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
} while(health>1);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
myservo1.attach(9);
myservo2.attach(10);
delay(15);
myservo1.write(1);
myservo2.write(1);
delay(1000);
myservo1.detach();
myservo2.detach();
delay(6000);
myservo1.attach(9);
myservo2.attach(10);
delay(15);
myservo1.write(179);
myservo2.write(0);//guessing the point should be opposite from the other so I chose 0
delay(1000);
myservo1.detach();
myservo2.detach();
delay(1000);
health=500;
}
With this code it works just like the other code, but the other servo motot that is supposed to be moving the opposite direction isnt turning at all.
Can you tell me how to fix this in my code?