Hello,
I am really new to coding and honestly have no idea what i am doing so i really need help xD
I want to program 2 timers:
- timer 1 indicated that the location is sent, this needs to reset itself every 5 minutes.
- timer 2 is the game timer, after the time has ended the timer has to stay on 00:00 and also stops timer 1
(they should start at the same time)
I am having troubles finding out how to reset timer 1 every time it hits 00:00, the countdown works but it weirdly start counting down from 150 after it hits 00:00
Can someone tell me how to solve this?
I also want to implement a ''start up button'', so this has to initiate both timers, any idea on how to do this?
CODE:
/**
- Project countdown timer
- Component: display, button(on pin 3D(from ground pin)+ and between - a resitor brown,black,red,gold),
- Led (on pin 9, with red-red-brown-gold resistor between D9) AND BLE-Nano
- Wiring display: vcc -> + ;GND -> -; SCL -> A5; SDA -> A4
*/
#include <U8g2lib.h> //OLED display
#define COUNTDOWN_TIME_HIDER 5000ul //countdown timer in miliseconds (300 000 ->5min)
#define COUNTDOWN_TIME_GAME 10000ul //countdown timer in miliseconds (1200 000 ->20min)
char
szString[20];
byte
//first timer
mins, secs,
//second timer
mins2, secs2;
//saving vairables of millis function
unsigned long
timeTemp,
timeNow,
timeStart,
timeElapsed,
timeLeft,
//for second timer
timeTemp2,
timeNow2,
timeStart2,
timeElapsed2,
timeLeft2;
// display setup:
// software i2c:
//U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(7, 8, U8X8_PIN_NONE);
// hardware i2c:
U8X8_SSD1306_128X64_NONAME_HW_I2C display(U8X8_PIN_NONE);
char buf[10]; // text buffer; to be able to use drawString to show the value
//LED pin
int ledPin = 9;
//timer
int S = 59; // count seconds
int M = 59; // count minutes
void setup() { //setup code here, to run once:
// initialize display:
display.begin();
display.setPowerSave(0);
display.setFont(u8x8_font_pxplusibmcgathin_f);
display.drawString(0,0,"Location send in:");
display.drawString(0,3,"Game ends in:");
//initialize the communication
Serial.begin(9600);
timeStart = millis(); //millis counts the arduino ticks (so time in milli seconds when the arduino is started up)
mins = 1;
secs = 1;
//second timer
mins2 = 1;
secs2 = 1;
//LED pin
pinMode(ledPin, OUTPUT);
//Button pin
int buttonPin = 3;
pinMode(buttonPin, INPUT_PULLUP);
}
void DoCountdown()
{
static unsigned long
lastTimeNow = 0;
static byte
lastsecs = 1;
timeNow = millis();
timeElapsed = timeNow - timeStart;
timeLeft = COUNTDOWN_TIME_HIDER - timeElapsed;
mins = (byte)(timeLeft / 60000ul);
timeTemp = timeLeft - (mins * 60000);
secs = (byte)(timeTemp / 1000ul);
timeTemp = timeTemp - (secs * 1000ul);
if( mins == 0 && secs == 0 )
{
//if timer end the led glows and stays on for 2 seconds
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
}
else if( secs != lastsecs )
{
lastsecs = secs;
sprintf( szString, "%02d:%02d", mins, secs );
Serial.println( szString );
//display the hider timer
display.setCursor( 0, 2); //on the second row
display.print( szString );
display.display();
}
//timer 2 for the GAME TIMER
static unsigned long
lastTimeNow2 = 0;
static byte
lastsecs2 = 1;
timeNow2 = millis();
timeElapsed2 = timeNow2 - timeStart2;
timeLeft2 = COUNTDOWN_TIME_GAME - timeElapsed2;
mins2 = (byte)(timeLeft2 / 60000ul);
timeTemp2 = timeLeft2 - (mins2 * 60000);
secs2 = (byte)(timeTemp2 / 1000ul);
timeTemp2 = timeTemp2 - (secs2 * 1000ul);
if( mins2 == 0 && secs2 == 0 )
{
//if game timer ends the led glows stays on indicating the game has ended
digitalWrite(ledPin, HIGH);
}
else if( secs2 != lastsecs2 )
{
lastsecs2 = secs2;
sprintf( szString, "%02d:%02d", mins2, secs2 );
Serial.println( szString );
//display the game timer
display.setCursor( 0, 4); //on the second row
display.print( szString );
display.display();
}
}
void loop() { // put your main code here, to run repeatedly:
DoCountdown();
}