i want to turn it off after 10 seconds!!

Hello!I am doing a project and i would like your help(i am not very good with codes,so it is not mine).When i push the button for the RED1 i want to stay it on for 10 seconds and then to turn it off automatically.How could i do it?
Thanks!!


_4_LED_PUSHBUTTON_CONTROL.ino (10.4 KB)

/*
* Sketch: TWO_WAY_LED_CONTROL_1_LED
* By Martyn Currey
* 20.08.2016
* Written in Arduino IDE 1.6.3
*
* Requires the Arduino Bluetooth Control II Android App. Can be downloaded from Google Play.
* See http://www.martyncurrey.com/arduino-bluetooth-control/
*
* Turn an LED on and off from an Android app or from a physical switch connected to the Arduino
* Uses the following pins
*
* D8 - AltsoftSerial TX
* D9 - AltsoftSerial RX
* D5 - LED + resistor
* D2 - Button switch 
*/

// AltSoftSerial uses D9 for TX and D8 for RX. While using AltSoftSerial D10 cannot be used for PWM.
// Remember to use a voltage divider on the Arduino TX pin / Bluetooth RX pin
// Download from https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3);
 

// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;

// Constants for hardware
const byte LED1_PIN = 4;
const byte SWITCH1_PIN = 5;

// general variables
boolean LED1_State = false;
boolean switch1_State = false;
boolean oldswitch1_State = false;

void setup()  
{
    // Set the button switch pin for input
    pinMode(SWITCH1_PIN, INPUT); 

    // Set the red LED pin for output and make it LOW
    pinMode(LED1_PIN, OUTPUT); 
    digitalWrite(LED1_PIN,LOW);
     
    // open serial communication for debugging
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");
     
    //  open software serial connection to the Bluetooth module.
    BTserial.begin(9600); 
    Serial.println("AltSoftSerial started at 9600"); 
          
    newData = false;

} // void setup()
 
 
void loop()  
{
       checkSwitch();
       recvWithStartEndMarkers();                // check to see if we have received any new commands
       if (newData)  {   processCommand();  }    // if we have a new command do something about it
}



/*
****************************************
* Function checkSwitch()
* checks the status of a button switch and turns an LED on or off depending on the switch status
* 
* passed:
*  
* global: 
*      switch1_State
*      LED1_State
*      oldswitch1_State
*
* Returns:
*          
* Sets:
*      switch1_State
*      LED1_State
*      oldswitch1_State
*/
void checkSwitch()
{
     // Simple toggle switch function with very simple debouce.
     boolean state1 = digitalRead(SWITCH1_PIN); delay(1);
     boolean state2 = digitalRead(SWITCH1_PIN); delay(1);
     boolean state3 = digitalRead(SWITCH1_PIN); delay(1);
     unsigned long currentMillis = millis();
     if ((state1 == state2) && (state1==state3))   
     { 
          switch1_State = state1;  
          if ( (switch1_State == HIGH) && (oldswitch1_State == LOW) )
          {
               LED1_State = ! LED1_State;  
               if ( LED1_State == HIGH )) 
               {  
                    
                    BTserial.print("<L,1,1>" );  
                    digitalWrite(LED1_PIN,HIGH);  
                    Serial.println("Sent - <L,1,1>");    
               }
                                           
               else                     
               {  
                   BTserial.print("<L,1,0>");   
                   digitalWrite(LED1_PIN,LOW);   
                   Serial.println("Sent - <L,1,0>");    
               }    
          }          
          oldswitch1_State = switch1_State;
      }
}






/*
****************************************
* Function processCommand
* parses data commands contained in receivedChars[]
* receivedChars[] has not been checked for errors
* 
* passed:
*  
* global: 
*       receivedChars[]
*       newData
*
* Returns:
*          
* Sets:
*       receivedChars[]
*       newData
*/
void processCommand()
{

     Serial.print("receivedChars = ");  
     Serial.println(receivedChars);

    if (strcmp ("L10",receivedChars) == 0) 
    {
        digitalWrite(LED1_PIN,LOW);
        LED1_State = LOW; 
        Serial.println("LED1 LOW");  
    }
    
    else if (strcmp ("L11",receivedChars) == 0)
    {
        digitalWrite(LED1_PIN,HIGH);
        LED1_State = HIGH; 
        Serial.println("LED1 HIGH");  
    }




   
    receivedChars[0] = '\0';
    newData = false;
   
}


// function recvWithStartEndMarkers by Robin2 of the Arduino forums
// See  http://forum.arduino.cc/index.php?topic=288234.0
/*
****************************************
* Function recvWithStartEndMarkers
* reads serial data and returns the content between a start marker and an end marker.
* 
* passed:
*  
* global: 
*       receivedChars[]
*       newData
*
* Returns:
*          
* Sets:
*       newData
*       receivedChars
*
*/
void recvWithStartEndMarkers()
{
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
     char rc;
 
     if (BTserial.available() > 0) 
     {
          rc = BTserial.read();
          if (recvInProgress == true) 
          {
               if (rc != endMarker) 
               {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx > maxDataLength) { ndx = maxDataLength; }
               }
               else 
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { recvInProgress = true; }
     }
}

You can create a boolean variable to represent the on/off state of the LED. Then adapt (mostly copy) the Blink without Delay sketch that comes with the IDE to implement the delay.

In your "processCommand()" you set the variable instead of turning the LED on directly. A timer function in loop() looks at the state of the variable, the time, and clears the LED.

Did you go back and edit your post?!!!?

aarg:
Did you go back and edit your post?!!!?

There is no indication of that in the subject line of the posts.

sterretje:
There is no indication of that in the subject line of the posts.

Ok, well my answer remains the same.

thanks a lot for your answer!It is a little complicated for me!I am electricean and i am not good with codes,but i will try to do it!The first zip that i uploded uses 4 push button with four led!The last code has one push button and one led!I want to include a timer in the sketch and when i push the button the lede will turn on,and after 10 seconds to turn off automatically!I want this for a control of electric shouters with relay in my house!!I will try to include whatever i can! :slight_smile:

I try to include the millis but i am stuck.I did something but it seems not to be wright!

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3);
 

// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;

// Constants for hardware
const byte LED1_PIN = 4;
const byte SWITCH1_PIN = 5;

// general variables
[color=red]unsigned long off_time;  //what a added[/color]

boolean LED1_State = false;
boolean switch1_State = false;
boolean oldswitch1_State = false;

void setup()  
{
    // Set the button switch pin for input
    pinMode(SWITCH1_PIN, INPUT); 

    // Set the red LED pin for output and make it LOW
    pinMode(LED1_PIN, OUTPUT); 
    digitalWrite(LED1_PIN,LOW);
     
    // open serial communication for debugging
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");
     
    //  open software serial connection to the Bluetooth module.
    BTserial.begin(9600); 
    Serial.println("AltSoftSerial started at 9600"); 
          
    newData = false;

} // void setup()
 
 
void loop()  
{
       checkSwitch();
       recvWithStartEndMarkers();                // check to see if we have received any new commands
       if (newData)  {   processCommand();  }    // if we have a new command do something about it
}



/*
****************************************
* Function checkSwitch()
* checks the status of a button switch and turns an LED on or off depending on the switch status
* 
* passed:
*  
* global: 
*      switch1_State
*      LED1_State
*      oldswitch1_State
*
* Returns:
*          
* Sets:
*      switch1_State
*      LED1_State
*      oldswitch1_State
*/
void checkSwitch()
{
     // Simple toggle switch function with very simple debouce.
     boolean state1 = digitalRead(SWITCH1_PIN); delay(1);
     boolean state2 = digitalRead(SWITCH1_PIN); delay(1);
     boolean state3 = digitalRead(SWITCH1_PIN); delay(1);

     if ((state1 == state2) && (state1==state3))   
     { 
          switch1_State = state1;  
          if ( (switch1_State == HIGH) && (oldswitch1_State == LOW) )
          {
               LED1_State = ! LED1_State; 
              
              [color=red]off_time = millis() + 5000;[/color]
              
                
               if( ( LED1_State == HIGH) [color=red]&& (millis()>=off_time)[/color] )  
               {  
                    
                    
                    
                    BTserial.print("<L,1,1>" );  
                    digitalWrite(LED1_PIN,HIGH);  
                    Serial.println("Sent - <L,1,1>");    
               }
                                           
               else                     
               {  
                   BTserial.print("<L,1,0>");   
                   digitalWrite(LED1_PIN,LOW);   
                   Serial.println("Sent - <L,1,0>");    
               }    
          }          
          oldswitch1_State = switch1_State;
      }
}






/*
****************************************
* Function processCommand
* parses data commands contained in receivedChars[]
* receivedChars[] has not been checked for errors
* 
* passed:
*  
* global: 
*       receivedChars[]
*       newData
*
* Returns:
*          
* Sets:
*       receivedChars[]
*       newData
*/
void processCommand()
{

     Serial.print("receivedChars = ");  
     Serial.println(receivedChars);

    if (strcmp ("L10",receivedChars) == 0) 
    {
        digitalWrite(LED1_PIN,LOW);
        LED1_State = LOW; 
        Serial.println("LED1 LOW");  
    }
    
    else if (strcmp ("L11",receivedChars) == 0)
    {
        digitalWrite(LED1_PIN,HIGH);
        LED1_State = HIGH; 
        Serial.println("LED1 HIGH");  
    }




   
    receivedChars[0] = '\0';
    newData = false;
   
}


