Back to Parent

// Particle Code

    //
    //∆∆∆∆     SERVO STUFF     ∆∆∆∆
    //

    int mouthClosed = 168;
    int mouthOpen = 129;
    int increment = 4;
    int newPos = mouthClosed + increment;
    int servoPin = D0;
    Servo myServo;
    int servoPos = mouthClosed;


    void setup() {
      Serial.begin(9600);

      myServo.attach( D0 );

      Particle.function("servo", servoControl);
      // Particle.function("message", handleDirectMessages)

      Particle.variable("servoPos" , &servoPos , INT );

      Particle.subscribe("DirectMessages", handleDirectMessages);
      const char * test;
      const char * t;
      handleDirectMessages(test, t);

      }

    void loop() {

    }

    void handleDirectMessages( const char * eventName, const char *data ){

      String dataStr = String( data );
      int directMessages = dataStr.toInt();

      Serial.println("new event published, sent");
      Serial.println(directMessages);
      Serial.println("hellyeah");
      incrementIt();
      for(int i = 0; i < 3; i++){
        digitalWrite(D7, HIGH);
        delay(100);
        digitalWrite(D7, LOW);
        delay(100);
      }
      //servoControl("fw");

    }

    int servoControl(String command)
    {
        //char data = 2;
        Serial.println(command);
        //handleDirectMessages("DirectMessages",  data );
       // Convert
         if(command.equals("fw"))
         {
           newPos = servoPos-increment;
         }
         else if(command.equals("bw"))
         {
           newPos = mouthClosed;
         }
         else{
            newPos = command.toInt();
         }
         servoPos = newPos;
         Serial.print("servoPos");


            // Make sure it is in the right range
      //servoPos = constrain( newPos, 0 , 180);

      // Set the servo
      myServo.write(servoPos);
    //Serial.println("wazzup" );
      Serial.print(newPos);
      Serial.print("    ");

      //BLINK IT
      //blinkLED(100, 100, 120);

      //blinkLED(230, 230);



       return 1;
    }

    int incrementIt()
    {
        if(servoPos-increment<=mouthOpen){
            servoControl("bw");
        }
        else{
            servoControl("fw");
        }
        return servoPos;
    }
Click to Expand

Content Rating

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

-1