Hi,
While learning the Arduino I wrote a little program using arrays.
I have a button array, a LED array and a servo array. LED 0 follows Button 0, LED 1 follows Button 1 etc..
For some reason the servo’s don’t follow. There must be something wrong with my code. Can someone tell me what’s wrong?
#include <Servo.h>
int ButtonArray[] = {30,31,32,33};
int LedArray[]= {40,41,42,43};
int Number = 4;
Servo ServoArray[4];
int buttonStateArray[4];
int lastButtonStateArray[4];
void setup() {
int i;
for (i=0;i<Number;i++){
pinMode(ButtonArray[i], INPUT);
pinMode(LedArray[i], OUTPUT);
buttonStateArray[i] = 0;
lastButtonStateArray[i] = 0;
}
for (uint8_t i=0;i<Number;i++)
ServoArray[i].attach(22+i, 700, 2300);
}
void loop() {
int i;
for (i=0;i<Number;i++){
buttonStateArray[i] = digitalRead(ButtonArray[i]);
if (buttonStateArray[i] != lastButtonStateArray[i]) {
if (buttonStateArray[i] == HIGH) {
digitalWrite(LedArray[i], LOW);
ServoArray[i].writeMicroseconds(700);
}
else {
digitalWrite(LedArray[i], HIGH);
}
lastButtonStateArray[i] = buttonStateArray[i];
ServoArray[i].writeMicroseconds(2300);
}
}
}
Thanks,
Chris