// function recvWithStartEndMarkers by Robin2 of the Arduino forums
// See  http://forum.arduino.cc/index.php?topic=288234.0
/*
****************************************
* Function recvWithStartEndMarkers
* reads serial data and returns the content between a start marker and an end marker.
* 
* passed:
*  
* global: 
*       receivedChars[]
*       newData
*
* Returns:
*          
* Sets:
*       newData
*       receivedChars
*
*/
void recvWithStartEndMarkers()
{
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
     char rc;
 
     if (BTserial.available() > 0) 
     {
          rc = BTserial.read();
          if (recvInProgress == true) 
          {
               if (rc != endMarker) 
               {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx > maxDataLength) { ndx = maxDataLength; }
               }
               else 
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { recvInProgress = true; }
     }
}

Hi guys!I can't find the way to turn the led after the delay of 10 seconds!Could someone help me with the code?I tried to turn it off automatically by using millis but i find difficulties.
I included inside to the code the :

unsigned long off_time; //what a added
off_time = millis() + 5000;
(millis()>=off_time)

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3);
 

// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;

// Constants for hardware
const byte LED1_PIN = 4;
const byte SWITCH1_PIN = 5;

// general variables
unsigned long off_time;  //what a added

boolean LED1_State = false;
boolean switch1_State = false;
boolean oldswitch1_State = false;

void setup()  
{
    // Set the button switch pin for input
    pinMode(SWITCH1_PIN, INPUT); 

    // Set the red LED pin for output and make it LOW
    pinMode(LED1_PIN, OUTPUT); 
    digitalWrite(LED1_PIN,LOW);
     
    // open serial communication for debugging
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");
     
    //  open software serial connection to the Bluetooth module.
    BTserial.begin(9600); 
    Serial.println("AltSoftSerial started at 9600"); 
          
    newData = false;

} // void setup()
 
 
void loop()  
{
       checkSwitch();
       recvWithStartEndMarkers();                // check to see if we have received any new commands
       if (newData)  {   processCommand();  }    // if we have a new command do something about it
}



/*
****************************************
* Function checkSwitch()
* checks the status of a button switch and turns an LED on or off depending on the switch status
* 
* passed:
*  
* global: 
*      switch1_State
*      LED1_State
*      oldswitch1_State
*
* Returns:
*          
* Sets:
*      switch1_State
*      LED1_State
*      oldswitch1_State
*/
void checkSwitch()
{
     // Simple toggle switch function with very simple debouce.
     boolean state1 = digitalRead(SWITCH1_PIN); delay(1);
     boolean state2 = digitalRead(SWITCH1_PIN); delay(1);
     boolean state3 = digitalRead(SWITCH1_PIN); delay(1);

     if ((state1 == state2) && (state1==state3))   
     { 
          switch1_State = state1;  
          if ( (switch1_State == HIGH) && (oldswitch1_State == LOW) )
          {
               
               LED1_State = ! LED1_State; 
              
              off_time = millis() + 5000;
              
                
               if( ( LED1_State==HIGH)  && (millis()>=off_time) )  
               {  
                    
                    
                    digitalWrite(LED1_PIN,HIGH);
                    BTserial.print("<L,1,1>" );  
                      
                    Serial.println("Sent - <L,1,1>");    
               }
                                           
               else 
              
                                   
               {  
                   BTserial.print("<L,1,0>");   
                   digitalWrite(LED1_PIN,LOW);   
                   Serial.println("Sent - <L,1,0>");    
               }    
          }          
          oldswitch1_State = switch1_State;
      }
}






/*
****************************************
* Function processCommand
* parses data commands contained in receivedChars[]
* receivedChars[] has not been checked for errors
* 
* passed:
*  
* global: 
*       receivedChars[]
*       newData
*
* Returns:
*          
* Sets:
*       receivedChars[]
*       newData
*/
void processCommand()
{

     Serial.print("receivedChars = ");  
     Serial.println(receivedChars);

    if (strcmp ("L10",receivedChars) == 0) 
    {
        digitalWrite(LED1_PIN,LOW);
        LED1_State = LOW; 
        Serial.println("LED1 LOW");  
    }
    
    else if (strcmp ("L11",receivedChars) == 0)
    {
        digitalWrite(LED1_PIN,HIGH);
        LED1_State = HIGH; 
        Serial.println("LED1 HIGH");  
    }




   
    receivedChars[0] = '\0';
    newData = false;
   
}


// function recvWithStartEndMarkers by Robin2 of the Arduino forums
// See  http://forum.arduino.cc/index.php?topic=288234.0
/*
****************************************
* Function recvWithStartEndMarkers
* reads serial data and returns the content between a start marker and an end marker.
* 
* passed:
*  
* global: 
*       receivedChars[]
*       newData
*
* Returns:
*          
* Sets:
*       newData
*       receivedChars
*
*/
void recvWithStartEndMarkers()
{
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
     char rc;
 
     if (BTserial.available() > 0) 
     {
          rc = BTserial.read();
          if (recvInProgress == true) 
          {
               if (rc != endMarker) 
               {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx > maxDataLength) { ndx = maxDataLength; }
               }
               else 
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { recvInProgress = true; }
     }
}

TWO_WAY_LED_CONTROL_1_LED.ino (5.68 KB)