Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Troels L Strand committed Jul 18, 2019
0 parents commit 85c064d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sketch": "FallingTarget.ino",
"board": "arduino:avr:leonardo",
"port": "/dev/ttyACM0"
}
20 changes: 20 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"/home/troels/Arduino/libraries/TM1637-1.2.0",
"/opt/arduino-1.8.9/tools/**",
"/opt/arduino-1.8.9/hardware/arduino/avr/**"
],
"forcedInclude": [
"/opt/arduino-1.8.9/hardware/arduino/avr/cores/arduino/Arduino.h"
],
"intelliSenseMode": "gcc-x64",
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
93 changes: 93 additions & 0 deletions FallingTarget.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <TM1637Display.h>

int button[] = {4, 5};
// int button[] = {5 ,6, 7, 8};

int buttonSize = sizeof(button) / sizeof(int);

unsigned long startTime;
unsigned long stopTime;
unsigned long currentTime;

#define CLK 2
#define DIO 3

void displayTime(long time, TM1637Display display, bool show100 = false, bool alwaysColon = false)
{
uint8_t digit1 = time / 100000 ? display.encodeDigit((time / 100000) % 10) : 0x0;
uint8_t digit2 = time / 10000 ? display.encodeDigit((time / 10000) % 10) : 0x0;
uint8_t digit3 = time / 1000 ? display.encodeDigit((time / 1000) % 10) : 0x0;
uint8_t digit4 = time / 100 ? display.encodeDigit(time / 100 % 10) : 0x0;

if (time < 9999)
{
digit1 = time / 1000 ? display.encodeDigit((time / 1000) % 10) : digit1;
if ((time / 100))
{
digit2 = display.encodeDigit((time / 100) % 10);
// Colon every half because it looks better.
digit2 = (((time / 10) % 10) >= 5 || alwaysColon) ? 0x80 | digit2 : digit2;
}

digit3 = display.encodeDigit((time / 10) % 10);
digit4 = show100 ? display.encodeDigit(time % 10) : 0x0;
}
// We won't show number of more than 9999 seconds.
if (time > 999999)
{
digit1 = digit2 = digit3 = digit4 = 0b01000000;
}

int8_t data[] = {digit1, digit2, digit3, digit4};
display.setSegments(data);
}

TM1637Display display(CLK, DIO);

void setup()
{
Serial.begin(9600);
startTime = millis();
display.setBrightness(3);

// Set the trigger pins.
for (int i = 0; i < buttonSize; i++)
{
pinMode(button[i], INPUT_PULLUP);
}
}

int programmerPin = 8;
int programmerPinPresses = 0;

void loop()
{
currentTime = millis();
int pressed = 0;
for (int i = 0; i < buttonSize; i++)
{
pressed += digitalRead(button[i]) == LOW;
}
if (!stopTime && pressed == buttonSize)
{
stopTime = millis();
}
if (stopTime)
{
displayTime((stopTime - startTime) / 10, display, true, true);
}
else if (startTime)
{
displayTime((millis() - startTime) / 10, display);
}
if (!startTime && pressed > 0)
{
startTime = millis();
}
// Reset the number when all buttons are released.
if (pressed == 0)
{
startTime = stopTime = NULL;
displayTime(0, display);
}
}

0 comments on commit 85c064d

Please sign in to comment.