how to make a code run automatically+manually?

hello ,
I want to make a code that light a led when I press a button and also every 5 min.
I know how to do everyone by it self - but how do I combine then together?
also when I press the button the countdown will start from zero again?

thanks,

If you have some code, post it. If not, I'll ask that this be moved to the Project Guidance section.

david1234:
hello ,
I want to make a code that light a led when I press a button

How long do you want the LED lit?

and also every 5 min.

How long do you want the LED lit?

also when I press the button the countdown will start from zero again?

Is that what you want to happen, or is that what you don't want to happen?
Post your code.

this is what I have so far :

int led = 13;
int test=4;
int timer=0;
void setup()
{      
  Serial.begin(9600);  
  pinMode(test,INPUT);
  pinMode(led, OUTPUT);     
}


void loop() 
{//start of loop

 if (test==HIGH)

 {//start of manual 
   Serial.println("button is on");
   digitalWrite(led,HIGH);
   delay(25000);
   digitalWrite(led,LOW);
timer=0;

 }//end of manual

 else  (timer<300000); //time of 5 min
{
timer++;//counting 5 min

}//end of counting 

 if (timer==300000)//5 min is past 

{//start of auto blink
Serial.println ("now working auto");
 digitalWrite(led,HIGH);
   delay(25000);
   digitalWrite(led,LOW);
timer=0;

}//end of auto blink

}//end of the loop
int led = 13;
int test=4;
unsigned long timer=0; //int
/*
[b]On the Arduino Uno (and other ATMega based boards) 
an int stores a 16-bit (2-byte) value. 
This yields a range of -32,768 to 32,767 
(minimum value of -2^15 and a maximum value of (2^15) - 1). [/b]
*/
void setup()
{      
  Serial.begin(9600);  
  pinMode(test,INPUT);
  pinMode(led, OUTPUT);     
}


void loop() 
{//start of loop

    if (digitalRead(test) == HIGH) // test is the pin number, you need to read it
  {//start of manual 
    Serial.println("button is on");
    digitalWrite(led,HIGH);
    delay(25000);
    digitalWrite(led,LOW);
    timer=0;

  }//end of manual

  else  (timer<300000L); //time of 5 min
  {
    timer++;//counting 5 min

  }//end of counting 

  if (timer==300000L)//5 min is past 
  {//start of auto blink
    Serial.println ("now working auto");
    digitalWrite(led,HIGH);
    delay(25000);
    digitalWrite(led,LOW);
    timer=0;

  }//end of auto blink

}//end of the loop

david1234:
this is what I have so far :

 else  (timer<300000); //time of 5 min

{
timer++;//counting 5 min

This will increment timer for each iteration of loop(), not each millisecond.
As each iteration takes only microseconds, the count will reach 300000 long before 5 mins is up.
Look up 'blink without delay' in the Playground for an answer to your problem.

O.K
I saw it , but I think something is missing :

const int ledPin =  13;      // the number of the LED pin
const int test=12;
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 600000;           // interval at which to blink (milliseconds~~ 10 min)

void setup() {

  pinMode(ledPin, OUTPUT);    
  pinMode(test,INPUT);


void loop()
{// start of loop

    unsigned long currentMillis = millis();

    if(currentMillis - previousMillis < interval) { //What to do if didn't pass 10 min

    previousMillis = currentMillis;  

 if (digitalRead(test) == HIGH) 
  {//start of manual 
    Serial.println("button is on");
    digitalWrite(led,HIGH);
    delay(25000);
    digitalWrite(led,LOW);
    
}

}//end of wait 

else // 10 min is done , need to light led auto 

{
digitalWrite(led,HIGH);
    delay(25000);
    digitalWrite(led,LOW);
previousMillis = currentMillis;

}

}// end of loop

Is my logic correct ?

Is my logic correct ?

No.

By declaring the variable currentMillis at the start of every loop you reset it. Therefore you will never see it time out.

david1234:
O.K
I saw it , but I think something is missing :

Quite a lot!
Try this (untested):

const int ledPin =  13;      // the number of the LED pin
const int test=12;
long startTime;                   // to store the start of the timer
long interval = 600000;           // interval at which to blink (milliseconds~~ 10 min)

void setup() {
  Serial.begin(9600);  //You need this for the serial monitor to work!
  pinMode(ledPin, OUTPUT);    
  pinMode(test,INPUT);
  startTime = millis(); //set startTime
}

void loop(){// start of loop

    if(millis() - startTime < interval) { //What to do if didn't pass 10 min
       if (digitalRead(test) == HIGH)  {//start of manual 
         Serial.println("button is on");
         digitalWrite(ledpin,HIGH);
         delay(25000);
         digitalWrite(ledpin,LOW);
         startTime = millis();  //reset startTime
       }
   }
else { // 10 min is done , need to light led auto 
    digitalWrite(ledpin,HIGH);
    delay(25000);
    digitalWrite(ledpin,LOW);
    startTime = millis();  //reset startTime
   }
}// end of loop

it's work just fine!
Thank you very much.

:slight_smile: