Back to Parent

/*Final Code
send time for next bus through particle function. 
This will move the bus if it is at or below threshold
Resources:
https://www.robotshop.com/media/files/pdf/datasheet-1182.pdf
Accelstepper library: https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#affbee789b5c19165846cf0409860ae79
*/

#include "AccelStepper.h"
const int tracklength = 1200; // # clicks to cover whole track
const int threshold = 5; // [minutes] consider this as whole track threshold for next bus




// defines pins
const int step = D2;
const int dir = D1;
// Define Stepper Motor
AccelStepper Stepper(1, step, dir);
void setup() {
    Stepper.setMaxSpeed(1000);
    pinMode(D7,OUTPUT);
    digitalWrite(D7, HIGH);
    Particle.function("next", bustime);
}
void loop() {
    //waiting to receive next bus time 
}
// Receives next bus time [min]
int bustime(String nextbustime){
    digitalWrite(D7, LOW);
    int bus_time = atoi(nextbustime);
    if (bus_time > threshold){
        transmit("bus is too far",bus_time);
    }
    if (bus_time == 0){
        transmit("waiting for next bus",1);
    }
    if (bus_time > 0 && bus_time <= threshold){
        transmit("next bus [min]: ",bus_time);
        // Calculate motor speed
        int speed = tracklength/(bus_time*60);
        transmit("speed [clk/sec]: ",speed);
        Stepper.setSpeed(speed); 
        // Run to end of track
        while (Stepper.currentPosition() < tracklength){ 
            Stepper.runSpeed();
        }
        transmit("Bus Arrived",1);
        // Run back to start zero time in 6 sec = 1200/(-200)
        Stepper.setSpeed(-200);
        while(Stepper.currentPosition() > 0){
            Stepper.runSpeed();
        }
        return 1;
    }
    else {
        return -1;
    }
}
void transmit(char message[],int value){
   char valtext[1];
   sprintf(valtext,"%i", value);
   Particle.publish(message,valtext,PRIVATE);
}
Click to Expand

Content Rating

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

0