Servo servo;
int servoPin = A2;
int switchPin = D5;
int curSwitchState;
int ledPin = D2;
int servoPos;
int first = 1;
void setup()
{
pinMode( switchPin , INPUT_PULLUP); // sets pin as input
pinMode( ledPin , OUTPUT ); // sets pin as output
servo.attach( servoPin );
servo.write(0);
servoPos = 0;
curSwitchState = digitalRead(switchPin);
Particle.variable("servoPos", servoPos);
Particle.variable("buttonState", curSwitchState);
Particle.subscribe("rotate", handleRemoteRotate);
}
// Last time, we wanted to continously blink the LED on and off
// Since we're waiting for input through the cloud this time,
// we don't actually need to put anything in the loop
void loop() {
int newSwitchState = digitalRead(switchPin);
if( newSwitchState == LOW ){
digitalWrite( ledPin, HIGH);
}else{
digitalWrite( ledPin, LOW);
}
if (curSwitchState != newSwitchState) {
Particle.publish("triggerRotate");
rotateServo();
curSwitchState = newSwitchState;
}
delay(1000);
}
int handleRemoteRotate(const char *event, const char *data) {
rotateServo();
return 1;
}
int rotateServo() {
if (servoPos == 0) {
servoPos = 180;
servo.write(180);
return 1;
} else if (servoPos == 180) {
servoPos = 0;
servo.write(0);
return 1;
} else {
return -1;
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .