Skip to content

Instantly share code, notes, and snippets.

@pfsq
Last active December 25, 2015 08:59
Show Gist options
  • Save pfsq/6951129 to your computer and use it in GitHub Desktop.
Save pfsq/6951129 to your computer and use it in GitHub Desktop.
Display current time on a 7segment/4digit display with Arduino and Python.
/*
Original code:
11-02-2012
Spark Fun Electronics
Nathan Seidle
Adapted by:
12-10-2013
Pablo Fernández
This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
For more information about the commands, be sure to visit:
http://github.com/sparkfun/Serial7SegmentDisplay/wiki/Special-Commands
To get this code to work, attached an OpenSegment to an Arduino Uno using the following pins:
Pin 10 on Uno (CS) to CS on OpenSegment
Pin 11 to MOSI
Pin 12 to MISO
Pin 13 to SCK
VIN to PWR
GND to GND
*/
#include <SPI.h>
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int csPin = 10; //You can use any IO pin but for this example we use 10
long value = 0;
void setup()
{
pinMode(csPin, OUTPUT);
digitalWrite(csPin, HIGH); //By default, don't be selecting OpenSegment
Serial.begin(9600); //Start serial communication at 9600 for debug statements
inputString.reserve(20);
SPI.begin(); //Start the SPI hardware
SPI.setClockDivider(SPI_CLOCK_DIV64); //Slow down the master a bit
//Send the reset command to the display - this forces the cursor to return to the beginning of the display
digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment
SPI.transfer('v'); //Reset command
}
void loop()
{
if (stringComplete) {
value = inputString.toInt();
spiSendTime(value); //Send the four characters to the display
delay(1); //If we remove the slow debug statements, we need a very small delay to prevent flickering
// clear the string:
inputString = "";
stringComplete = false;
}
}
//Given a number, spiSendValue chops up an integer into four values and sends them out over spi
void spiSendTime(long time) {
digitalWrite(csPin, LOW); //Drive the CS pin low to select OpenSegment
SPI.transfer(time / 100000); //Send the left most digit
time %= 100000; //Now remove the left most digit from the number we want to display
SPI.transfer(time / 10000);
time %= 10000;
SPI.transfer(time / 1000);
time %= 1000;
SPI.transfer(time / 100); //Send the right most digit
if (time%2 == 0) {
SPI.transfer(0x77);
SPI.transfer(0x10);
} else {
SPI.transfer(0x77);
SPI.transfer(0x00);
}
digitalWrite(csPin, HIGH); //Release the CS pin to de-select OpenSegment
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// it the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
#!/usr/bin/env python
def main():
import serial
from time import strftime, sleep
from sys import platform as _platform
# Open serial port
if _platform == 'linux' or _platform == 'linux2':
ser = serial.Serial(port='/dev/ttyUSB0',baudrate=9600)
elif _platform == 'win32':
ser = serial.Serial(port='COM4',baudrate=9600)
# Loop
time0 = 0
while ser.isOpen():
try:
time1 = strftime("%H%M%S\n")
if time1 != time0:
ser.write(time1)
time0 = time1
sleep(0.98)
except (KeyboardInterrupt, SystemExit):
ser.close()
break
if __name__ == "__main__":
from sys import exit
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment