Posts tagged: ciseco

Ooh Arr-duino

Santa this year was very generous to my son and gifted him with a Maverick Atom XT RTR 1/18 Electric 4WD Truggy but failed to mention that the batteries that came with it could only be left in the charger for 6 hours, no longer. With grave warnings about explosions and fire, I tried to find an egg-timer on a socket kind of solution, but could not find anything that would go beyond 4 hours.

So what is a tinkerer to do? He makes one himself! ;)




A long time ago I bought an Arduino, but never really got any further with it than a blinking RGB led. Now I finally found a purpose for it :) I was going to build an Arduino controlled timer socket!

All I needed now were some kind of relay, some sockets, a few buttons and a way of letting the user my son know what the time was set to and if it was on or not. I ended up with a Ciseco Arduino Relay Shield, two Maplin push-to-make switches, a DFRobot I2C 16×2 LCD and a Maplin project box big enough to fit it all in

First a small word of warning. You are dealing with MAINS voltage, which can be lethal if you fool around. DO NOT EVER! connect the bits to mains power when parts of it are exposed. You’ve been warned

I started off with a small mockup of materials and then got my trusty Dremel clone out and started cutting away at the project case.

First off is the socket in which the battery charger can be plugged. A small template on paper is by far the easiest way to make sure you don’t cut out too much behind the socket

With the socket in place, I created the template for the LCD. I took my time with this, as I didn’t want to cut out too much. A lot of careful sanding ensured that only the black part would be visible, and nothing else. Then with the help of some small bolts and nuts, I created some spacers, enabling the LCD to sit nicely in the cutout. Lots of hotglue completes the mounting

The buttons only required some measuring to make sure they are neatly symetric and do not interfere with the LCD

All that remains is the IEC socket where the power will go into the project box

Now with everything in place, all we need to do is connect the components together with some wire using the below schematic

Warm up the soldering iron and just follow the diagram. As my soldering skills are not that great, I also used a bit of heatshrink here and there to make sure that wires could not short on each other :)

I’m using an old iPhone charger to power the Arduino as it provides a neat 5V/1A, with a small retractable USB lead with a Type A to B converter. A quick test of the wiring to make sure it all works as planned

Now with construction over, it was time to do the Arduino coding. The LCD uses the LiquidCrystal I2C library which takes care of all the hard work interfacing to it. Likewise, the Metro library takes care of the timer. So, it is just a simple case of increasing the clock by an hour every time the red button is pressed, and starting the timer when the green button is pressed. Once the timer is running, any button press will stop the timer and reset the state to the beginning.

// Arduino controlled timer socket
// (CC BY) 2011
// http://awooga.nl
 
// pull in libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Metro.h>
 
 
// arduino pins constants
#define RELAY 2 // black wire connected to input on shield
#define RED   6 // green wire
#define GREEN 7 // white wire
 
// i2c pin A5 = white wire
// i2c pin A4 = green wire
 
// allow between 1 and 6 hours
#define MINIMUM_TIME 1
#define DEFAULT_TIME 6
#define MAXIMUM_TIME 6
 
 
const unsigned long MILLISECONDS=1;
const unsigned long SECONDS=(1000*MILLISECONDS);
const unsigned long MINUTES=(60*SECONDS);
const unsigned long HOURS=(60*MINUTES);
const unsigned long DAYS=(24*HOURS);
 
const unsigned long UNITS=HOURS;
 
 
// prgram states
#define IDLE         0
#define RED_BUTTON   1
#define GREEN_BUTTON 2
#define MENU         3
#define PAUSE        4
#define STOP_TIMER   5
#define CANCEL_TIMER 6
 
 
// global variables
byte events;
byte countdowntime;
boolean menu_running;
boolean timer_running;
 
boolean debug = false;
 
byte currentRed = LOW;
byte currentGreen = LOW;
byte previousRed = LOW;
byte previousGreen = LOW;
 
