#include <TM1637.h>
//Debug
const bool enableDebug = false;
//Sound
const int SOUND_Pin=23;
//IOs
const int vibr_pin=12;
const int startButton=13;
//LED
const int pinR = 27;
const int pinG = 26;
const int pinB = 25;
//Display
const int CLK = 2;
const int DIO = 4;
TM1637 tm(CLK, DIO);
//PrecedureProperties
bool readyToShoot = false;
long startTime = 0;
void displayTime(long milliseconds){
long seconds = milliseconds / 1000;
long mseconds = (milliseconds % 1000) / 10;
tm.point(1);
tm.display(3, mseconds % 10);
tm.display(2, mseconds / 10 % 10);
tm.display(1, seconds % 10);
tm.display(0, seconds / 10 % 10);
}
void WriteLED(bool red, bool green, bool blue)
{
digitalWrite(pinR, red);
digitalWrite(pinG, green);
digitalWrite(pinB, blue);
}
void TargetHitProcedure()
{
long currentTime = millis();
if(enableDebug)
Serial.println("Target hit detected...");
tone(SOUND_Pin,262,750);
readyToShoot = false;
long timeToDisplay = currentTime - startTime;
displayTime(timeToDisplay);
if (enableDebug)
Serial.println(timeToDisplay);
WriteLED(true, false, false);
delay(500);
WriteLED(false, false, false);
delay(500);
WriteLED(true, false, false);
delay(500);
WriteLED(false, false, false);
delay(500);
WriteLED(true, false, false);
delay(500);
WriteLED(false, false, false);
delay(500);
WriteLED(false, true, false);
}
void setup() {
// put your setup code here, to run once:
if (enableDebug)
Serial.begin(9600);
//Digital IO
pinMode(vibr_pin, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
//LEDs
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
//7-Segmented
tm.init();
tm.set(BRIGHT_TYPICAL);
//Prepare
displayTime(0);
WriteLED(false, true, false);
if(enableDebug)
Serial.println("Starting Programm");
}
void loop() {
// put your main code here, to run repeatedly:
int targetHit;
int startButtonPressed;
startButtonPressed = digitalRead(startButton);
if(startButtonPressed==LOW && readyToShoot == false)
{
if (enableDebug)
Serial.println("Start Button was pressed...");
readyToShoot = true;
startTime = millis();
}
targetHit=digitalRead(vibr_pin);
if(targetHit==LOW && readyToShoot==true)
{
TargetHitProcedure();
}
if(readyToShoot==true){
//Display running Timer
long readTime = millis();
long runningTimer = readTime - startTime;
displayTime(runningTimer);
if(runningTimer % 1000 > 500)
{
WriteLED(false, false, true);
}
else
{
WriteLED(false, false, false);
}
}
}