Back to Parent

Original code for the email notification
int motorPin = A3;

int hiSpeed = 150;
int medSpeed = 100; 
int lowSpeed = 50;
int motorSpeed = 100;  

long motorTimer = 0; 
int motorDelayMillis = 5000;

bool motorOn = false; 

void setup() {
    pinMode(motorPin, OUTPUT);
    Particle.function("motorSpeed", motorControl);
    Particle.variable(  "motorSpeed" , &motorSpeed , INT );
}

void loop() {
    motorTimerCheck();
}

int motorControl(String command)
{
    // Set Activation Time and Speed
    if (command == "HIGH"){
        TimerAdj(10);
        motorSpeed = hiSpeed;
    }
    else if (command == "MED"){
        TimerAdj(5);
        motorSpeed = medSpeed;
    }
    else if (command == "LOW"){
        TimerAdj(2);
        motorSpeed = lowSpeed;
    }
    
    analogWrite(motorPin, motorSpeed);
    motorOn = true;
    motorTimer = millis();
    return 1; 

    // done
}

void TimerAdj(int seconds){
    // convert to seconds
    motorDelayMillis = 1000* seconds;
}

void motorTimerCheck(){
    // if timer has elapsed
    if (motorOn && (millis()-motorTimer>= motorDelayMillis)){
        motorSpeed = 0; 
        motorOn = false;
        analogWrite(motorPin, motorSpeed);
    }
}
Click to Expand

Content Rating

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

0