Arduino servo sequence from button

Im looking to write some code and before i do im just looking to see if something is available before i take on this laborious task.

My intentions are:
im looking to hook up a button so that when the button is pushed (not held) it will execute a sequence to run servos 'until sequence is finished.
Then if the button is pushed again its to run another pre-defined sequence. pushed again it then runs the first sequence again. and so on…

any tips, code or examples will be greatly appreciated.

Jonny

Hi Jonny,

There is code in the Phoenix code base for running sequences. The code is probably a lot more complex than what you need, as we added stuff to it, to try to allow us to change the speed of the sequence (and direction) of the sequence while it was playing.

The code that actually talks to the SSC-32 is in the code Phoenix_Driver_SSC32.h The code for handling buttons being pressed, there are examples in the different input code classes (PS2, DIY remote, Commander).

Again this code could be simplified a lot, if you simply want to start a sequence, with maybe some other interlocking code that checks to make sure it is not in the middle of a sequence…

Kurt

Could declare a variable that change state and add some
arduino.cc/en/Reference/SwitchCase

Thanks for the pointers there.
i was in a previous code using the SwitchCase statement but i have just written another simpler version…
This version either determines whether the button is held or not.
but i need to allow the button to count an array of pushes so that the button gets pressed/debounce then runs a sequence, then the next time its pressed/debounced it runs a second sequence.?

[code]#include <Servo.h>

Servo servo1;
Servo servo2;

#define button 3 //attaches button to pin 3
int STORE = 0; //STORE is used to store state of input

//Initial Servo Position
int servo1_Pos = 90;
int servo2_Pos = 90;

void setup() {
servo1.attach(9);
servo2.attach(3);

pinMode(button, INPUT); //sets button as input

}

void loop() {
STORE = digitalRead(button); //reads state of button

if (STORE == HIGH) { //and button is pressed
servo1.write(160); //run this servo sequence
servo2.write(100);
delay(2000);
servo1.write(160);
servo2.write(80);
delay(4000);
servo1.write(160);
servo2.write(100);
delay(2000);
}
if (STORE == LOW) { //button is not pressed
servo1.write(90); //run this servo sequence
servo2.write(160);
//and so on…
}
}[/code]

You could always do it with functions, with an array of function pointers… Example:

[code]typedef void (*PFUNC) (void);

PFUNC apfuncs] = {&func1, &func2, &func1, &func3};
#define CNT_PFUNCS (sizeof(apfuncs)/sizeof(apfuncs[0]))
uint8_t ifunc = 0;

void setup()
{
Serial.begin(38400);
}

void loop()
{
(*apfuncs[ifunc])();
ifunc++;
if (ifunc >= CNT_PFUNCS)
ifunc = 0;
delay(1000);
}

void func1(void)
{
Serial.println(“Function1”);
}
void func2(void)
{
Serial.println(“Function2”);
}

void func3(void)
{
Serial.println(“Function3”);
}
[/code]

Does not do anything with buttons or servos, but allows you to simply add in pointers to different functions…

Thanks Kurte.
well im now using this and as far as i can tell its working. but thats a blind assumption as i dont acctually have the board powered at the moment. :slight_smile:
Such a dear devil i know. lol

[code]#include <Button.h>
#include <Servo.h>
//====================— I N N E R B R E E D F X —=========================
//Project; Animatronic hand for InnerbreedFX
//Description: Control for Jonny Poole - InnerbreedFX Animatronics
//Software version: V1.0
//Date: 30-09-2013
//Developer/s: Jonny Poole
//
//
//=============================================================================

Button button = Button(12,PULLDOWN);

//Servo Names
Servo Thumb1;
Servo Thumb2;
Servo Pointing_Finger;
Servo Index_Finger;
Servo RingAndPinky_Finger1;
Servo RingAndPinky_Finger2;
Servo Wrist;

//Initial Servo Position
int Thumb1_Pos = 90;
int Thumb2_Pos = 90;
int Index_Finger_Pos = 90;
int Pointing_Finger_Pos = 90;
int RingAndPinky_Finger2_Pos = 90;
int RingAndPinky_Finger1_Pos = 90;
int Wrist_Pos = 90;

void setup() {
//Servo Pin Connections
Thumb1.attach(9);
Thumb2.attach(3);
Pointing_Finger.attach(11);
Index_Finger.attach(10);
RingAndPinky_Finger1.attach(A1);
// FREE.attach(A2);
RingAndPinky_Finger2.attach(A3);
Wrist.attach(A0);
delay(1000);}

//— Main Loop —
byte pressCount = 0;

void loop()
{
if(button.uniquePress())
pressCount++;

switch(pressCount)
{
case 1:
// sequence one
break;
case 2:
// sequence two
break;
case 3:
// sequence three
pressCount = 0;
break;
}
}
[/code]

It does something… don’t know what … don’t know when… but… something… :stuck_out_tongue:

Eric, the code allows me to set predefined servo positions, and then by pushing the button, it selected each group of positions set.

ok well i managed to solve my previous requirements but rather than opening another thread i thought id post my next requirement to see if anyone has any detail.

Im looking to have a sequence that’s runs once when power is applied, but then doesn’t loop or get called again until the program is restarted from a power up?
Any info on how this can be achieved would be great. :wink:

LOL. its ok… I’ve figured that out too… :wink: