Go Down

Topic: how to make a code run automatically+manually? (Read 738 times) previous topic - next topic

david1234

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,
   

PaulS

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

Henry_Best

#2
Jul 17, 2013, 04:23 am Last Edit: Jul 17, 2013, 04:29 am by Henry_Best Reason: 1

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

How long do you want the LED lit?
Quote
and also every 5 min.

How long do you want the LED lit?
Quote
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.

david1234

this is what I have so far :

Code: [Select]

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


BillHo

Code: [Select]
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


Henry_Best


this is what I have so far :
Code: [Select]

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.

david1234

#6
Jul 21, 2013, 09:39 am Last Edit: Jul 21, 2013, 09:48 am by david1234 Reason: 1
O.K
I saw it , but I think something is missing :

Code: [Select]

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 ?

Grumpy_Mike

Quote
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.

Henry_Best


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

Quite a lot!
Try this (untested):
Code: [Select]

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


david1234


Go Up