I’m pretty new to hardware and I’m struggling to use two servos in the same application with the spark core. They don’t necessarily need to move simultaneously, but they do need to be triggered from the same web event.
I’m not sure what the exact problem is, but I’m suspecting it has something to do with power consumption. If I comment out the code for one servo, the other one works perfectly. If I leave code for both servos, they behave erratically (move back and forth randomly together).
Is there anything wrong with the below code? I’ve added delay
calls to pause the program to allow power to be distributed when needed. Each servo receives power from the 3v3 pin on the core.
Thanks for any help you can provide
Servo FLOOR_SELECTOR;
Servo BELL;
int pins[] = {D0, D1, D2, D3, D4, D5};
// Pass in params like 1,100
int updateState(String param)
{
int pinNumber = param[0] - '0';
int location = param.substring(2, param.length()).toInt();
BELL.write(145);
delay(1000);
BELL.write(125);
delay(1000);
digitalWrite(pins[pinNumber], 1);
FLOOR_SELECTOR.write(location);
delay(5000);
// Reset pins and floor selector
for(int i=0; i<6; i++){
digitalWrite(pins[i], 0);
}
FLOOR_SELECTOR.write(90);
return 1;
}
void setup()
{
FLOOR_SELECTOR.attach(A0);
BELL.attach(A4);
FLOOR_SELECTOR.write(90);
for(int i=0; i<6; i++){
pinMode(pins[i], OUTPUT);
digitalWrite(pins[i], 1);
}
delay(3000);
for(int i=0; i<6; i++){
digitalWrite(pins[i], 0);
}
Spark.function("updateState", updateState);
}