Skip to content

Instantly share code, notes, and snippets.

@shrmnk
Created February 14, 2016 07:09
Show Gist options
  • Save shrmnk/f992c08afda33039b7c8 to your computer and use it in GitHub Desktop.
Save shrmnk/f992c08afda33039b7c8 to your computer and use it in GitHub Desktop.
Toggles a relay every 2 seconds (adjustable). Requires DFRobot LCD Shield. Up/Down changes delay by 500ms, Right resets to 2s. Select to Pause.
/*
RelayTest
Toggles a relay every 2 seconds (adjustable)
Values adjustable by the DFRobot LCD Shield's onboard buttons.
Each Up press increases the delay by 0.5s, and down does the reverse.
Right resets the delay to the default of 2s.
See RELAY_PIN and STARTING_DELAY to change configuration
modified 14 Feb 2016
by ShrmnK
*/
#include <LiquidCrystal.h>
#include <Math.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel
#define LED_PIN 13
#define RELAY_PIN 2
#define STARTING_DELAY 2000
#define PAUSED_ON_START true
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int lcd_key = 0;
int adc_key_in = 0;
unsigned int current_delay = STARTING_DELAY;
bool relay_on = false;
unsigned int previous_key = 0;
bool is_paused = PAUSED_ON_START;
unsigned long last_toggle = 0;
unsigned int times_toggled = 0;
int read_LCD_buttons() { // read the buttons
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
// We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in > 1000) return btnNONE;
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
// For V1.0 comment the other threshold and use the one below:
/*
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
*/
return btnNONE; // when all others fail, return this.
}
void updateCount() {
lcd.setCursor(0, 0);
lcd.print("Toggled: ");
lcd.print(++times_toggled);
lcd.print(" ");
}
void setup() {
lcd.begin(16, 2); // start the library
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
last_toggle = millis();
lcd.setCursor(0, 0);
lcd.print("Relay on Pin ");
lcd.print(RELAY_PIN);
lcd.print(": ");
if (PAUSED_ON_START) {
lcd.setCursor(0, 1);
lcd.print("SELECT to start");
}
}
void loop() {
lcd_key = read_LCD_buttons(); // read the buttons
if (lcd_key != previous_key) {
previous_key = lcd_key;
if (lcd_key == btnSELECT) {
is_paused = !is_paused;
lcd.setCursor(0, 1); // move to the begining of the second line
lcd.print(is_paused ? "PAUSED " : "RELAY TESTING");
}
else if (lcd_key == btnUP) {
current_delay += 500;
} else if (lcd_key == btnDOWN) {
if(current_delay >= 1000)
current_delay -= 500;
} else if (lcd_key == btnRIGHT) {
current_delay = 2000;
}
}
if (!is_paused) {
if (millis() - last_toggle >= current_delay) {
last_toggle = millis();
if (relay_on) {
digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW
digitalWrite(RELAY_PIN, LOW); // Switch Relay to OFF
} else {
digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(RELAY_PIN, HIGH); // Switch Relay to ON
}
updateCount();
relay_on = !relay_on;
} else {
lcd.setCursor(0, 1); // move to the begining of the second line
lcd.print(relay_on ? "ON" : "OFF");
lcd.print(" (-");
lcd.print(round((current_delay - (millis() - last_toggle))/100));
lcd.print(") ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment