I am working on an animatronic project that will have around 10 switches (buttons) and, eventually, 3 Parallax PIR sensors as inputs. I have all the sensors hooked to the Arduino Mega 2560 which then sends commands to the Mini Maestro. Currently, my program allows me to press any of these buttons and a series of commands and delays are sent to the Meastro to perform an animation with the servos (Currently there are 10 but there will be 20 servos).
The problem is that I would like for the buttons to interrupt an animation that is currently running to start a new animation instead of having to wait for the current animation to end.
Also, is it possible to perform a command in the background that is always running (breathing). I have not tried this but is there a way to run internal scripting in the Maestro while also receiving serial commands? I have read a little about interrupts but there are a limited number of interrupt ports. However, this may still be the way to go.
I am looking for a direction to head with re-structuring my logic in the code so I do not waste time wondering about. Are interrupts the best way to go about this?
What is 'Mini Maestro'? How is your system working currently? It's not clear what functions the Arduino performs. It can certainly do the sort of thing you're describing if the control logic runs on the Arduino, but I have no idea whether it currently does.
PeterH:
What is 'Mini Maestro'? How is your system working currently? It's not clear what functions the Arduino performs. It can certainly do the sort of thing you're describing if the control logic runs on the Arduino, but I have no idea whether it currently does.
I would imagine if the OP is using the scriptting language interface, that in that script, you would have to have the ability of a signal to kill the current program.
If the OP is controlling the mini maestro from the Arduino, it likely has hard coded delays in the code. The OP will then need to recode his/her program so that it doesn't have delays, but instead maintains a work queue of when to turn things on or off using a state machine. The blink without delay example shows how to do this, but I tend to think that blink without delay probably doesn't help people getting to the next step of using a time based state machine.
MichaelMeissner:
If the OP is controlling the mini maestro from the Arduino, it likely has hard coded delays in the code. The OP will then need to recode his/her program so that it doesn't have delays, but instead maintains a work queue of when to turn things on or off using a state machine. The blink without delay example shows how to do this, but I tend to think that blink without delay probably doesn't help people getting to the next step of using a time based state machine.
That does seem to be what the OP is doing, and the approach you're suggesting seems sensible.
PeterH:
What is 'Mini Maestro'? How is your system working currently? It's not clear what functions the Arduino performs. It can certainly do the sort of thing you're describing if the control logic runs on the Arduino, but I have no idea whether it currently does.
I would imagine if the OP is using the scriptting language interface, that in that script, you would have to have the ability of a signal to kill the current program.
If the OP is controlling the mini maestro from the Arduino, it likely has hard coded delays in the code. The OP will then need to recode his/her program so that it doesn't have delays, but instead maintains a work queue of when to turn things on or off using a state machine. The blink without delay example shows how to do this, but I tend to think that blink without delay probably doesn't help people getting to the next step of using a time based state machine.
Yes, I am sending serial commands to the Maestro from the Arduino. Here is a simplified example of what I am doing:
int button1 = 22; //the pin where the first switch is connected
int button2 = 23;
int button3 = 24;
int button4 = 25;
int button5 = 26;
void setup() {
//initialize the button pin as an input
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
// initialize serial communication:
Serial1.begin(9600);
}
void loop() {
//read the pushbotton input pin
digitalRead(button1);
digitalRead(button2);
digitalRead(button3);
digitalRead(button4);
digitalRead(button5);
if (button1 == HIGH) {
//if the current state is HIGH then the button went from off to on
Serial1.println("on");
acceleration_headlift();
headlift_minimum();
delay(1000);
headlift_maximum();
delay(5000);
headlift_minimum();
delay(1000);
}
else {
}
}
The headlift and acceleration commands are just a set of numbers sent to the Meastro using Serial.write using voids... I looked at the blink without delay led example and I see how it can take the place of delays. Will this method allow for me to change the time between the functions that different buttons command? Is a time based state machine the same as a finite state machine? I will have to read more on the state machines over the weekend but they sound interesting.
Basically what is happening is I have a creature that looks around until he is "poked" (button is pressed) on the left or right side and he looks that direction. So the looking around needs to be interrupted when poked and the poked needs to be interrupted if poked in another location.