diff --git a/.vscode/arduino.json b/.vscode/arduino.json new file mode 100644 index 0000000..240bdd8 --- /dev/null +++ b/.vscode/arduino.json @@ -0,0 +1,5 @@ +{ + "sketch": "FallingTarget.ino", + "board": "arduino:avr:leonardo", + "port": "/dev/ttyACM0" +} \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..01e9df3 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -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 +} \ No newline at end of file diff --git a/FallingTarget.ino b/FallingTarget.ino new file mode 100644 index 0000000..7b08139 --- /dev/null +++ b/FallingTarget.ino @@ -0,0 +1,93 @@ +#include + +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); + } +}