I was only planning to have a quick play with the LCD display I bought recently, but ended up finishing off my Arduino + Home Easy polar bear controller project as well. Not that it actually needed a display you understand, but I’d wired it up, so why not!
…complete with a manual override should it annoy Jo a bit too much! I finished a bit ahead of schedule in the end; the polar bears aren’t actually out yet… because it’s not Christmas! I have dug the tree up though, and as soon as it’s indoors we’ll have some ambient bears to tell us when to turn something off. Here’s the sketch so far, warts and all:
#include <Bounce.h> #include <HomeEasyCtrl.h> #include <Messenger.h> #include <LiquidCrystal.h> #define WIRELESS 6 #define BUTTON 7 #define LED 13 // Define a metronome unsigned long previousMillis = 0; // Set the default interval to every 3 minutes - // enough time for a cup of tea unsigned long interval = 180000; // Instantiate a Bounce object with a 5 millisecond debounce time Bounce bouncer = Bounce(BUTTON,5); // Instantiate Home Easy controller with transmitter on pin 4 and LED on 13 HomeEasyCtrl lightController(WIRELESS,LED); // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); // Instantiate Messenger object with the message function and the default separator (the space character) Messenger message = Messenger(); // Flag indicating the desired state of the lights bool deviceOn = false; // Flag indicating whether bears are on manual! bool manual = false; // Last energy reading in watts int watts = 0; // Energy use messages int energyUse = 0; char* energyUseMessages[]={"Energy: low", "Energy: normal", "Energy: high"}; // Define messenger function void messageCompleted() { int elementCount = 0; int last = 0; Serial.println("Message received"); // Loop through all the available elements of the message while ( message.available() ) { elementCount++; int value = message.readInt(); if ((value == 0) || (elementCount > 2)) { Serial.println("Oops"); return; } Serial.println(value, DEC); // wait for same value twice for simple // error checking if (value == last) { watts = value; } else { last = value; } } Serial.println("Watts"); Serial.println(watts, DEC); checkEnergyUse(); } void checkEnergyUse() { if (watts < 50 || watts > 350) { deviceOn = false; previousMillis = millis(); // Don't mind waiting for things to go off if (watts < 50) { energyUse = 0; // low } else { energyUse = 2; // high } } else if (watts > 100 && watts < 300) { deviceOn = true; previousMillis = millis(); energyUse = 1; // normal // Want things to come on immediately lightController.deviceOn(); } // otherwise, no change updateDisplay(); } void updateDisplay() { lcd.clear(); // 1st line if (manual) { lcd.print("Manual override"); } else { lcd.print(energyUseMessages[energyUse]); } // 2nd line lcd.setCursor(0,1); if (deviceOn) { lcd.print("Polar bears: on"); } else { lcd.print("Polar bears: off"); } } void setup() { pinMode(BUTTON,INPUT); // Initiate Serial Communication Serial.begin(9600); // set up the LCD's number of rows and columns: lcd.begin(16, 2); lcd.print("Ready"); lcd.setCursor(0,1); lcd.print("Polar bears: off"); // set up incoming message processor message.attach(messageCompleted); } void loop() { // Check push button for manual override if ( bouncer.update() ) { if ( bouncer.read() == LOW) { if (!manual) { manual = true; deviceOn = true; lightController.deviceOn(); } else { if (deviceOn) { deviceOn = false; lightController.deviceOff(); } else { manual = false; checkEnergyUse(); } } updateDisplay(); } } // The following line is the most effective way of // feeding the serial data to Messenger while ( Serial.available( ) ) message.process(Serial.read( ) ); // Keep the device in the desired state if ( millis() - previousMillis > interval ) { previousMillis = millis(); if (deviceOn) { lightController.deviceOn(); } else { lightController.deviceOff(); } updateDisplay(); } }
No polar bears have been harmed in the making of this post. At least not yet anyway.
Update: Seems to be working quite well, although the manual override button seems to have problems- it seems to randomly flip back on when I want it to stay off. Still, the button works well enough to pair with a Home Easy socket which is the main thing. Tree and polar bears should be up this weekend. (16 Dec 2009)
Pingback: Polar Bear Tree « Notes from a small field
Pingback: Living with polar bears « Notes from a small field
Pingback: Illuminations « Notes from a small field