i started this code and it works to count from 24 to 0. However, im just confuse as to where should i insert the button start so that when that button is pressed the countdown will start. Any help pls.
Thanks a lot
unsigned long currentmillis;
unsigned long previousmillis;
int duration=900;
int counter=25;
int pTens=0;
int pOnes=0;
const int dataPin=2;
const int clockPin =3;
const int latchPin=4;
static byte digitDisplay[10]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void setup()
{
pinMode(dataPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(latchPin,OUTPUT);
Serial.begin(9600);
}
void loop()
{
currentmillis=millis();
if(currentmillis-previousmillis>duration)
{
previousmillis=currentmillis;
if(counter!=0)
{
counter=counter-1;
}
if(counter<25 && counter>19)
{
pTens=2;
pOnes=counter-20;
digitalWrite(latchPin,HIGH);
shiftOut(dataPin,clockPin,MSBFIRST,digitDisplay[pOnes]);
shiftOut(dataPin,clockPin,MSBFIRST,digitDisplay[pTens]);
digitalWrite(latchPin,LOW);
}
if(counter<20 && counter>9)
{
pTens=1;
pOnes=counter-10;
digitalWrite(latchPin,HIGH);
shiftOut(dataPin,clockPin,MSBFIRST,digitDisplay[pOnes]);
shiftOut(dataPin,clockPin,MSBFIRST,digitDisplay[pTens]);
digitalWrite(latchPin,LOW);
}
if(counter<10)
{
pTens=0;
pOnes=counter;
digitalWrite(latchPin,HIGH);
shiftOut(dataPin,clockPin,MSBFIRST,digitDisplay[pOnes]);
shiftOut(dataPin,clockPin,MSBFIRST,digitDisplay[pTens]);
digitalWrite(latchPin,LOW);
}
else
Serial.println(counter);
}
}
Actually, i really wanted to stop the countdown if necessary, like in basketball, and press again to start the countdown. If it reaches 00 , another button is press to start the countdown again. With my code above, it only countdown immediately and stop when it reaches 00.
I tried to add
if (digitalRead(buttonPin)==HIGH) at the bottom of the setup , it does nothing.
The word please mean " that i've been trying to solve this problem and come up with a working 24 shot clock for 2 mos now" and until now i can't figure out how to do it. Thanks
Do a search on "switch debouncing" as that could be another issue. Also, we can often be more helpful if we see all of the code for the program, not just a glimpse of parts of it. I think new programmers avoid doing this because they think we will judge them. Actually, we do, but that's part of the journey in moving from a beginning programmer to a more experienced programmer. The only way to improve is to avoid repeating past mistakes. We've all been there, so, just bite the bullet and join the crowd.
EDIT: Us judging a poster's code is not always a bad thing, as it may influence the details and method of the answer. That is, it helps us frame an answer aimed at the experience level we see in the posted code.
Thanks Mr. econjack for the enlightenment. You were right, im afraid that i might received negative comments for bad coding. By the way here is my complete code that gives me a bit confusion where to insert the start button to start the countdown and press the same button to pause the countdown. thanks
const int dataPin=2;
const int clockPin =3;
const int latchPin=4;
const int buttonPin=5;
int buttonState=0;
int counter=24;
int pOnes=0;
int pTens=0;
int LastButtonState=0;
static byte displayDigit[10]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void setup() {
pinMode(dataPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(latchPin,OUTPUT);
pinMode(buttonPin,INPUT);
initDisplay();
}
void loop()
{
buttonState=digitalRead(buttonPin);
if(buttonState!=LastButtonState)
{
if ( buttonState==1)
{
for (int x=24;x>0;x--)
{
if (counter!=0)
{
counter--;
}
}
}
LastButtonState=buttonState;
updateScore();
//delay(50);
}
delay(1000);
}
void updateScore()
{
if (counter<25 && counter>19)
{
pTens=2;
pOnes=counter-20;
}
else if(counter<20 && counter>9)
{
pTens=1;
pOnes=counter-10;
}
else if ( counter<10)
{
pTens=0;
pOnes=counter;
}
UpdateLed();
}
void UpdateLed()
{
digitalWrite(latchPin,HIGH);
shiftOut(dataPin,clockPin,MSBFIRST,displayDigit[pOnes]);
shiftOut(dataPin,clockPin,MSBFIRST,displayDigit[pTens]);
digitalWrite(latchPin,LOW);
}
void initDisplay()
{
digitalWrite(latchPin,HIGH);
shiftOut(dataPin,clockPin,MSBFIRST,displayDigit[4]);
shiftOut(dataPin,clockPin,MSBFIRST,0x5B);
digitalWrite(latchPin,LOW);
}
No problem. Here's a small program that addresses your determination of tens and ones. You were right to make it a function, but you can make it simpler. Don't use this code; just use it as a learning tool.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
char input[6];
int charsRead;
int counter;
int pOnes = 0;
int pTens = 0;
if (Serial.available() > 0) {
charsRead = Serial.readBytesUntil('\n', input, sizeof(input) - 1);
input[charsRead] = NULL;
counter = atoi(input);
Serial.print("counter = ");
Serial.print(counter);
SeparateDigits(counter, &pOnes, &pTens);
Serial.print(" tens = ");
Serial.print(pTens);
Serial.print(" ones = ");
Serial.println(pOnes);
}
}
void SeparateDigits(int count, int *ones, int *tens)
{
*tens = count / 10;
*ones = count % 10;
}
After the code compiles, open the Serial monitor (Ctrl-Shift-M) and type in a count into the textbox and press Enter. Explain what you see. Also, it will be easier to read your code if you use Ctrl-T before you post it.