I have a Mega2560 with a HC-SR04 Ping sensor with a few servos. I have set up so that if the ping is under a curtain distance, the servo activates. Unfortunately, I have other servos that are not controlled by the Ping sensor and I believe that they are running on the same interrupt. This is making the servos "jump" around. I am not %100 sure that this is an interrupt conflict but I cannot find anything else that is causing the servos to jump.
The other servos are being controlled by a joystick. The joystick isn't permanent because I will eventually have them operating with face recognition. I am just doing one step at a time..
The project is a robotic "head" with eyes and eyelids being controlled by servos. The Ping sensor is for the eyelids. If an object gets to close to the face, the eyelids close.
I do not know much about interrupts and I am currently researching this, but figured if anyone would be willing to help out in anyway, it would be greatful.
I am attaching the libraries.
#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 9 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
unsigned long pingTimer; // Holds the next ping time.
Servo righteyelid;
Servo lefteyelid;
Servo EyeUpDown;
Servo EyeLeftRight;
int potpin = A1; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int potpin2 = A5; // analog pin used to connect the potentiometer
int val2; // variable to read the value from the analog pin
void setup() {
Serial.begin (115200);
EyeUpDown.attach(46);
EyeLeftRight.attach(48);
righteyelid.attach(50); // attaches servo on Right Eye Lid
lefteyelid.attach(52); // attaches servo on Left Eye Lid
}
void loop() {
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if (uS / US_ROUNDTRIP_CM <= 8 && uS / US_ROUNDTRIP_CM > 0) //if distance is less than 8 cm, close eye lids
{
righteyelid.write(60); //Closed postion of Right Eye
lefteyelid.write(135); //Closed position of Left Eye
}
else
{
righteyelid.write(95); //open postion of Right Eye
lefteyelid.write(96); //open postion of Left Eye
}
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 40, 170); // scale it to use it with the servo (value between 0 and 180)
EyeUpDown.write(val); // sets the servo position according to the scaled value
val2 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 40, 150); // scale it to use it with the servo (value between 0 and 180)
EyeLeftRight.write(val2); // sets the servo position according to the scaled value
}
}