Hello,
The code I have controls 5 servos (0-180 range) through 5 buttons.
It's for an art installation where mushrooms will have their eyes following people passing by them, triggering sensors along a sidewalk.
All servos start at 0 degrees, then when I push any button;
servo 1 goes to 90
servo 2 goes to 112
servo 3 goes to 134
servo 4 goes to 156
servo 5 goes to 178
Now, I want this for button 1. But for button 2, I would like to withdraw 22 from each value to get:
servo 1 goes to 68
servo 2 goes to 90
servo 3 goes to 112
servo 4 goes to 134
servo 5 goes to 156
then withdraw 22 again for button 3, 4 and 5.
I am over my head in this!!! I tried many different avenues but my programming skills are really not up to the task.
here's the code:
#include <Servo.h>
Servo myservos[5];
int buttonPin[] = {2, 3, 4, 5, 6};
int buttonState[] = {0, 0, 0, 0, 0};
int lastButtonState[] = {0, 0, 0, 0, 0};
int angles[] = {0, 0, 0, 0, 0};
int positions1[] = {90, 112, 134, 156, 178};
void setup() {
int x = 11;
int serv;
for (serv=0;serv<5;serv++){
myservos[serv].attach(x); x--; //subtract 1 from x every time
}
for (int buttonIn = 0; buttonIn < 5; buttonIn++) {
pinMode (buttonPin[buttonIn], INPUT);
}
}
void moveButton(int cycle){
int subrunner;
for(int subrunner=4; subrunner>=0; subrunner--){
buttonState[cycle] = digitalRead(buttonPin[cycle]);
if (buttonState[cycle] != lastButtonState[cycle]){
if (buttonState[cycle] == 1){
for(angles[subrunner] = 90; angles[subrunner] <= positions1[subrunner]; angles[subrunner] += 1){
myservos[subrunner].write(angles[subrunner]);
}
delay(5);
}
else{
for(angles[subrunner] = positions1[subrunner]; angles[subrunner] >= 0; angles[subrunner] -= 1){
myservos[subrunner].write(angles[subrunner]);
}
delay(5);
}
}
}
lastButtonState[cycle] = buttonState[cycle];
delay(20);
}
void loop(){
int runner;
for (int runner = 4; runner >= 0; runner--){
moveButton(runner);
}
}
I have tried to create an array with a series of increments of 22
(0,22,44,66,88) and then assign these values to a substraction that would've been performed on each pass of the moveButton function. But I realize I have to first tell the program that I want to do this for each particular buttons. But how?
My brain sees many possibilities like the one cited above, but I don't know enough language to try them out...
If anyone understands this... maybe...