Skip to content

Instantly share code, notes, and snippets.

@htruong
Last active April 14, 2016 03:09
Show Gist options
  • Save htruong/bed170c71983dfcc7c0968174aaed8e2 to your computer and use it in GitHub Desktop.
Save htruong/bed170c71983dfcc7c0968174aaed8e2 to your computer and use it in GitHub Desktop.
/*
Button / SolderingIronBOff
Automatically turns off soldering station.
Turns on the power switch tail (also the LED)
connected to digital pin 1.6,
when pressing a pushbutton attached to pin 2.
Automatically times out when it reaches a threshold.
Pressing it again resets the timer.
A warning LED on Pin 1.0 is lit when
we have < 20% time left.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
modified Apr 27 2012
by Robert Wessels
modified Apr 12 2016
by Huan Truong
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = PUSH2; // the number of the pushbutton pin
const int powerSwitchPin = GREEN_LED; // the number of the pwer switch tail
const int warningLedPin = RED_LED; // the number of the warning LED
const unsigned long maxTimeOut = 1000UL * 60UL * 15UL;
const unsigned long warningMaxTimeOut = maxTimeOut / 10UL * 8UL;
volatile int buttonState; // the current reading from the input pin
volatile int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
volatile unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
volatile unsigned long debounceDelay = 5; // the debounce time; increase if the output flickers
volatile unsigned long timeOut = 0;
volatile unsigned long warningTimeOut = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(powerSwitchPin, OUTPUT);
digitalWrite(powerSwitchPin, LOW);
pinMode(warningLedPin, OUTPUT);
digitalWrite(warningLedPin, LOW);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
//Serial.begin(9600);
//Serial.println("Soldering station auto-off 1.0 - Written by Huan Truong 2016");
}
void loop(){
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from HIGH to LOW), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
// check if the pushbutton is pressed.
// if it is, the buttonState is LOW
if (buttonState == LOW) {
// turn LED on:
digitalWrite(powerSwitchPin, HIGH);
digitalWrite(warningLedPin , LOW);
// set the timeout to be a point in future
timeOut = millis() + maxTimeOut;
warningTimeOut = millis() + warningMaxTimeOut;
//Serial.println("Turned the soldering station on!");
}
}
if (millis() > timeOut) {
// Turn station off if timeout
digitalWrite(powerSwitchPin, LOW);
digitalWrite(warningLedPin , LOW);
} else if (millis() > warningTimeOut) {
// Turn warning LED off if we have less than 20% remaining time.
digitalWrite(warningLedPin , HIGH);
}
lastButtonState = reading;
//Serial.print("Time left: ");
//Serial.println(timeOut);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment