Countdown timer using 4digit7segment with Hour:Min displayed

Hey, i need help creating a working countdown timer for my 4digit7segment display with Hour:Minute showing. Im very new, and i suck at arduino, but i need this for a puzzle, where we got 3 buttons, and the countdown timer goes down from 1hour to 0. If its possible, is there any way to program it so that it loses 5 minutes for each wrong press?

Yes, there is a way to program an it so that the it loses 5 seconds for the wrong press. The way I'd program it would depend on the it type.

What you would want to do with it is change the code on line number to the same code as on the other line number as shown in the code you posted.

Welcome to the forum!

What kind of help do you need? Did you try to make your project yourself? Please show your efforts.

well, i don't have the timer yet, because the only way it works is in seconds, and because 1h = 3600 seconds, it 's not nice on my display. Here is the button puzzle though, and it seems to work perfectly.

#include <Servo.h>

Servo s;

const int K1 = 4;
const int K2 = 5;
const int K3 = 8;
const int buzz = 10;
int state1 = LOW;
int state3 = LOW;
int state2 = LOW;


boolean correct = false;
void setup() {
  // put your setup code here, to run once:
  s.attach(A0);
  pinMode(K1, INPUT);
  pinMode(K2, INPUT);
  pinMode(K3, INPUT);
  pinMode(buzz, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  state1 = digitalRead(K1);
  state2 = digitalRead(K2);
  state3 = digitalRead(K3);
  if (state1 == HIGH){
    Serial.print("1");
    correct = true;
    tone(buzz, 3000);
    delay(100);
    noTone(buzz);
    delay(50);
    tone(buzz, 3000);
    delay(100);
    noTone(buzz);
  }
  if (state2 == HIGH){
    correct = false;
    Serial.print("2");
    tone(buzz, 200);
    delay(1000);
    noTone(buzz);
  }
  if (state3 == HIGH){
    correct = false;
    Serial.print("3");
    tone(buzz, 200);
    delay(1000);
    noTone(buzz);
  }
  if (correct == false){
    Serial.println("servo fout");
    s.write(0);
  }else{
    Serial.println("servo juist");
    s.write(180);
    
  }
}

Please insert your code using the code tags. You can go back and fix your original post.
Read the forum guidelines to see how to properly insert the code and some other good information on making a good post.

So you want me to fix the code that is NOT IN CODE TAGS from post#4 that is working or do you want someone to add timer code or what?

What is considered a correct press? What is a wrong press?

id like to add a timer code if possible

Well add timer code, no need to wait on me to do it.

sorry, but the forum doesn't work this way....
We can help you, but only "help" rather than make thing to you.
You should try yourself and show us what you achieved.


int digit1 = 11; 
int digit2 = 10; 
int digit3 = 9; 
int digit4 = 6; 


int segA = A1; 
int segB = 3;
int segC = 4; 
int segD = 5;
int segE = A0; 
int segF = 7; 
int segG = 8; 

void setup() {                
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);

  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
  
  pinMode(13, OUTPUT);
}

void loop() {
  
  //long startTime = millis();

  displayNumber(millis()/1000);

  //while( (millis() - startTime) < 2000) {
  //displayNumber(1217);
  //}
  //delay(1000);  
}

//Given a number, we display 10:22
//After running through the 4 numbers, the display is left turned off


void displayNumber(int toDisplay) {
#define DISPLAY_BRIGHTNESS  500

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  long beginTime = millis();

  for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      break;
    case 2:
      digitalWrite(digit2, DIGIT_ON);
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      break;
    }

    //Turn on the right segments for this digit
    lightNumber(toDisplay % 10);
    toDisplay /= 10;

    delayMicroseconds(DISPLAY_BRIGHTNESS); 
    //Display digit for fraction of a second (1us to 5000us, 500 is pretty good)

    //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
  }

  while( (millis() - beginTime) < 10) ; 
  //Wait for 20ms to pass before we paint the display again
}

//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay) {

#define SEGMENT_ON  LOW
#define SEGMENT_OFF HIGH

  switch (numberToDisplay){

  case 0:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 10:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;
  }
}

The first thing you'd want to do is get rid of using delay().

Look at File|Examples|02.Digital|BlinkWithoutDelay to use millis() for timing instead of delay().

code from pst#4 uses delay().

When is the timer supposed to start?

on the end of the case add in a

default:
numberToDisplay=0;
break;

just in case a number ends up in numberToDisplay that's not on the case list.

at 60 minutes, i normally put it at seconds 3600

oh so the timer out value will be 3600X1000mS. And when the timer is loaded that's when you want to start the clock, right? And you will be using millis() for this timer, right? What are the reasons that the timer would reduce by 5X1000mS?

What starts the timer?

while( (millis() - beginTime) < 10) ;

That is blocking code.

delayMicroseconds(DISPLAY_BRIGHTNESS);

mixing millis() and delay() is not a good idea.

When is the countdown supposed to start?

Hey, sorry for the really late answer, i was quite busy. I just wanted the timer to start once the energy got pulled in, because i was supposed to merge it with the button code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.