Last active
May 21, 2024 01:14
-
-
Save steveadams/b34a5fc119cbd014ea937b4a087dfff8 to your computer and use it in GitHub Desktop.
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
#include <Automaton.h> | |
#define SOLENOID_RELAY_PIN 13 | |
Atm_analog waterLevel; | |
Atm_bit solenoidRelay; | |
Atm_controller solenoidRelayOpener; | |
Atm_controller solenoidRelayCloser; | |
int previousWaterLevel = 0; | |
void setup() { | |
Serial.begin(9600); | |
while (!Serial); | |
Serial.println("Serial connected."); | |
// Get the water level from analog pin 1 | |
// Map its values from 0..100 | |
waterLevel.begin(A1) | |
.range(0, 100) | |
.onChange(reportLevel); | |
// Register the solenoid relay's pin as the bit machine's status LED | |
// The HIGH/LOW states will open and close the solenoid as expected | |
solenoidRelay.begin() | |
.led(SOLENOID_RELAY_PIN) | |
.onChange([](int idx, int v, int up) { | |
Serial.print("Relay is now "); | |
Serial.println(v ? "ON" : "OFF"); | |
}); | |
// I'd like to have a single controller, but couldn't find a way to have it behave properly | |
// This uses one to open the solenoid and one to close it using separate conditions. | |
solenoidRelayOpener.begin() | |
.IF( waterLevel, '<', 25 ) | |
.onChange( true, solenoidRelay, solenoidRelay.EVT_ON ); | |
solenoidRelayCloser.begin() | |
.IF( waterLevel, '>', 90 ) | |
.onChange( true, solenoidRelay, solenoidRelay.EVT_OFF ); | |
} | |
void loop() { | |
automaton.run(); | |
} | |
void reportLevel( int idx, int v, int up ) { | |
if (previousWaterLevel != v) { | |
Serial.println("Water Level:" + String(v)); | |
previousWaterLevel = v; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment