How to show my random time on my 4times 7segment display

i make a TIC TAC BOOM BOMB with arduino and i want to show how much time they got left to play.
Somebody that can help me with te code. I'm really struggling.

int motorPin = 3; //motor transistor is connected to pin 3
int Buttonpin = 2; //schakelaar is pin 2
int onoffbutton = 4; //schakelaar is pin 4
int ledje = 5; //ledje is pin 5 VSQA void setup()

int buttonPinneke = 7;
void setup(){
pinMode(motorPin, OUTPUT);
pinMode(Buttonpin, INPUT);
pinMode(onoffbutton, INPUT);
pinMode(ledje,OUTPUT);

pinMode(buttonPinneke, INPUT_PULLUP);
}

void loop()
{
if(digitalRead(buttonPinneke) == LOW) //functions based off of button pulling input pin LOW
{
delay(random(30000,90000 )); // random timer tussen 30 seconden en 1 minuut 30seconden
digitalWrite(ledje,HIGH);
digitalWrite(motorPin, HIGH); //vibrate
delay(12000); // 12 seconden
digitalWrite(motorPin, LOW); //stop vibrating
digitalWrite(ledje,LOW);}

}

Making people who want to help you guess what your code is will be time consuming and would be a big struggle. I suspect if we had to guess your code it would be, also, a struggle.

If the code, that you want help with, was posted in code tags, then the guessing your code part would be completed and we can then move onto troubleshooting your code.

1 Like

code tags?

1 Like

i shared my code. I want on my display MM:SS that would be nice but i'm not that good in programming

How much time do they initially have?

random time between 30 seconds and 1minute 30 seconds

Do you know about millis()? You can use the

and words like "millis for timing" to gain more information.

Oh, millis() and delay do not play well together as a note.

Code tags, 2nd request?

oh ok i go trying now

Different displays and different display chips work differently. So, are you able to display numbers correctly without worrying about time?

If you just want to display seconds that should be straightforward. If you want minutes & seconds you'll probably want two different variables for minutes & seconds.

And note that an unsigned integer only counts-up to about 64,000 so for milliseconds you'll usually want an unsigned long. Once you've converted to minutes & seconds, integers are fine.

I now have an timer on my LCD display but if i set in the code that he chose a random time between 0 seconds and 1 minute 30 seconds he chose a random time every second.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
long minute;
long second ; 
int indication = 10; //Connect LED or Buzzer to digital pin 10
void setup() {
  pinMode(indication, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4, 0);
  lcd.print("MM:SS");
}

void loop() {
  minute = random(0,2);
  second = random(0,59);
  long countdown_time = (minute * 60) + second;
  long countdowntime_seconds = countdown_time - (millis() / 1000);
  if (countdowntime_seconds >= 0) {
    long countdown_minute = ((countdowntime_seconds / 60) % 60);
    long countdown_sec = countdowntime_seconds % 60;
    lcd.setCursor(4, 1);
     if (countdown_minute < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_minute);
    lcd.print(":");
    if (countdown_sec < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_sec);
    if (countdowntime_seconds == 0) {
      digitalWrite(indication, HIGH);
    }
  }
  delay(500);
} 

I now have this code but it choose every second another time. How can i change the code that he choose one time a random and then he countdown. If i push then another button it choose another random time

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
long minute;
long second ; 
int indication = 10; //Connect LED or Buzzer to digital pin 10
void setup() {
  pinMode(indication, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4, 0);
  lcd.print("MM:SS");
}

void loop() {
  minute = random(0,2);
  second = random(0,59);
  long countdown_time = (minute * 60) + second;
  long countdowntime_seconds = countdown_time - (millis() / 1000);
  if (countdowntime_seconds >= 0) {
    long countdown_minute = ((countdowntime_seconds / 60) % 60);
    long countdown_sec = countdowntime_seconds % 60;
    lcd.setCursor(4, 1);
     if (countdown_minute < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_minute);
    lcd.print(":");
    if (countdown_sec < 10) {
      lcd.print("0");
    }
    lcd.print(countdown_sec);
    if (countdowntime_seconds == 0) {
      digitalWrite(indication, HIGH);
    }
  }
  delay(500);
} 

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hi,
Problems in your code.

  1. You initialize variables and create random each loop cycle.
  1. Which button are you referring to?
1 Like

Since hardware is involved it would be best if you posted an accurate schematic of your circuit as you have it wired. Frizzy pictures are not schematics they are wiring diagrams and almost useless. Also post links to the hardware devices that give technical information, links to places like Amazon generally give sales information, not technical information.

@robindc123

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

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