How would i increase the timer to 45 mins?

Hello

I am using this code found online but cannot work out how to change the timer to 45 mins on teh countdown?

The code is below, is anyone able to assist please? Very very new to this!!!

Thanks

#define red 13
#define Speaker 12
#define blue 8
#define Green 7

float times;
float delays = 1000;
float delays2 = 500;


void setup() {
  Serial.begin(9600);
  pinMode(red, OUTPUT);
  pinMode(Speaker, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  myfunction();

}



void myfunction() {

  delays2 = delays2/2.0;
  
  digitalWrite(Green, HIGH);
  tone(Speaker, 2000, delays2);
  delay(delays2);
  digitalWrite(Green, LOW);
  delay(delays2);
  digitalWrite(Green, HIGH);
  tone(Speaker, 2000, delays2);
  delay(delays2);
  digitalWrite(Green, LOW);
  delay(delays2);
  digitalWrite(Green, HIGH);
  tone(Speaker, 2000, delays2);
  delay(delays2);
  digitalWrite(Green, LOW);
  delay(delays2);
  digitalWrite(Green, HIGH);
  tone(Speaker, 2000, delays2);
  delay(delays2);
  digitalWrite(Green, LOW);
  delay(delays2);

}


void loop() {
    Serial.println(digitalRead(2));
  delays = delays/1.03;
 
  if (times < 1200){
    times += 1;
    tone(Speaker, 4000, delays);
    digitalWrite(red, HIGH);
    delay(delays);                     
    digitalWrite(red, LOW);
    digitalWrite(Speaker, LOW);   
    delay(delays); 
  }
   
     if (times == 1200){
   tone(Speaker, 500, 3000);
   digitalWrite(blue, HIGH);
   delay(19999999);
     
    }
  
      
  if (digitalRead(2) == LOW){
      times = 1230;
      digitalWrite(red, HIGH);
      delay(1000);
      digitalWrite(red, LOW);
      delay(1000);
      digitalWrite(blue, HIGH);
      delay(1000);
      digitalWrite(blue, LOW);
      defuse();

 
  } 
 

}


   

      
void defuse() {

       
      digitalWrite(Green, HIGH);
      tone(Speaker, 2000, 100);
      delay(100);
      digitalWrite(Green, LOW);
      digitalWrite(blue, HIGH);
      tone(Speaker, 1000, 100);
      delay(100);
      digitalWrite(blue, LOW);
      digitalWrite(red, HIGH);
      tone(Speaker, 2000, 100);
      delay(100);
      digitalWrite(red, LOW);
      delay(100);
      digitalWrite(Green, HIGH);
      delay(1999999);
      
}

not clear what this code does.

looks like it does something for 1200 iterations and then delays for 5.5 hours

maybe you just need to increase that 1200 value

Thanks for your reply, appreciated. it is from this https://www.hackster.io/nashboss100/arduino-airsoft-time-bomb-updated-b92d0d

You mean this one?

2700000 is 45 minutes in milliseconds

1 Like

quite possibly. I have done some tweaking to this code now, but again struggling to get the countdown timer changed whilst keeping the LED and Buzzer consistently flashing etc whilst counting down

int bluePin=6;
int redPin=7;
int greenPin=8;
int Speaker=12;
int Button=11;
int countdownDuration=1;
unsigned long countdownStartTime;
bool countdownState;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Initialise pins
pinMode(Speaker, OUTPUT);
digitalWrite(Speaker,LOW);
pinMode(bluePin,OUTPUT);
digitalWrite(bluePin,LOW);
pinMode(redPin,OUTPUT);
digitalWrite(redPin,LOW);
pinMode(greenPin,OUTPUT);
digitalWrite(greenPin,LOW);
pinMode(Button,OUTPUT);
digitalWrite(Button,LOW);
// Blink green LED 5 times
// OK to use delay here since the countdown isn't running yet
for (int j=0; j<=4; j++) {
digitalWrite(greenPin,HIGH);
delay(500);
digitalWrite(greenPin,LOW);
delay(500);
}
// Initialise timer
countdownStartTime=millis();
countdownState=true;
}
void loop() {
// put your main code here, to run repeatedly:
if (countdownState) {
unsigned long durationInMillis=countdownDuration * 60. * 1000.; // Convert minutes to milliseconds
durationInMillis=20000;
unsigned long currentMillis=millis();
long elapsedTime=currentMillis - countdownStartTime;
long endTime=countdownStartTime + durationInMillis;
long remainingTime=durationInMillis - elapsedTime;
// Flash the red led for 50 milliseconds, every (10% of the remaining time)
// Eg. If time remaining is 2000 milliseconds, flash the led every 200 milliseconds
flashLed(remainingTime/10,50);
digitalWrite(Button,HIGH);
//check for Button press here.
//If button has been pressed, run "defuse" sequence, and set
//countdownState to false.
//If not, then do this...
if (remainingTime < 0) {
Serial.println("BOOM!");
countdownState=false;
digitalWrite(redPin,LOW);
digitalWrite(bluePin,HIGH);
}
}
}
void flashLed(int frequency, int duration) {
static bool ledState;
static long lastFlash;
long currentFlashMillis = millis();
if (!ledState && (currentFlashMillis >= (lastFlash + frequency))) {
ledState=true;
digitalWrite(redPin,HIGH);
digitalWrite(Speaker,HIGH);
lastFlash=millis();
} else {
if (currentFlashMillis >= (lastFlash + duration)) {
ledState=false;
digitalWrite(redPin,LOW);
digitalWrite(Speaker,LOW);
}
}
}

Maybe the trick is to change:
int countdownDuration=1;
to:
int countdownDuration=45;

You would also have to remove this line which ignores the value calculated from countdownDuration and forces the countdown to 20 seconds:
durationInMillis=20000;

1 Like

I will give that a try now John and feedback! Thank you!

unfortunately it didn't change the duration

@johnwasser so i've tried a variety of different commands such as changing the 1 -> 45 and commenting out the 20000 part as mentioned. Nothing seems to make this 45 mins!

Hello
Did you check to use the millis() funktion to built your own time handler ?

@paulpaulson hi Paul. I am not sure how to do so to be honest, very new to this. I've tried several iterations on adjusting the int countdownDuration=60;
and durationInMillis=20000; but nothing seems to make it last 30 mins or 45 mins. Do you have any suggestions?

Hello
Inside the IDE you will find a proper example how to work with the millis() - function.

1 Like

I am the only one who worries from time to time that we are helping t*rr*r*sts with lousy coding skills make they devices?

a7

1 Like

No. I recall several threads involving possible weapons applications where Robin2's only contribution was to state that he would not be contributing :laughing:

sorry to disappoint, this is for an airsoft prop lol

@paulpaulson i did have a read of that, couldn't fathom it out for some reason though

Hello
It´s very simple - learn it !

what is simple to some, may not be for others, hence asking for help. I am trying to but am struggling

This code is pure crap. Downloading garbage code from the internet and trying to modify it, is probably not the best way to learn. You can only learn how not to do things.

Also because it's crap, nobody wants to experience the pain of trying to fix it.

Didn’t say, but that’s how I felt about the gentleman with the Dixie Horns…

a7