Skip to content

Instantly share code, notes, and snippets.

@shapr
Created June 7, 2013 13:38
Show Gist options
  • Save shapr/5729319 to your computer and use it in GitHub Desktop.
Save shapr/5729319 to your computer and use it in GitHub Desktop.
Arduino sketch to ask for how many seconds to delay, for my spouse-safe wrist alarm, first working version, don't expect beauty
int powerPin = 17;
int groundPin = 19;
int failcount = 0;
char buffer[18];
void setup() {
Serial.begin(9600);
Serial.flush();
pinMode(powerPin,OUTPUT);
pinMode(groundPin,OUTPUT);
};
void loop() {
if (Serial.available() > 0) {
int index=0;
delay(100); // let the buffer fill up
int numChar = Serial.available();
if (numChar>15) {
numChar=15;
}
while (numChar--) {
buffer[index++] = Serial.read();
}
splitString(buffer);
} else { // no input, snooze for five minutes
failcount++;
delay(1000); // wait one second
if (failcount > 30) { // after thirty seconds looking for serial input once per second
// start the five minute delay
delay(1000 * 60 * 5);
while(1){
buzz();
}
}
}
}
void splitString(char* data) {
Serial.print("Data entered: ");
Serial.println(data);
unsigned long Ans = strtol(data, NULL, 10);
Ans = constrain(Ans,0,30000);
Serial.print("Delaying ");
Serial.print(Ans);
Serial.println(" seconds.");
Serial.print("going to delay milliseconds: ");
Ans = Ans * 1000;
Serial.println(Ans);
delay(Ans);
while(1){
buzz();
}
// Clear the text and serial buffers
for (int x=0; x<16; x++) {
buffer[x]='\0';
}
Serial.flush();
}
void buzz() {
int amount = 500;
delay(amount);
digitalWrite(powerPin,HIGH);
delay(amount);
digitalWrite(powerPin,LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment