Skip to content

Instantly share code, notes, and snippets.

@kmmonk

kmmonk/Dancing Groot

Created Dec 16, 2014
Embed
What would you like to do?
#define PIN_MOTOR D0
#define PIN_SOUND D4
#define PIN_MOTION D2
#define MAX_DANCE_TIME 2 * 60 * 1000
unsigned int started_dancing = 0;
bool dancing = false, wasMotion = false;
void setup() {
pinMode(PIN_MOTOR, OUTPUT);
pinMode(PIN_MOTION, INPUT);
pinMode(PIN_SOUND, OUTPUT);
Spark.function("dance", toggleDancingFn);
Spark.function("music", toggleMusicFn);
Spark.function("stop", stopFn);
//turn off sound on power up?
// toggleMusic();
Spark.publish("groot/status", "online");
}
void loop() {
int motion = digitalRead(PIN_MOTION);
if (motion && !wasMotion) {
wasMotion = true;
toggleDancing();
}
//foo
else if (!motion && wasMotion) {
wasMotion = false;
}
unsigned int danceTime = millis() - started_dancing;
if (dancing && (danceTime > MAX_DANCE_TIME)) {
toggleDancing();
}
delay(250);
}
int toggleDancingFn(String cmd) {
toggleDancing();
return 0;
}
int stopFn(String cmd) {
dancing = false;
switchMotor();
toggleMusic();
}
void toggleDancing() {
dancing = !dancing;
if (dancing) {
started_dancing = millis();
Spark.publish("groot/status", "dancing");
}
else {
Spark.publish("groot/status", "NOT DANCING :(");
}
switchMotor();
toggleMusic();
}
void switchMotor() {
digitalWrite(PIN_MOTOR, (dancing) ? HIGH : LOW);
}
void toggleMusic() {
if (dancing) {
digitalWrite(PIN_SOUND, HIGH);
}
else {
digitalWrite(PIN_SOUND, LOW);
}
}
int toggleMusicFn(String cmd) {
toggleMusic();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment