MULTIPLE DELAY VALUE VIA BLUETOOTH SERIAL

i am trying to make an automation project based on arduino and relay here i am using basic led blink program

int led 2;
void setup() {

pinMode(2, OUTPUT);
}

void loop() {
digitalWrite(led, HIGH);
delay(1000); // 1st delay
digitalWrite(led, LOW);
delay(1000); //2nd delay
}

i want to take that 1st and 2nd delay time from user via bluetooth. i hope someone help me in this project

I HAVE WRITTEN THIS PROGRAM FOR MY LAST PROJECT WHICH SIMPLY TAKE DELAY VALUE FROM USER USING 4X4 KEYPAD AND DISPLAY 16X2 LED . I HOPE YOU ALL LIKE THIS PROJECT..

BUT I NEED TO TAKE THAT BOTH DELAY FROM USER VIA BLUETOOTH :---??????

THANKS IN ADVANCE

//programmed by Sandeep Automation //
// Use Buzzer as indication for application startup //
#include <Wire.h> 
#include <LiquidCrystal.h>
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns
// Define the Keymap

char keys[ROWS][COLS] = {
  {'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

 {'#','0','*','D'}

};


byte rowPins[ROWS] = { A0, A1, A2, A3 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { A4, A5,  9, 10 };//connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // KEYPAD CREATED

int relay = 7;                  //relay connected to pin 2
int buzzer = 8;                 // buzzer connected to pin 3
int onstat = 13;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup() {




  
                // initialize the lcd 
  lcd.begin(16 , 2);
  lcd.setCursor(1,0);
  lcd.print("SANDEEP");
  lcd.setCursor(1,1);
  lcd.print("AUTOMATION");
  delay(300);
  pinMode(buzzer, OUTPUT); 
  pinMode(relay,  OUTPUT);
  pinMode(onstat, OUTPUT);

  for (int i = 150; i <= 153; i++){
  digitalWrite(buzzer, 1);
  delay(i);
  digitalWrite(buzzer, 0);
  delay(i);
  }
  digitalWrite(buzzer, 1);
  delay(1000);
  digitalWrite(buzzer, 0);
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("Wait");
  delay(300);

  
  for (int i = 1; i <= 4; i++){
  lcd.clear();
  lcd.setCursor(1,1);
  lcd.print("SYSTEM INIT");
    delay(300);
  lcd.clear();
  lcd.setCursor(1,1);
  lcd.print("SYSTEM INIT.");
    delay(300);
  lcd.clear();
  lcd.setCursor(1,1);
  lcd.print("SYSTEM INIT..");
    delay(300);
  lcd.clear();
  lcd.setCursor(1,1);
  lcd.print("SYSTEM INIT....");
    delay(300);
  lcd.clear();
  lcd.setCursor(1,1);
  lcd.print("SYSTEM INIT.....");
    delay(300);
  }
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("DEVICE IS ON");
    delay(600);
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.write(" WELCOME");
    delay(1000);
  lcd.setCursor(1,1);
  lcd.write("SANDEEP AUTOMATION");
    delay(1000);
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("ENTER OFF TIME");
  lcd.setCursor(1,1);
  lcd.print("THEN PRESS *");
    delay(2000);
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("ENTER ON TIME");
  lcd.setCursor(1,1);
  lcd.print("THEN PRESS #");// take value from user ON time
    delay(800);
}
  void loop()
  {
char buffer[4];
static int ontime = 0;
static int offtime = 0;

  int i=0;
    while (1)
    {
        char key = keypad.getKey();

        // If it's a number AND we have space left, add to our string
        if ('0' <= key && key <= '9' && i<3)
        {
            buffer[i] = key;
            i++;
        }
        // If it's a * or #, end
        else if ('#' == key || '*' == key)
        {
            // Null terminate
            buffer[i] = 0;

            // Convert to an integer
            int value = atoi(buffer);

            // Set the appropriate value
            if ('*' == key) ontime = value; 
            if ('#' == key) offtime = value; 
            break;
        }    
    }


    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("OFF TIME IS=");
    lcd.setCursor(1,1);
    lcd.print(offtime);
    lcd.print("-Minutes");
    delay(600);
    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("ON TIME IS=");
    lcd.setCursor(1,1);
    lcd.print(ontime);
    lcd.print("-Minutes");
 digitalWrite(buzzer,1);
 delay(500);
 digitalWrite(buzzer, 0);
 delay (100); 
 digitalWrite(buzzer,1);
 delay(500);
 digitalWrite(buzzer, 0);
 delay (100);

if (ontime > 0 && offtime > 0)
  
  {
  turn:
  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("ON TIME");
  lcd.setCursor(1,1);
  lcd.print(ontime);
  lcd.print("-MINUTES");
  digitalWrite(relay, HIGH); 
  digitalWrite(onstat, 0);
  delay(offtime*600);

  lcd.clear();
  lcd.setCursor(1,0);
  lcd.print("OFF TIME");
  lcd.setCursor(1,1);
  lcd.print(offtime);
  lcd.print("-MINUTES");
  digitalWrite(relay, LOW);
  digitalWrite(onstat, 1);
  
  delay(ontime*600);
    
 goto turn;
  
}
}

Please stop SHOUTING - it is rude, as well as hard to read.

Bluetooth is just serial-by-wireless. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

Can you explain it.. I mean how I implement that code for desirable result

sandeep9148:
Can you explain it.. I mean how I implement that code for desirable result

Have you tried any of the examples?

If you describe some specific point about an example that you don't understand I will try to help.

...R

Thanks Robin for helping me...
and i am extremely apologize for shouting

pal, actually i want to run an simple program as like led blink where i will put the relay instead of led but i want take on and off delay time from user via bluetooth through android phone application. As i seen your posts you have lot of knowledge of arduino programming you can help me in this project.

sandeep9148:
As i seen your posts you have lot of knowledge of arduino programming you can help me in this project.

I will try to help if you get stuck but I won't write the program for you.

Please re-read Reply #4

...R

thanks my program is here its working on bluetooth android serial monitor

//programmed by Sandeep Automation //
// Use Buzzer as indication for application startup //

int j;
int k;
int relay = 7;                  //relay connected to pin 7
int buzzer = 8;                 // buzzer connected to pin 8
int onstat = 13;               // led status for on/off status


void setup() {

  pinMode(buzzer, OUTPUT); 
  pinMode(relay, OUTPUT);
  pinMode(onstat, OUTPUT);

  for (int i = 150; i <= 153; i++){
  digitalWrite(buzzer, 1);
  delay(i);
  digitalWrite(buzzer, 0);
  delay(i);
  }

  
 
  delay(300);
  Serial.begin(9600);
 
  Serial.println("WELCOME");
   delay(1000);
  Serial.println("SANDEEP AUTOMATION");
   delay(1000);
  
Serial.println("PLEASE ENTER THE OFF TIME");
 
  // take value from user ON time
    while(!Serial.available())
    {/*do nothing*/}

    j = Serial.parseInt();
   
  Serial.print("OFF TIME IS =");
  Serial.print(j);
  Serial.println(" Minutes");

  delay(3000);
 
    Serial.println("PLEASE ENTER THE ON TIME");
  delay(3000);
 k = Serial.parseInt();
    
Serial.print("ON TIME IS =");

  
  Serial.print(k);
  
     Serial.println(" Minutes");
      //buzzer for programme startup in loop
 digitalWrite(buzzer,1);
 delay(500);
 digitalWrite(buzzer,0);
 delay(100); 
  digitalWrite(buzzer,1);
 delay(500);
 digitalWrite(buzzer,0);
 delay(100);
  
 delay(100);  

looper: 
  digitalWrite(relay, HIGH); 
  digitalWrite(onstat, 0);
  delay(k*600);
  Serial.print("Off time is-");
  Serial.println(k);
  digitalWrite(relay, LOW);
  digitalWrite(onstat,1);
  delay(j*600);
  Serial.print("On time is-");
  Serial.println(j);  
goto looper;
}

its works well but it has problem when i take first delay value "j" its wait for serial command but not waiting for second delay "k" so i have added delay(3000); but its take "0" value sometime if value not given in specify time "3000"

Have a look at the simple user input example in Planning and Implementing a Program.

Building an effective user input system is not trivial and will probably take a lot more code than the actual operating part of the program.

The easiest way to be sure you get two numbers from the user is to get him/her to enter them both at the same time in a form like
<numberA, numberB>
and then you can use the parse example in the link I have you earlier to extract the values.

You must also take into account the fact that an Arduino works much faster than serial data arrives. If you look at my examples you will see that they may take a number of iterations to receive a complete message.

I don't see any loop() function in your program.

Don't use GOTO unless you are a very experienced programmer.

...R

THANKS ROBIN FOR REPLY! BUT MY SKETCH WORKS WELL BUT I WANT YOUR HELP TO REPLACE DELAY () INTO MILLIS SO I CALL RESET FUNCTION FOR RESET THE ARDUINO AND GIVE THE NEW COMMAND AGAIN

  //programmed by Sandeep Automation //
  // Use Buzzer as indication for application startup //
  
  int j;
  int k;
  int relay = 7;                  //relay connected to pin 7
  int buzzer = 8;                 // buzzer connected to pin 8
  int onstat = 13;
  char data = 0; 
  
  void(* resetFunc) (void) = 0; //declare reset function @ address 0
  void setup() {
  
    pinMode(buzzer, OUTPUT); 
    pinMode(relay, OUTPUT);
    pinMode(onstat, OUTPUT);
  
  
     
    for (int i = 300; i <= 306; i++){
    digitalWrite(buzzer, 1);
    delay(i);
    digitalWrite(buzzer, 0);
    delay(i);
    }
  
    
    
     
  
    delay(300);
    Serial.begin(9600);
     Serial.println("WELCOME");
     delay(1000);
    Serial.println("SANDEEP AUTOMATION");
     delay(1000);
    
  Serial.println("PLEASE ENTER THE OFF TIME");
   
    // take value from user ON time
      while(!Serial.available())
      {/*do nothing*/}
  
      j = Serial.parseInt();
   
     digitalWrite(buzzer,1);
   delay(200);
   digitalWrite(buzzer,0);
   delay(100); 
    Serial.print("OFF TIME IS =");
    Serial.print(j);
    Serial.println(" Minutes");
  
    delay(3000);
   
      Serial.println("PLEASE ENTER THE ON TIME");
    delay(3000);
      while(!Serial.available())
      {/*do nothing*/}                      //second value wait
   k = Serial.parseInt();
     digitalWrite(buzzer,1);
   delay(200);
   digitalWrite(buzzer,0);
   delay(100);   
  Serial.print("ON TIME IS =");
  
    
    Serial.print(k);
    
       Serial.println(" Minutes");
        //buzzer for programme startup in loop
   digitalWrite(buzzer,1);
   delay(500);
   digitalWrite(buzzer,0);
   delay(100); 
    digitalWrite(buzzer,1);
   delay(500);
   digitalWrite(buzzer,0);
   delay(100);
    
   delay(100);  
  
  looper: 
    digitalWrite(relay, HIGH);
     digitalWrite(buzzer,1);
   delay(500);                                  
   digitalWrite(buzzer,0);
   delay(100);  
    digitalWrite(onstat, 0);
    delay(k*600);                         //REPLACE THIS DELAY INTO MILLIS
    Serial.print("Off time is-");
    Serial.println(k);
    digitalWrite(relay, LOW);
     digitalWrite(buzzer,1);
   delay(500);
   digitalWrite(buzzer,0);
   delay(100); 
    digitalWrite(onstat,1);
    delay(j*600);                          //REPLACE THIS ALSO DELAY INTO MILLIS
   Serial.print("On time is-");
    Serial.println(j); 
    if(Serial.available() > 0)  // Send data only when you receive data:
  {
  data = Serial.read();      //Read the incoming data and store it into variable data
  Serial.print(data);        //Print Value inside data in Serial monitor
  Serial.print("\n");        //New line 
  if(data == 'r')            //Checks whether value of data is equal to 1 
  resetFunc();  //call reset
  else goto looper;
  }         
      
  goto looper;
  }

sandeep9148:
THANKS ROBIN FOR REPLY! BUT MY SKETCH WORKS WELL BUT I WANT YOUR HELP TO REPLACE DELAY () INTO MILLIS SO I CALL RESET FUNCTION FOR RESET THE ARDUINO AND GIVE THE NEW COMMAND AGAIN

You are still SHOUTING. If you do it again I won't bother to read your Post.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

There are dozens of delay()s in your program so you really need to learn how to use millis() and then completely re-write the program.

With a well-written program there should NEVER be a need for a software reset.

...R