Back to Parent

Servo switchServo;
// invokes the inbuilt servo function and assigns it to switchServo

int step = 20;
int speed = 0;
// step and speed are global variables used to control the current degree and speed of the servo motor

long startedAt = 0;
// It monitors the time since the last servoevent was activated
//duration is used

bool shouldDoIt = false;
bool increasing = true;

int touchSensor = D3;
// assigns capacitive sensor, that is connected to D3 to a variable named TouchSensor 
bool lastState = false;
int touchState = LOW;
// touchState is used to monitor when the cap touch was last activated 
// lastState is used to control the movement of the servo based on servo activation


bool overRide = false;
// used to turn on the loop when servoEvent is activated

int a = 1;

long lastPublishedAt = 0;
// this is the time delay before we should publish a new event
// from this device
int publishAfter = 10000;

void setup()
{

pinMode( touchSensor, INPUT);

switchServo.attach(A0);
switchServo.write(20); 
// sets servo to starting position

Particle.subscribe("diot/2019/paired/sijiavedhachun/servo",servoEvent);
// the devices are subscribed to a common channel and an event named servoEvent from that channel

Particle.subscribe(  "servostart", servoEvent );
// the devices are subcribed to an IFTTT trigger

}

void loop()

{
    if ( a = 1 )
    {
        shouldDoIt = true;
        a = 2; 
    }

    
touchState = digitalRead(touchSensor);
 // reads the value from touchSensor
checkLaststate();
// function checks when the touchSensor was last activated


  if(shouldDoIt == true)

  {
   startedAt = millis();
   // records the time at which the servoEvent starts
   shouldDoIt = false;
  }



if ( overRide == true )

  {

   speed = speedCalculate();
   // uses a function to calculate the speed of the servo motor in respect to the time since last trigger of servoEvent
   switchServo.write(step);
   // servo motor  is moved to a required angle based on the value on the step variable
   delay(speed);
   // controls the time between consecutive steps of a servo motor
   step = stepCalculate();
   // calculates the step value for the next loop
 
  }

}
// void loop ends


int speedCalculate()
// the function uses the time difference between the time when the servoEvent started and the time at a given moment to calculate the time delay between two consecutive servo motions
{
    if ((millis () - startedAt) < 20*1000 )
    {
    speed = 500;
    }
     if ((millis () - startedAt) > 20*1000 && (millis () - startedAt) < 40*1000 )
    {
    speed = 300;
    }
    if ((millis () - startedAt) > 40*1000 )
    {
    speed = 200;
    }
    return speed;
}


int stepCalculate()
// the function uses the previous step of the servo motor to caclulate its next step or angle. The function also decides the direction of movement 
{
     
     if (step < 160 && increasing == true )
 {
     step = step + 5;
 }
     else 
 {
     step = step - 5;
     increasing = false;
      if (step == 20)
     {
         increasing = true;
         // increasing is a variable used to register the direction of motion of the servo motor
     }
 }
 return step;
}


bool checkLaststate()
// checks is capacitive touch has been activated
// stops the motor
// publishes an event to trigger IFTTT
{
    if ( touchState == HIGH ); 
    {
    publishMyEvent();
    overRide = false;
// delay for a bit
    delay(1000);
    } 
}

int servoEvent(const char*event, const char*data)
// recieves an input from the channel subscribed to and restarts the servo loop
 {
   
step = 20;
// sets servo angle to start point
increasing = true;
// sets servo direvtion to start point
shouldDoIt = true;
//resets the timer
overRide = true;
// starts the loop
return 1;
 }


void publishMyEvent()
{
  // Remember that a device can publish at rate of about 1 event/sec,
  // with bursts of up to 4 allowed in 1 second.
  // Back to back burst of 4 messages will take 4 seconds to recover.
  // So we want to limit the amount of publish events that happen.

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
      // Remember our subscribe is matching  "db2018/paired/"
      // We'll append the device id to get more specific
      // about where the event came from
      // System.deviceID() provides an easy way to extract the device
      // ID of your device. It returns a String object of the device ID,
      // which is used to identify your device.
      String eventName = "diot/2019/paired/sijiavedhachun" + System.deviceID();
      // now we have something like "diot/2019/paired/0123456789abcdef"
      // and that corresponds to this devices info
      // then we share it out
      Particle.publish( eventName, "true" );
      // And this will get shared out to all devices using this code
      // we just pubished so capture this.
      
      Particle.publish( "sendmessage", "done" );
      // the trigger for IFTTT sending message 
      
      lastPublishedAt = millis();
  }

}
Click to Expand

Content Rating

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

0