Skip to content

Instantly share code, notes, and snippets.

@splbio
Created January 5, 2013 10:33
Show Gist options
  • Save splbio/4460925 to your computer and use it in GitHub Desktop.
Save splbio/4460925 to your computer and use it in GitHub Desktop.
/*-
* Copyright (c) 2012 Alfred Perlstein <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int onboardLed = 13;
int powerPin = 4;
int powerState = 0;
int resetPin = 3;
int resetState = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(onboardLed, OUTPUT);
pinMode(powerPin, OUTPUT);
pinMode(resetPin, OUTPUT);
}
void
pushPower()
{
Serial.println("Holding down power button");
digitalWrite(powerPin, HIGH);
powerState = 1;
}
void
releasePower()
{
digitalWrite(powerPin, LOW);
Serial.println("Released power button");
powerState = 0;
}
void
pushReset()
{
Serial.println("Holding down reset button");
digitalWrite(resetPin, HIGH);
resetState = 1;
}
void
releaseReset()
{
digitalWrite(resetPin, LOW);
Serial.println("Released reset button");
resetState = 0;
}
void serialHelp()
{
Serial.println("Usage: \n"
"P(hard power off), p(soft power off / power off)\n"
"R(hit reset button)\n"
"Ss(status)\n"
"1(toggle power), 2(toggle reset)\n"
);
}
// the loop routine runs over and over again forever:
void loop() {
top:
if (Serial.available() > 0) {
int incomingByte;
// read the incoming byte:
incomingByte = Serial.read();
// Serial.print("Processing byte.. value: ");
// Serial.println(incomingByte);
switch (incomingByte) {
case '1':
if (powerState)
releasePower();
else
pushPower();
break;
case '2':
if (resetState)
releaseReset();
else
pushReset();
break;
case 'P':
if (powerState) {
Serial.println("Button is already pushed, use 1 to toggle");
serialHelp();
break;
}
Serial.println("Performing hard power off");
pushPower();
delay(8000);
releasePower();
break;
case 'p':
if (powerState) {
Serial.println("Button is already pushed, use 1 to toggle");
serialHelp();
break;
}
Serial.println("Performing soft power off");
pushPower();
delay(250);
releasePower();
break;
case 'R':
if (resetState) {
Serial.println("Button is already pushed, use 2 to toggle");
serialHelp();
break;
}
Serial.println("Performing reset");
pushReset();
delay(250);
releaseReset();
break;
case 's':
case 'S':
Serial.print("Status: power:");
Serial.print(powerState);
Serial.print(", reset:");
Serial.println(resetState);
break;
default:
serialHelp();
}
delay(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment