Back to Parent

#include <stdio.h>
#include <string.h>
#include <math.h>

int SENSORPIN = A4;
int LEDPIN = D0;
int sensorState = 0, lastState=0;
int servoPin = A5;
int servoPos = 180;
Servo myServo;
void setup() {
  Particle.function("rssData", rssData);
  //  Servo
  myServo.attach( A5 );
  //Register our Particle to control the servo
  Particle.function("servo", servoControl);
  // Initialize the LED pin as an output
  pinMode(LEDPIN, OUTPUT);
  // Initialize the sensor pin as an input
  pinMode(SENSORPIN, INPUT);
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup
  Serial.begin(9600);
}
void loop() {
  // Read the state of the pushbutton value:
  sensorState = digitalRead(SENSORPIN);
  // Sensor Beam is Broken
  if (sensorState == LOW) {
    // turn LED on
    lightUp();
  }
  else {
    // turn LED off
    digitalWrite(LEDPIN, LOW);
  }
  if (sensorState && !lastState) {
    Serial.println("Unbroken");
  }
  if (!sensorState && lastState) {
    Serial.println("Broken");
  }
  lastState = sensorState;
}

int lightUp () {
  digitalWrite(LEDPIN, HIGH);
  servoControl("10");
  delay(3000);
  servoControl("180");
  return 0;
}

int rssData(String command)
{
  int foo = command.toInt();
  lightUp();
  return 1;
}

int servoControl(String command)
{
  // Convert
  int newPos = command.toInt();
  // Make sure it is in the right range and set the position
  servoPos = constrain( newPos, 0 , 360);
  // Set the servo
  myServo.write( servoPos );
  return 1;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0