Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Save early. Save often.
/**
* Save Reminder
* Date: February 1, 2018
* Author: Shawn Hymel (SparkFun Electronics)
*
* Connect to an open USB port on your computer. Every 5 minutes,
* the LED will begin flashing. Press the button to send a ctl+s
* or cmd+s key press to your computer to save your work.
*
* Pick your operating system by updating the OS variable.
*
* Laser cutting files for acrylic base can be found in
* save_early_laser_cut_stand.zip
*
* Components:
* - SparkFun Pro Micro (5V/16MHz)
* - NovelKeys Big Switch
* - 10mm LED
* - 220 Ohm resistor
* - Wire
*
* Connections
* - Button to pin 2
* - LED to pin 9
*
* This sketch was written by SparkFun Electronics, with lots of
* help from the Arduino community. This code is completely free
* for any use.
*/
#include "Keyboard.h"
// OS parameters
typedef enum {
LINUX,
WINDOWS,
MAC
} os_types;
// Change this to your operating system
const os_types OS = WINDOWS;
// Pins
const int btn_pin = 2;
const int led_pin = 9;
// Constants
const int debounce_delay = 50; // ms
const int led_delay = 500; // ms
const unsigned long notify_delay = 300000; // ms
// Globals
int btn_state = HIGH;
int btn_prev = HIGH;
unsigned long last_debounce_time = 0;
int os_ctrl;
int led_state = LOW;
unsigned long led_timestamp = 0;
unsigned long notify_timestamp = 0;
bool flashing = true;
void setup() {
// Set up LED and button pins
pinMode(btn_pin, INPUT_PULLUP); // Set the button as an input
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, LOW);
// Begin keyboard
Keyboard.begin();
// Switch to correct control/command key
switch(OS){
case LINUX:
case WINDOWS:
os_ctrl = KEY_LEFT_CTRL;
break;
case MAC:
os_ctrl = KEY_LEFT_GUI;
break;
default:
os_ctrl = KEY_LEFT_CTRL;
break;
}
// Get initial timestamp
led_timestamp = millis();
notify_timestamp = millis();
}
void loop() {
// Read current state of the button
int btn_read = digitalRead(btn_pin);
// Remember when the button changed states
if ( btn_read != btn_prev ) {
last_debounce_time = millis();
}
// Wait before checking the state of the button again
if ( millis() > (last_debounce_time + debounce_delay) ) {
if ( btn_read != btn_state ) {
btn_state = btn_read;
if ( btn_state == LOW ) {
// Send ctrl+s or cmd+s
Keyboard.press(os_ctrl);
Keyboard.press('s');
delay(100);
Keyboard.releaseAll();
// Stop the flashing!
flashing = false;
notify_timestamp = millis();
led_state = LOW;
digitalWrite(led_pin, led_state);
}
}
}
// Remember the previous button position for next loop()
btn_prev = btn_read;
// Change state of LED if enough time has passed
if ( flashing ) {
if ( millis() > led_timestamp + led_delay ) {
led_state = !led_state;
digitalWrite(led_pin, led_state);
led_timestamp = millis();
}
}
// Set LED to flash again if enough time has passed
if ( millis() > notify_timestamp + notify_delay ) {
flashing = true;
notify_timestamp = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.