Skip to content

Instantly share code, notes, and snippets.

@alairock
Created July 17, 2020 19:49
Show Gist options
  • Save alairock/e5dbf294eb5502e8edffec68efb6e766 to your computer and use it in GitHub Desktop.
Save alairock/e5dbf294eb5502e8edffec68efb6e766 to your computer and use it in GitHub Desktop.
This is the code for a single button, debounced, pulse/variable-clock combo.
int RXLED = 17;
int TXLED = 30;
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 16; // the number of the pushbutton pin
const int ledPin = 10; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long ledPulseStartTime = 0;
unsigned long ledPulseLength = 100;
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
unsigned long modeSwitchTime = 2000;
unsigned long buttonPressStartTime = 0;
unsigned long ledPulseInterval = 1000;
unsigned long autoIntervalLedStartTime = 0;
bool releasedButtonForManualMode = true;
bool manualMode = true;
bool autoIntervalLedOn = false;
bool shouldChangeAutoInterval = false;
bool didChangeAutoInterval = false;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(RXLED, OUTPUT); // We are going to use these LEDs for user feedback
pinMode(TXLED, OUTPUT); // We are going to use these LEDs for user feedback
// set initial LED state
digitalWrite(ledPin, LOW);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
if (reading == LOW) {
// We are not hloding down the button, this is an easier way of managing the state, even if less efficient
buttonPressStartTime = millis();
releasedButtonForManualMode = true;
}
if ((millis() - buttonPressStartTime) > modeSwitchTime && releasedButtonForManualMode) {
Serial.println("You held the button for two seconds"); // Now lets flash the
digitalWrite(RXLED, HIGH); // set the LED off
digitalWrite(TXLED, HIGH); // set the LED off
delay(50);
digitalWrite(RXLED, LOW); // set the LED off
digitalWrite(TXLED, LOW); // set the LED off
delay(50);
digitalWrite(RXLED, HIGH); // set the LED off
digitalWrite(TXLED, HIGH); // set the LED off
delay(50);
digitalWrite(RXLED, LOW); // set the LED off
digitalWrite(TXLED, LOW); // set the LED off
delay(50);
digitalWrite(RXLED, HIGH); // set the LED off
digitalWrite(TXLED, HIGH); // set the LED off
releasedButtonForManualMode = false;
manualMode = !manualMode;
}
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), 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:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is H IGH
if (buttonState == HIGH) {
ledPulseStartTime = millis();
ledState = !ledState;
}
}
}
// set the LED:
if ((millis() - ledPulseLength) > ledPulseStartTime) {
if (!didChangeAutoInterval) shouldChangeAutoInterval = true;
digitalWrite(RXLED, HIGH);
digitalWrite(TXLED, HIGH);
if (manualMode) digitalWrite(ledPin, LOW);
} else {
shouldChangeAutoInterval = false;
didChangeAutoInterval = false;
digitalWrite(RXLED, LOW);
digitalWrite(TXLED, LOW);
if (manualMode) digitalWrite(ledPin, HIGH);
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
if (!manualMode) {
if (shouldChangeAutoInterval) {
Serial.print("Changing interval to ");
Serial.println(ledPulseInterval);
ledPulseInterval = ledPulseInterval / 2;
if (ledPulseInterval < 10) ledPulseInterval = 2000;
didChangeAutoInterval = true;
shouldChangeAutoInterval = false;
}
digitalWrite(ledPin, autoIntervalLedOn ? HIGH : LOW);
if (autoIntervalLedOn) {
if ((millis() - autoIntervalLedStartTime) > ledPulseLength) {
autoIntervalLedOn = false;
autoIntervalLedStartTime = millis();
}
} else {
if ((millis() - autoIntervalLedStartTime) > ledPulseInterval) {
autoIntervalLedOn = true;
autoIntervalLedStartTime = millis();
}
}
}
}
@alairock
Copy link
Author

This is a clock module for 8-bit/6502 breadboard "computers". This should be a valid drop-in alternative for a module like this one. This project is inspired by the breadboard computer tutorials by Ben Eater.

This uses a ProMicro/Leonardo.
Pin 10 - Output signal
Pin 16 - Button/Momentary switch

Usage:

Press the button once to send a debounced pulse.

Press and hold button for two seconds to switch to clock mode. Onboard LED's should pulse a few times.

When in clock mode, press the button to increase the speed. Initial speed is 1000ms followed by 1/2 of previous interval speed for each pulse, until 10ms, then it jumps up to 2000ms, and repeats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment