Hello all,
Like you have all read before " I am new". all I have is common sense and a little electronics knowledge. I have been banging my head off the wall for a few weeks now and finally have come looking for help. I am currently trying to build a small scoreboard for my son to use with his hockey toys. however I am stumped on the timer portion. I currently have a uno and adafruit 7seg backpack. (ht16k33) I have figured out, by dissecting other projects how to make a basic countdown but I am stumped when it comes to dealing with time. dealing with minutes and seconds is not going so well for me. I can count down from 9999 to 0 no issues but with time im lost. I have figured out the score keeping with single 7 seg displays and shift registers. so this is the only thing im stuck on now. ultimately this is what im looking to do.
display period length on the adafruit 7 seg display. I would like to have the ability to set the time with buttons but if I need to I will set with code and leave it at that. Also be able to reset it to the starting time with a button which im sure I could probably figure out.
thank you in advance for any help. And please I'm not trying to have this done for me I,m just looking for a little direction.
From 9999 what? Seconds? Minutes? Phases of the moon?
I would like to have the ability to set the time with buttons
So, if you button one button that means what? If you button three buttons, what does that mean?
Using switches is so much easier.
display period length on the adafruit 7 seg display.
In minutes and seconds? In minutes? In seconds? In phases of the moon?
If you have time in seconds, converting that to minutes and seconds is trivial, using modulo (%) 60 and divide by 60. To put the two values into a single variable, multiply minutes by 100 and add seconds.
From 9999 what? Seconds? Minutes? Phases of the moon?
Quote
whole numbers.
So, if you button one button that means what? If you button three buttons, what does that mean?
I am basically saying I would like to control setting the time with hands on approach rather than doing it in the code. ie: use one button for minutes up and another for minutes down. and same for seconds one up and one down.
Using switches is so much easier.
I would love to use an encoder for setting time but I know that is out of my ability at this time.
In minutes and seconds? In minutes? In seconds? In phases of the moon?
I would like to display simply minutes and seconds. for example 5:00 start time. and once the minutes were down to 1 display seconds on the last two digits of the display ie: 00:59 and counting down.
So what needs to be displayed is the result of the conversion being done here? so what would be sent to the display would need to be a variable created and linked to the result. in your example it would be 459 and 458 would be the resulting variable sent to the display correct? sorry if i am not putting that into the write words.
Thank you for your help Paul. After some quick tinkering i have gotten the timer and display to work together with the following code. now i will start reading on how to join what i have already written for the score tracking and maybe i will be on my way.
int minutesToGo = 0;
int secondsToGo = 0;
int secondsToPlay = 10;
int timeRemaining = 0;
void setup() {
// put your setup code here, to run once:
matrix.begin(0x70);
}
void loop() {
// put your main code here, to run repeatedly:
minutesToGo = secondsToPlay / 60;
secondsToGo = secondsToPlay % 60;
timeRemaining = minutesToGo * 100 + secondsToGo;
if(timeRemaining >= secondsToPlay){
secondsToPlay--;
}
if(timeRemaining == -1){ // the negative 1 is there so that the display will show 0 but im sure you figured that out on your own.
Djcevera:
sorry for not following you here. in that code aren't I already decrementing the secondstoplay every second with the "secondsToPlay--"?.
You are, but conditionally, based on the wrong condition. You should do it unconditionally, after the delay(1000); call, or conditionally, based on millis() and the last time the value was decremented, without the need for delay().
You are, but conditionally, based on the wrong condition. You should do it unconditionally, after the delay(1000); call, or conditionally, based on millis() and the last time the value was decremented, without the need for delay().
Im going to attempt to wrap my head around this. so by placing the code in a if situation for lack of better words I am making it a conditional situation? now being as green as I am to this programming thing. after processing what you said a bit I should be doing this after the delay? however the only way I could think of would be by using a while command. Ie: while(timeRemaining >= secondsToPlay){
secondsToPlay--;
}
and placing it after the delay but im thinking this is still a form of conditional.
so you don't end up with -17 seconds to play, followed by -18, -19, etc.
i like this method to avoid the -#'s better than my current method. but by doing it this way wont it be putting the secondsToPlay-- back into a conditional situation?
i like this method to avoid the -#'s better than my current method. but by doing it this way wont it be putting the secondsToPlay-- back into a conditional situation?
Yes, but there is nothing wrong with changing the remaining time in a conditional statement, as long as the conditional statement makes sense.
Paul,
Once again thank you for your reply. I appreciate you taking the time to explain things. if you wouldnt mind i have been chopping around some code and in regards to having more control over setting the time for the counter could you please look at the mess i have attached and tell me if it looks like it would work. im basically trying to increase minutes by one with a button press and increase seconds by 10 with a button press. ultimatly i will also include a if command for controll of starting the timer function. something along the lines of if button one is pressed then call the timer function that i will create.
const int minButton = 3;
const int secButton = 4;
int minButtonState = 0;
int secButtonState = 0;
int minButtonLastState = 0;
int secButtonLastState = 0;
int secondsToPlay = 0;
I tried to bite off more than I could chew on that last post. so I tried to start with a smaller task. which is a simple start button for the countdown but with the following code the timer only runs if I hold down the start button connected to the arduino #5 pin. I tried to change a bunch of things can someone please tell me what I am missing because im sure it is staring me right in the face and im just not seeing it.
int minutesToGo = 0;
int secondsToGo = 0;
int secondsToPlay = 500;
int timeRemaining = 0;
const int startButton = 5;
int startButtonState = 0;
int startButtonLastState = 0;
First, we have no idea how the switches are wired, or whether HIGH means pressed or released.
The simplest way to wire a switch is to connect on leg to a digital pin and the other leg to ground, and use the INPUT_PULLUP mode to enable the internal pullup resistor.
With any other mode, an external resistor is required.
With the internal one, HIGH means not pressed and LOW means pressed, just like the top of a pushbutton switch.
There is not much point in saving the current state as the previous state if you never care about the previous state.
Starting the timer and showing the time should be independent events, shouldn't they?
Starting the timer should simply set a boolean flag, timerRunning perhaps, to true. Another switch could be used to pause the timer, and another to zero the time.
exit() basically causes the processor to twiddle its thumbs uselessly forever. You really shouldn't be calling it.
Display the time remaining, or 00:00, if there is time remaining, or time has run out. But, keep loop()ing, in case there is to be another period.
First, we have no idea how the switches are wired, or whether HIGH means pressed or released.
The simplest way to wire a switch is to connect on leg to a digital pin and the other leg to ground, and use the INPUT_PULLUP mode to enable the internal pullup resistor.
With any other mode, an external resistor is required.
I was using the latter of the two. one leg to power the second leg to input pin with a resistor pulling to ground.
There is not much point in saving the current state as the previous state if you never care about the previous state.
I was using code from the state change example so i figured it was a needed element but i will change this.
Starting the timer and showing the time should be independent events, shouldn't they?
I will seperate the two if that would make sense.
Starting the timer should simply set a boolean flag, timerRunning perhaps, to true. Another switch could be used to pause the timer, and another to zero the time.
this is something new to me. i understand that a boolean is either true or false. but i will need to read up a little more on to how to implement this into my code.
once again Paul thank you for taking the time to explain. rather than taking the "just because i said approach".
Im sorry I need another set of eyes on this. I know im sure I have gone backwards because im trying to figure this out but so far with the code below I can control input to "minutes and seconds" however I cannot for the life of me get this countdown to start. what am I missing. I did something earlier that when I hit the start button it all just froze. kind of like my brain at this point. I did not want to ask directly for an example of how I could implement a start button into this but im at the point where I just cannot figure it out. could someone please show me an example.
see code below. edited due to un-needed comments within the sketch
#include <Wire.h>
#include <Adafruit_GFX.h> #include <gfxfont.h>
#include <Adafruit_LEDBackpack.h>
Adafruit_7segment matrix = Adafruit_7segment();
const int minutesButtonPin = 2;
const int secondsButtonPin = 3;
const int startButtonPin = 4;
int startButtonState = 0;
int lastButtonStateStart = 0;
int minutesButtonState = 0;
int lastButtonStateMinutes = 0;
int secondsButtonState = 0;
int lastButtonStateSeconds = 0;
int minutesToGo = 0;
int secondsToGo = 0;
int secondsToPlay = 0;
int timeRemaining = 0;