Hello all. I will try to keep this concise.
There was a project I was working on that involved cannibalizing another product for my needs. My friend said I could do more, and better building it myself. He bought me an arduino kit to begin practicing. I've never done electronics or any programming before so this is all new to me and I've finally hit a wall that I haven't been able to get over.
I've tried finding various examples online etc and can't seem to find anything that applies to my specific circumstances that is solving my issue.
The goal:
- Have two servos run a loop going from point a, to b, to c with a delay between each move.
- Have a button I can press that will interrupt the a/b/c/ loop, move the servos to a specific point and hold them there until the button is released whereby they go back to the a/b/c loop.
- Set up 3 seperate IR range/motion sensors that will automatically interrupt the a/b/c loop and again move the servos to a specific point and hold them there until they are no longer receiving input.
I built the loop to make the servos follow the a/b/c loop (though the delay is shorter than in the final version and one of the steps is blocked out for testing purposes).
I have the button coding in and it works... sort of. When I press the button it will instantly move both servos to 90 degrees, however if the a/b/c loop is on the first step, it will finish the loop moving to the second step (and obviously the third if it wasn't blocked out) where it will then return to 90 degrees and hold it until I release the button where it will return to the a/b/c cycle.
No matter how I change the interrupt and coding it always behaves that way. I can't seem to figure out how to get it to just 'stop' the a/b/c loops, move the servos to 90 degrees and hold them there until I release the button without it finishing the whole a/b/c loop.
I'm assuming I will run into the same issue once I get the ir's into the code as well.
Any brighter minds out there see what is wrong with my code?
This is the most current version I'm working on:
#include <Servo.h>
const int buttonPin = 2; // pushbutton pin
Servo servoL; // create servo object
Servo servoR; // create servo object
int buttonState = 0; // variable for reading the pushbutton states
int eardly = 1000; // use for setting delay
volatile int pos;
volatile int state = LOW;
void setup() {
Serial.begin(9600);
Serial.println("DualServo_Button_Interrupt4_break.ino");
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
pinMode(0, INPUT_PULLUP);
servoL.attach(6); // define the servo pin
servoR.attach(9); // define the servo pin
attachInterrupt(0, button, FALLING);
}
void loop()
{
buttonpress();
}
void button()
{
servoL.write(90); // tell left servo to turn to 90 degrees
servoR.write(90); // tell right servo to turn to 90 degrees
}
void earauto(){
servoR.write(0); // tell right servo to turn to 0 degrees
servoL.write(270); // tell left servo to turn to 270 degrees
delay (eardly); // delay x seconds
servoR.write(270); // tell right servo to turn to 270 degrees
servoL.write(0); // tell left servo to turn to 0 degrees
delay (eardly); // delay x seconds
// servoR.write(90); // tell right servo to turn to 90 degrees
// servoL.write(90); // tell left servo to turn to 90 degrees
// delay(eardly); // delay x seconds
}
void buttonpress(){
buttonState = digitalRead(buttonPin); // read button state
while (buttonState == HIGH) // button state is up
{
earauto(); // servo loop for auto-movement
break;
}
buttonState = digitalRead(buttonPin); // read button state
if (buttonState == LOW)
{
servoL.write(90); // tell left servo to turn to 90 degrees
servoR.write(90); // tell right servo to turn to 90 degrees
}
}
Any advice or feedback would be greatly appreciated.
Thank you.