Back to Parent

Final Code
/*
 * Project aHome_Sight
 * Description:  Requires integration with aHome_Sound
 * Author:  Ben Fisher
 * Date:
 */

// stepper
// defines pin numbers
int stepPin = D5;
int dirPin = D6;
int whereIsTheStepperNow = 0;
int numSteps = 0;  // number of steps to travel (currently unused)
int stepsPerRev = 200; // stepper motor steps per revolution of the spindle
int numberOfDrawings=4; // number of possible "outcomes" represented in 360 deg.

// stepper-related variables
int degreeChangeRequired = 0;
int currentPicture = 0; // ranges from 0 to (numberOfDrawings-1)
int desiredPicture = 0; // ranges from 0 to (numberOfDrawings-1)

// weather
String weatherIcon = "";
double temperature = 0;
double precipProbability = 0;
double precipIntensity = 0;

// setup() runs once, when the device is first turned on.
void setup() {
  // Put initialization like pinMode and begin functions here.

  //stepper
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);

  // make the temperature values visible online
  Particle.variable("weatherIcon", &weatherIcon, STRING );

  // debug motor
  Particle.variable("currentPos", &currentPicture, INT );
  Particle.variable("desiredPos", &desiredPicture, INT );

  // connect to other devices
  Particle.subscribe(  "diot/2018/connected/atHome" , handleSharedEvent );

  // opens serial port for LCD shield
  Serial.begin(9600);
}

void getData() // For weather
{
    // Publish an event to trigger the webhook
 Particle.publish("forecast", "19.0896,72.8656", PRIVATE);
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {

}

void moveStepper( int steps ){

    if (steps > 0){
      // Enables the motor to move in a particular direction
      digitalWrite(dirPin, HIGH);
      // Makes "steps" number of steps (200 = full revolution)
      for(int x = 0; x < abs(steps); x++) {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(5000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(5000);
      }
    }else{
      // Enables the motor to move in the opposite direction
      digitalWrite(dirPin, LOW);
      // Makes "steps" number of steps (200 = full revolution)
      for(int x = 0; x < abs(steps); x++) { //abs() in case steps is negative
                              // alternatively, for(int x = 0; x > steps; x--){
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(5000);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(5000);
      }
    }

    // Either way, update whereIsTheStepperNow
    whereIsTheStepperNow = whereIsTheStepperNow + steps;
    if( whereIsTheStepperNow >= 200 ){ // we passed the 0 point moving cw
      whereIsTheStepperNow = whereIsTheStepperNow - 200;
    }
    if( whereIsTheStepperNow < 0 ){ // we passed the 0 point moving ccw
      whereIsTheStepperNow = whereIsTheStepperNow + 200;
    }

    delay(1000); // One second delay
    Serial.print( "Current number of steps from 0 is " );
    Serial.println( whereIsTheStepperNow );

}

void handleSharedEvent(const char *event, const char *data)
{
    // Now we're getting ALL events published using "diot/2018/connected/atHome"
    // This includes events from this device.
    // So we need to ignore any events that we sent.

    // Let's check the event name
    String eventName = String( event ); // convert to a string object
    // This gives us access to a bunch of built in methods
    // Like indexOf()
    // Locates a character or String within another String.
    // By default, searches from the beginning of the String,
    // but can also start from a given index,
    // allowing for the locating of all instances of the character or String.
    // It Returns: The index of val within the String, or -1 if not found.

    // We can use this to check if our event name contains the
    // id of this device

    String deviceID = System.deviceID();
    weatherIcon = String(data);

    if( eventName.indexOf( deviceID ) != -1 ) //is mark publishing anything
    {
      // if we get anything other than -1
      // the event came from this device.
      // so stop doing stuff
      return;
    }

    // otherwise do your stuff to respond to
    // the paired device here

    Serial.print("weather icon is ");
    Serial.println(weatherIcon);

    // move the stepper motor accordingly
    /*
    note that these are based on the event published by other devices,
    not getting it from forecast.io using a webhook
    */
    if (weatherIcon == "cloud") {
      desiredPicture = 1;
    }
    else if (weatherIcon == "wind") {
      desiredPicture = 2;
    }
    else if (weatherIcon == "clear"){
      desiredPicture = 3;
    }
    else if (weatherIcon == "rain"){
      desiredPicture = 0;
    } //otherwise it's none of these, and no motion is required
    Serial.print("desiredPicture is");
    Serial.println(desiredPicture);

    // reminder:  need {} if using 2+ lines in the "then" statements
    int picDifference = desiredPicture - currentPicture;

    // reset from previous iteration of looo
    degreeChangeRequired = 0;

    if (picDifference == 1 || picDifference == (1 - numberOfDrawings))
      degreeChangeRequired=(360 / numberOfDrawings);
    if (picDifference == -1 || picDifference == (numberOfDrawings - 1))
      degreeChangeRequired=(-360 / numberOfDrawings);
    if (picDifference == 2 || picDifference == (2 - numberOfDrawings))
      degreeChangeRequired=(-2 * 360 / numberOfDrawings);
    // otherwise, no motion is required

    numSteps = degreeChangeRequired*stepsPerRev/360;
    moveStepper( numSteps );
        // update currentPicture
        currentPicture = desiredPicture;
}
Click to Expand

Content Rating

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

0