// set the LCD address to 0x27 for the 16x2 display
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
// initiate Metro object
Metro relayMetro = Metro(0, true);
 
 
// arduino setup routine
void setup() {
 
  // set the arduino pins
  pinMode(RELAY, OUTPUT);
  pinMode(RED, INPUT);
  pinMode(GREEN, INPUT);  
 
  // initialise the lcd
  lcd.init();
  lcd.backlight();
 
  // activate debugging on the serial port
  if (debug) {
    Serial.begin(9600);
    Serial.println("Lets go!");
  }
 
  // initialise variables
  events = IDLE;
  countdowntime = DEFAULT_TIME;
  menu_running = false;
  timer_running = false;
  digitalWrite(RELAY, LOW);
}
 
 
// arduino loop routine
void loop() {
 
  switch (events) {
 
    case IDLE:
      events = Idling();
      break;
 
    case MENU:
      events = MainMenu();
      break;
 
    case RED_BUTTON:
      events = RedButton();
      break;
 
    case GREEN_BUTTON:
      events = GreenButton();
      break;
 
    case PAUSE:
      events = Pause();
      break;
 
    case STOP_TIMER:
      events = StopTimer();
      break;
 
    case CANCEL_TIMER:
      events = CancelTimer();
      break;
 
    default:
      events = IDLE;
      break;
  }
}
 
 
int MainMenu() {
 
  if (timer_running) {
    return IDLE;
  }
 
  if (menu_running) {
    return IDLE;
  }
 
  if (debug) { Serial.println("MainMenu()"); }
 
  lcd.clear();
  lcd.print("  Red: Set time");
  lcd.setCursor(0, 1);
  lcd.print("Green: Go (");
  lcd.print(countdowntime, DEC);
  if (countdowntime == 1) {
    lcd.print("hr)");
  }
  else {
    lcd.print("hrs)");
  }
 
  menu_running = true;
 
  return IDLE;
}
 
 
int Idling() {
 
  boolean do_red = false;
  boolean do_green = false;
 
  if (timer_running) {
    if (relayMetro.check()) {
      return STOP_TIMER;
    }
  }
 
  currentRed = digitalRead(RED);
  currentGreen = digitalRead(GREEN);
 
  if (currentRed != previousRed) {
    do_red = (currentRed == HIGH);
  }
  previousRed = currentRed;
 
  if (currentGreen != previousGreen) {
    do_green = (currentGreen == HIGH);
  }
  previousGreen = currentGreen;
 
  if ((do_red) && (!do_green)) {
    return RED_BUTTON;
  }
 
  if ((!do_red) && (do_green)) {
    return GREEN_BUTTON;
  }
 
  // all other button combinations are ignored
  return MENU;
}
 
 
int RedButton() {
  if (debug) { Serial.println("RedButton()"); }
 
  if (timer_running) {
    return CANCEL_TIMER;
  }
 
  if (++countdowntime > MAXIMUM_TIME) { countdowntime = MINIMUM_TIME; }
 
  lcd.clear();
  lcd.print("Time set to");
  lcd.setCursor(0, 1);
  lcd.print(countdowntime, DEC);
  if (countdowntime == 1) {
    lcd.print(" hour");
  }
  else {
    lcd.print(" hours");
  }
 
  menu_running = false;
 
  return PAUSE;
}
 
 
int GreenButton() {
  if (debug) { Serial.println("GreenButton()"); }
 
  if (timer_running) {
    return CANCEL_TIMER;
  }
 
  lcd.clear();
  lcd.print("Running timer");
  lcd.setCursor(0, 1);
  lcd.print("for ");
  lcd.print(countdowntime, DEC);
  if (countdowntime == 1) {
    lcd.print(" hour");
  }
  else {
    lcd.print(" hours");
  }
 
  unsigned long t = countdowntime * UNITS;
 
  timer_running = true;
  if (debug) {
    Serial.print("Setting metro for ");
    Serial.print(t);
    Serial.println(" seconds");
  }
  relayMetro.interval(t);
 
  digitalWrite(RELAY, HIGH);
 
  return IDLE;
}
 
 
int StopTimer() {
  if (debug) { Serial.println("StopTimer()"); }
 
  digitalWrite(RELAY, LOW);
 
  lcd.clear();
  lcd.print("Timer finished");
 
  timer_running = false;
  menu_running = false;
  relayMetro.reset();
 
  return PAUSE;
}
 
 
int CancelTimer() {
  if (debug) { Serial.println("CancelTimer()"); }
 
  digitalWrite(RELAY, LOW);
 
  lcd.clear();
  lcd.print("Timer stopped");
 
  timer_running = false;
  menu_running = false;
  relayMetro.reset();
 
  return PAUSE;
}
 
 
int Pause() {
  delay(1000);
  return IDLE;
}

Or just download the .pde file here

Obviously you can go all crazy with options and clocks, but I just wanted something quick and working. Changing the code to do all that is just a matter of opening it up, and uploading the new software.




And just to prove it is all working, here’s a small video demonstration. It doesn’t show the timer ending, you just have to take my word on that :D