Pages: [1]   Go Down
Author Topic: how to make a code run automatically+manually?  (Read 615 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,
   
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 48993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1415
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 16, 2013, 09:29:16 pm by Henry_Best » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is what I have so far :

Code:
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

Logged

SG
Offline Offline
God Member
*****
Karma: 11
Posts: 515
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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

Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1415
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is what I have so far :
Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

O.K
I saw it , but I think something is missing :
 
Code:
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 ?
« Last Edit: July 21, 2013, 02:48:44 am by david1234 » Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 618
Posts: 33940
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1415
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quite a lot!
Try this (untested):
Code:
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
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

 smiley
Logged

Pages: [1]   Go Up
Jump to: