Created
March 29, 2015 10:07
-
-
Save jameswood/d3713326de32275d71e3 to your computer and use it in GitHub Desktop.
Prawnbot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool debug = FALSE; | |
bool disableMotor = FALSE; | |
bool motorJammed = FALSE; | |
int motorPin = D5; | |
int switchPin = D2; | |
int ledPin = D7; | |
int dispense(String command); | |
int dispenseTimeout = 2000; | |
int debounce = 50; | |
long dispenseStartTime; | |
#pragma GCC diagnostic ignored "-Wunused-variable" | |
void setup() { | |
Spark.function("Dispense", dispense); | |
pinMode(switchPin, INPUT_PULLUP); | |
pinMode(motorPin, OUTPUT); | |
pinMode(ledPin, OUTPUT); | |
if(!debug) { | |
while (digitalRead(switchPin)==HIGH) { | |
dispenseStartTime = millis(); | |
advanceMotor(millis(), debounce); | |
if (millis() > dispenseStartTime + dispenseTimeout) { | |
jam(); | |
break; | |
}; | |
}; | |
if (!disableMotor) digitalWrite(motorPin, LOW); | |
}; | |
} | |
void loop() { | |
digitalWrite(ledPin, digitalRead(switchPin)); | |
} | |
int dispense(String command) { | |
if (!motorJammed) { | |
if(!debug) { | |
dispenseStartTime = millis(); | |
while (digitalRead(switchPin)==LOW){ // initial movement to get past switch point | |
advanceMotor(millis(), debounce); | |
if (millis() > dispenseStartTime + dispenseTimeout) { | |
jam(); | |
return 1; | |
break; | |
}; | |
}; | |
while (digitalRead(switchPin)==HIGH){ // Moving motor to end point | |
advanceMotor(millis(), debounce); | |
if (millis() > dispenseStartTime + dispenseTimeout) { | |
jam(); | |
return 1; | |
break; | |
}; | |
}; | |
if (!disableMotor) digitalWrite(motorPin, LOW); | |
}; | |
Spark.publish("doggies_fed","fed"); | |
return 1; | |
} | |
} | |
void advanceMotor(long startTime, int runTime) { | |
while (millis() < startTime + runTime) if (!disableMotor) digitalWrite(motorPin, HIGH); | |
} | |
void jam() { | |
if (!disableMotor) digitalWrite(motorPin, LOW); | |
Spark.publish("doggies_fed","jam"); | |
motorJammed = TRUE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cat feeder for Spark Core. Triggered/notifications via IFTTT.