#include "DFRobotDFPlayerMini.h"
#include "TM1637Display.h"
#include "SoftwareSerial.h"
/* Digital Pins to TM1637 */
#define CLK 5
#define DIO 4
TM1637Display display(CLK, DIO);
DFRobotDFPlayerMini myDFPlayer;
SoftwareSerial mySerial(8,9);
int lastLoopTime = 0;
const int buttonPin = 7; //Arm button
const int defusePin = 6; //Defuse button
const int LEDPin = 13; //Arming LED
const int BoomPin = 12; //the bomb
int buttonState = 0; // current state of the arm button
int lastButtonState = 0; // previous state of the arm button
int defuseState = 0; // current state of the defuse button
int lastdefuseState = 0; // previous state of the defuse button
unsigned long startPressed = 0; // the moment the arm button was pressed
unsigned long endPressed = 0; // the moment the arm button was released
unsigned long holdTime = 0; // how long the arm button was held
unsigned long idleTime = 0; // how long the arm button was idle
unsigned long DstartPressed = 0; // the moment the disarm button was pressed
unsigned long DendPressed = 0; // the moment the disarm button was released
unsigned long DholdTime = 0; // how long the disarm button was held
unsigned long DidleTime = 0; // how long the disarm button was idle
int timerState = 0; // Timer on/off
unsigned long timerTime = 0; // Timer on time
unsigned long timerStart = 0; // Timer start time
long countDown = 0;
unsigned long counter = 0;
uint8_t data[] = {0, 0, 0, 0};
unsigned long timerLength = 300000; //timer length, should be 300000 for 5 minute timer
unsigned long noSeconds=timerLength/1000;
unsigned long runHours= noSeconds/3600;
unsigned long afterSecs=noSeconds%3600;
unsigned long stopMinutes=afterSecs/60;
unsigned long stopSeconds=afterSecs%60;
void setup() {
pinMode(buttonPin, INPUT); // initialize the button pin as a input
pinMode(defusePin, INPUT); // Initialize defuse button pin as input
pinMode(LEDPin, OUTPUT); // Set Arming LED as output
pinMode (BoomPin, OUTPUT);// Set explosion pin as output
digitalWrite(LEDPin, LOW);
digitalWrite (BoomPin, LOW); //Set initial state as off for above pins
display.setBrightness(5);
Serial.begin(9600);
display.showNumberDecEx(stopSeconds, 0x00, true, 2, 2);
display.showNumberDecEx(stopMinutes, 0x40, true, 2, 0);
//myDFPlayer.volume (30);
}
void loop() {
buttonState = digitalRead(buttonPin); // read the button input
if (buttonState != lastButtonState) {
updateState(); // button state changed. It runs only once.
} else {
updateCounter(); // button state not changed. It runs in a loop.
}
lastdefuseState = defuseState; // save state for next loop
defuseState = digitalRead(defusePin); // read the button input
if (defuseState != lastdefuseState) {
dupdateState(); // button state changed. It runs only once.
} else {
dupdateCounter(); // button state not changed. It runs in a loop.
}
lastButtonState = buttonState; // save state for next loop
if (timerState == 1){
TimerCount ();
}
/*while(timerState == 1){
myDFPlayer.loop(1); //MP3 0001 must be armed alarm
}*/}
void updateState() {
// the arm button has been just pressed
if ((buttonState == HIGH) && (timerState == 0)) {
startPressed = millis();
}}
void updateCounter() {
// the arm button is still pressed
if (buttonState == HIGH) {
holdTime = millis() - startPressed;
if ((holdTime >= 1000) && (timerState !=1)) {
digitalWrite (LEDPin, HIGH);
timerState = 1;
timerStart = millis();
counter = timerLength; //should be 30000 for 5 minutes
holdTime = 0;
}}}
void dupdateState() {
// the defuse button has been just pressed
if ((defuseState == HIGH) && (timerState == 1)) {
DstartPressed = millis();
}}
void dupdateCounter() {
// the defuse button is still pressed
if ((defuseState == HIGH) && (timerState == 1)) {
DholdTime = millis() - DstartPressed;
if ((DholdTime >= 1000) && (timerState == 1)) {
digitalWrite (LEDPin, LOW);
timerState = 0;
counter = timerLength; //should be 30000 for 5 minutes
display.showNumberDecEx(stopSeconds, 0x00, true, 2, 2);
display.showNumberDecEx(stopMinutes, 0x40, true, 2, 0);
}}}
void TimerCount(){ //Countdown to fucksplosion
if ((timerState == 1)){
timerTime = millis() - timerStart; //counts time since start up
countDown = counter - timerTime; //counts down from pre-set time
//below turns millis into seconds and minutes
unsigned long allSeconds=countDown/1000;
unsigned long runHours= allSeconds/3600;
unsigned long secsRemaining=allSeconds%3600;
unsigned long runMinutes=secsRemaining/60;
unsigned long runSeconds=secsRemaining%60;
if (timerState == 1){
display.showNumberDecEx(runSeconds, 0x00, true, 2, 2);
display.showNumberDecEx(runMinutes, 0x40, true, 2, 0);
if (timerTime >= timerLength){ //Timer according to timerLength
Boom();
timerState = 0;
}}
}}
void Boom(){
digitalWrite (BoomPin, HIGH); //Yes, Rico. Boom.
myDFPlayer.play(2); //0002 must be explosion sound
timerState = 0;
}