Skip to content

Instantly share code, notes, and snippets.

@dvberkel
Created May 11, 2012 20:10
Show Gist options
  • Save dvberkel/2662160 to your computer and use it in GitHub Desktop.
Save dvberkel/2662160 to your computer and use it in GitHub Desktop.
Arduino controlled 7-segment clock
const int heartBeatPin = 13;
int heartBeatState = LOW;
long interval = 250;
long previousMillis = 0;
const int segments[10] = {192, 249, 164, 176, 153, 146, 130, 248, 128, 144};
const int bases = 2;
const int base[] = {10, 6};
int timer[] = {0, 0};
int clockPin = 2;
int latchPin = 3;
int dataPin = 4;
void setup() {
pinMode(heartBeatPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
if (timeToToggle()) {
toggleHeartBeat();
increaseTimer();
setSegmentsToTimer(timer);
}
}
boolean timeToToggle() {
long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
return true;
}
return false;
}
void toggleHeartBeat() {
toggleHeartBeatState();
signalHeartBeat();
}
void toggleHeartBeatState() {
if (heartBeatState == LOW) {
heartBeatState = HIGH;
} else {
heartBeatState = LOW;
}
}
void signalHeartBeat() {
digitalWrite(heartBeatPin, heartBeatState);
}
void increaseTimer() {
int index = 0;
increaseTimerSlot(index);
while (isTimerSlotOverflowed(index)) {
resetTimerSlot(index);
increaseTimerSlot(index + 1);
index++;
}
}
void increaseTimerSlot(int index) {
if (0 <= index && index < bases) {
timer[index] = timer[index] + 1;
}
}
boolean isTimerSlotOverflowed(int index) {
return index < bases && timer[index] == base[index];
}
void resetTimerSlot(int index) {
timer[index] = 0;
}
void setSegmentsToTimer(int values[]) {
for (int index = bases - 1; index >= 0; index--) {
setSegmentTo(segments[values[index]]);
}
}
void setSegmentTo(int value) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, value);
digitalWrite(latchPin, HIGH);
}
const int heartBeatPin = 13;
int heartBeatState = LOW;
long interval = 1000;
long previousMillis = 0;
const int segments[10] = {192, 249, 164, 176, 153, 146, 130, 248, 128, 144};
const int bases = 3;
const int base[] = {10, 6, 10};
int timer[] = {0, 0, 1};
int clockPin = 2;
int latchPin = 3;
int dataPin = 4;
int switchPin = 5;
int switchState = LOW;
void setup() {
pinMode(heartBeatPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(switchPin, INPUT);
}
void loop() {
readSwitch();
if (timeToToggle()) {
toggleHeartBeat();
decreaseTimer();
setSegmentsToTimer(timer);
}
}
void readSwitch() {
if (digitalRead(switchPin) == HIGH) {
switchState = HIGH;
}
}
boolean timeToToggle() {
long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
return true;
}
return false;
}
void toggleHeartBeat() {
toggleHeartBeatState();
signalHeartBeat();
}
void toggleHeartBeatState() {
if (heartBeatState == LOW) {
heartBeatState = HIGH;
} else {
heartBeatState = LOW;
}
}
void signalHeartBeat() {
digitalWrite(heartBeatPin, heartBeatState);
}
void increaseTimer() {
if (switched()) {
int index = 0;
increaseTimerSlot(index);
while (isTimerSlotOverflowed(index)) {
resetTimerSlotTo(index, 0);
increaseTimerSlot(index + 1);
index++;
}
}
}
boolean switched() {
return switchState == HIGH;
}
void increaseTimerSlot(int index) {
if (0 <= index && index < bases) {
timer[index] = timer[index] + 1;
}
}
boolean isTimerSlotOverflowed(int index) {
return index < bases && timer[index] == base[index];
}
void resetTimerSlotTo(int index, int value) {
timer[index] = value;
}
void decreaseTimer() {
if (switched()) {
int index = 0;
while (isTimerSlotUnderflowed(index)) {
index++;
}
if (index < bases) {
decreaseTimerSlot(index);
index--;
while (index >= 0) {
resetTimerSlotTo(index, base[index] - 1);
index--;
}
}
}
}
boolean isTimerSlotUnderflowed(int index) {
return index < bases && timer[index] == 0;
}
void decreaseTimerSlot(int index) {
if (0 <= index && index < bases) {
timer[index] = timer[index] - 1;
}
}
void setSegmentsToTimer(int values[]) {
for (int index = bases - 1; index >= 0; index--) {
setSegmentTo(segments[values[index]]);
}
}
void setSegmentTo(int value) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, value);
digitalWrite(latchPin, HIGH);
}
const int heartBeatPin = 13;
int heartBeatState = LOW;
long interval = 500;
long previousMillis = 0;
const int numberOfClockPins = 8;
int clockPins[numberOfClockPins] = {12, 11, 10, 9, 8, 7, 6, 5};
int segments[10][numberOfClockPins] = {
{LOW , LOW , LOW , LOW , LOW , LOW , HIGH, HIGH},
{HIGH, LOW , LOW , HIGH, HIGH, HIGH, HIGH, HIGH},
{LOW , LOW , HIGH, LOW , LOW , HIGH, LOW , HIGH},
{LOW , LOW , LOW , LOW , HIGH, HIGH, LOW , HIGH},
{HIGH, LOW , LOW , HIGH, HIGH, LOW , LOW , HIGH},
{LOW , HIGH, LOW , LOW , HIGH, LOW , LOW , HIGH},
{LOW , HIGH, LOW , LOW , LOW , LOW , LOW , HIGH},
{LOW , LOW , LOW , HIGH, HIGH, HIGH, HIGH, HIGH},
{LOW , LOW , LOW , LOW , LOW , LOW , LOW , HIGH},
{LOW , LOW , LOW , LOW , HIGH, LOW , LOW , HIGH},
};
int count = 0;
void setup() {
pinMode(heartBeatPin, OUTPUT);
for (int index = 0; index < numberOfClockPins; index++) {
pinMode(clockPins[index], OUTPUT);
}
}
void loop() {
if (timeToToggle()) {
toggleHeartBeat();
increaseCount();
setSegmentTo(segments[count]);
}
}
boolean timeToToggle() {
long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
return true;
}
return false;
}
void toggleHeartBeat() {
toggleHeartBeatState();
signalHeartBeat();
}
void toggleHeartBeatState() {
if (heartBeatState == LOW) {
heartBeatState = HIGH;
} else {
heartBeatState = LOW;
}
}
void signalHeartBeat() {
digitalWrite(heartBeatPin, heartBeatState);
}
void increaseCount() {
count++;
if (count == 10) {
count = 0;
}
}
void setSegmentTo(int values[]) {
for (int index = 0; index < numberOfClockPins; index++) {
digitalWrite(clockPins[index], values[index]);
}
}
/*
RegisterDisplay.cpp - Library for 74HC545 controlled 7-segment display
Created by Daan van Berkel, May 30, 2012.
Released into the public domain.
*/
#include "Arduino.h"
#include "RegisterDisplay.h"
RegisterDisplay::RegisterDisplay(int numberOfSegments, int clockPin, int latchPin, int dataPin) {
_segmentData = {192, 249, 164, 176, 153, 146, 130, 248, 128, 144};
_numberOfSegments = numberOfSegments;
_clockPin = clockPin;
_latchPin = latchPin;
_dataPin = dataPin;
pinMode(_clockPin, OUTPUT);
pinMode(_latchPin, OUTPUT);
pinMode(_latchPin, OUTPUT);
};
void RegisterDisplay::setSegmentsTo(int data[]) {
for (int index = _numberOfSegments - 1; index >= 0; index--) {
setSegmentTo(data[index]);
}
}
void RegisterDisplay::setSegmentTo(int data) {
digitalWrite(_latchPin, LOW);
shiftOut(_dataPin, _clockPin, MSBFIRST, _segmentData[data]);
digitalWrite(_latchPin, HIGH);
}
/*
RegisterDisplay.h - Library for 74HC545 controlled 7-segment display
Created by Daan van Berkel, May 30, 2012.
Released into the public domain.
*/
#ifndef RegisterDisplay_h
#define RegisterDisplay_h
#include "Arduino.h"
class RegisterDisplay {
public:
RegisterDisplay(int numberOfSegments, int clockPin, int latchPin, int dataPin);
void setSegmentsTo(int data[]);
private:
void setSegmentTo(int data);
int _segmentData[10];
int _numberOfSegments;
int _clockPin;
int _latchPin;
int _dataPin;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment