Team Blossom - Skills Dev V

Made by Amal Jafrani, Priya Jain, Miley Hu and ihans

Created: November 30th, 2021

0

Outcome

    • The goal of this skills dev was to create a paired device that allowed us to push a button one device and send that data to the other to spin a servo
0

Process

We began our process by creating a singular device and writing the code so that when a button is pressed the servo would spin. We encountered some minor problems here, but overall it was a smooth process. The next step was writing out the code for the paired devices. Initially we struggled with understanding the difference between particle subscribe and publish, but once we were able to understand that the rest of the code was easier to complete. The final step was combining the two parts, so we combined our initial circuit with the code for the paired device. This is where we ran into the most problems. We first forgot about echo, and then once that was set up we ran into some minor bugs. For example, when we press the button both our servos move, but we only want the servo of the other device to move. We still were not able to solve this problem. Another issue we ran into was having too many delays. We did not realize one of the delays being 10 seconds would create problems, but this delay required us to hold down the button which we did not initially realize. We were able to solve this issue fairly quickly by changing the delay, and we were then done with our paired device.

0

Reflection

Overall, our group is happy with where we are at with the paired device because even though we were unable to get the servo to only spin on one device we have a good base for our final project. We would need to replace the button with a photocell and the servo with a neopixel. If we had more time we would want to focus on the bugs we encountered with the echo.

0
long lastPublishedAt = 0;
int publishAfter = 100;

bool motorOn = false;

int button = D3;
int servoPin = A3;

Servo myServo;
int servoPos = 0;

void setup()
{
  
    myServo.attach( A3 );
    pinMode(button, INPUT_PULLUP);
    pinMode(servoPin, OUTPUT);

  Particle.subscribe( "diot2021" , handleSharedEvent );

}

void loop()
{

    publishMyEvent();
    
    if (motorOn == true) {
        for (servoPos = 0; servoPos <= 180; servoPos += 1) { // goes from 0 degrees to 180 degrees
        // in steps of 1 degree
            myServo.write(servoPos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
        }
        for (servoPos = 180; servoPos >= 0; servoPos -= 1) { // goes from 180 degrees to 0 degrees
            myServo.write(servoPos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
        }
        
        motorOn = false;
    }
    
    delay(100);
}


void publishMyEvent()
{

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
    
      String eventName = "diot2021";

      if (digitalRead(button) == LOW){
          
            Particle.publish( eventName );
      }
      
      
      lastPublishedAt = millis();
  }

} 

void handleSharedEvent(const char *event, const char *data)
{
    
    String eventName = String( event ); // convert to a string object
    

    String deviceID = System.deviceID();

    // device id = 0123456789abcdef
    // event = "diot/2019/paired/0123456789abcdef"

    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }
    
    else {
        motorOn = true;
    }
    
}
Click to Expand
x