Skip to content

Instantly share code, notes, and snippets.

@danhigham danhigham/fsm_dc.ino
Last active Jun 8, 2017

Embed
What would you like to do?
Dust collection with an Arduino
#include <Fsm.h>
#include "EmonLib.h"
#define RELAY1_PIN 7
#define LED_PIN 13
#define ON 1
#define OFF 0
#define THRESHOLD 4
EnergyMonitor emon;
void on_dc_off_enter() {
Serial.println("Turning dust collector off");
digitalWrite(RELAY1_PIN, HIGH);
}
void on_dc_ready_enter() {
Serial.println("Entering ready state");
}
void on_dc_on_enter() {
Serial.println("Turning dust collector on");
digitalWrite(RELAY1_PIN, LOW);
}
void on_dc_cool_enter() {
Serial.println("Running dust collector for 6 seconds");
}
State state_dc_on(&on_dc_on_enter, NULL);
State state_dc_off(&on_dc_off_enter, NULL);
State state_dc_cool(&on_dc_cool_enter, NULL);
Fsm fsm_dc(&state_dc_off);
void setup() {
Serial.begin(9600);
pinMode(RELAY1_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(LED_PIN, LOW);
Serial.println("Running...");
emon.current(1, 111.1);
Serial.println("Skipping first 10 samples...");
for (int x=0;x<10;x++) emon.calcIrms(1480); // ignore the first 10 samples
fsm_dc.add_transition(&state_dc_off, &state_dc_on, ON, &on_dc_on_enter);
fsm_dc.add_transition(&state_dc_on, &state_dc_cool, OFF, &on_dc_cool_enter);
fsm_dc.add_timed_transition(&state_dc_cool, &state_dc_off, 6000, NULL);
}
void loop() {
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
// put your main code here, to run repeatedly:
double irms = emon.calcIrms(1480);
if (irms > THRESHOLD) {
fsm_dc.trigger(ON);
} else {
fsm_dc.trigger(OFF);
}
fsm_dc.check_timer();
Serial.println(irms);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.