Controlling a two switches same time

I want to control a two switches at a same time.. Both of them Will start at same time and close
close each at different timings.
It may be a simple logic but i am having a hard time doing it.

int pin_A = 13;
int pin_B = 12;
int timeA =0;
int timeB = 0;
int time_running =0;
#define buffer_size 80
char buffer[buffer_size] = "";
void setup(){
Serial.begin(9600);
  Serial.println("Enter  the time 1");
   timeA = Serial_Check();
  Serial.println(timeA);
  Serial.println("Enter the time 2");
   timeB= Serial_Check();
   Serial.println(timeB);
  
}
void loop(){

  digitalWrite(pin_A,HIGH);
  unsigned long timeA_starting = millis();
  digitalWrite(pin_B,HIGH);
  unsigned long timeB_starting = millis();
  
  if(timeA==timeB)
  {
    
    while((time_running=millis()-timeA_starting)<timeA){
    // Wait Here
    }
    digitalWrite(pin_A,LOW);
    digitalWrite(pin_B,LOW);
  }else if(timeA<timeB)
  {
    while((time_running = millis()-timeB_starting<timeB)){
      while(time_running<timeA){
      // wait here Until the time A Completed
      }
      digitalWrite(pin_A,LOW);
      //wait here until time B completed
    }
    digitalWrite(pin_B,LOW);
  }else
  {
    while((time_running = millis()-timeA_starting)<timeA){
      while(time_running<timeB){
        //wait until the time B completed
        }
        digitalWrite(pin_B,LOW);
        //wait here until the timee B Completed
    }
    delay(1000);
  }
}
  
  // Serial Check Function to check the monitor and send the value back
  int Serial_Check(){
    while(!Serial.available()){
      // wait till the user enters the data
    }
    char incoming_byte = Serial.read();
    int byte_return;
    if (incoming_byte = '/r'){
      byte_return = atoi(buffer);
      return byte_return;
    }else{
      buffer[buffer_size+1]  = incoming_byte;
      buffer[buffer_size] = '\0';
    }
  }
  1. why i am getting "0" on the Serial monitor.
  2. Does this Code Work?

Hello,

You should really avoid "useless" while loops or delays. Learn to do non blocking code, or you will regret it soon or later, maybe not in this simple code, but when you will do something more advanced :slight_smile:

Something that you should read: http://www.gammon.com.au/forum/?id=11425&reply=1#reply1

Also:if (incoming_byte = '/r'){Did you mean '\r' instead?

Or even

if (incoming_byte == '\r'){

Mind you, as

      while(time_running<timeA){
      // wait here Until the time A Completed
      }

this loop will never end because nothing changes in it, does it matter ?

I want to control a two switches at a same time.

Switches are usually controlled by the user. You really can't program the switches. So, please explain what you are really trying to do.

i want to switch ON two led's at the same time..
there is some delay on executing a instruction .. presently i won't bother about it.
On two led's one after another(~sametime)

digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);

and OFF those LED's At Different Timings

say.. LED1 after 1 sec
LED2 after 2sec

How to Do it with knowing elapsed time.

Look at the demo several things at a time

And have you looked at the Thread planning and implementing a program ?

...R

Robin2:
Look at the demo several things at a time

And have you looked at the Thread planning and implementing a program ?

...R

Hey Robin2 thanks for the thread..

Can anyone tell me What i am doing wrong in this code.
it doesn't Showing any errors and not Showing any Output on the Serial monitor Either.

#define DATABUFFERSIZE      80
char dataBuffer[DATABUFFERSIZE+1]; //Add 1 for NULL terminator
byte dataBufferIndex = 0;
String question_1 = "Enter a value";
void setup(){
  Serial.begin(9600);
  ask_question(question_1);
  while(!getSerialString()){
    //wait here
}
int a = atoi(dataBuffer);
Serial.println("the value of A is::");
Serial.println(dataBuffer);



}
void loop(){
  
}

void ask_question(String question){
Serial.println(question);
}

boolean getSerialString(){
    byte dataBufferIndex = 0;
    while(Serial.available()>0){
        char incomingbyte = Serial.read();

            dataBufferIndex = 0;  //Initialize our dataBufferIndex variable

            if(incomingbyte=='\r'){
                dataBuffer[dataBufferIndex] = 0; //null terminate the C string
                //Our data string is complete.  return true
                return true;
            }
            else{
                dataBuffer[dataBufferIndex++] = incomingbyte;
                dataBuffer[dataBufferIndex] = 0; //null terminate the C string
            }

    }
   
    
    return false;
}

Add the following line immediately following the line Serial.begin(9600)

Serial.println("Program Starting ...");

and see what happens.

If that does not print it probably means the Serial Monitor is not using the same baud rate as the Arduino.

If it does print, then add some other Serial.println() statements into your code to allow you to see how far it gets before the problem arises.

...R

The Serial monitor is working fine.

Robin2:
Add the following line immediately following the line Serial.begin(9600)

Serial.println("Program Starting ...");

and see what happens.

If that does not print it probably means the Serial Monitor is not using the same baud rate as the Arduino.

If it does print, then add some other Serial.println() statements into your code to allow you to see how far it gets before the problem arises.

...R

I am attaching a snapshot of output.

I am getting a Empty line In the Serial monitor.

Correction in code : I reinitialized dataBufferIndex in while loop..., I removed It
there is no change in output

I send a value in "123" through serial monitor and placed Serial.print in the other places.
when i placed it in the while starting...
it is printing 3 times ..
in if loop it 1 time
in else loop 3 times.

Capture.PNG

That is good progress.

You know the Serial monitor is correctly receiving stuff.

Next question is where, in your program, is the code that is printing "loop statement in"?

It looks like it is repeating over and over. Why do think it might be doing that?

...R

Robin2:
Why do think it might be doing that?

...R

I don't understand what are you trying to say...

I places Serial.println("loop statement") in the loop function to know is the code reaching loop or struck in setup function.
code reaching loop function and while loop is also repeating .
But I don't know what i made wrong in the code.
I think i am receiving a blank string in for dataBuffer variable.. Is that the reason why it is printing a empty line?

Your problem is most likely that you always reset dataBufferIndex to 0. You should reset it once you received the '\r', for the next time a data string is sent to the arduino :wink:

anilkunchalaece:
Correction in code : I reinitialized dataBufferIndex in while loop..., I removed It

I removed it...
But still getting the same.

What about posting your code?

#define DATABUFFERSIZE      80
char dataBuffer[DATABUFFERSIZE+1]; //Add 1 for NULL terminator
byte dataBufferIndex = 0;
String question_1 = "Enter a value";
void setup(){
  Serial.begin(9600);
  ask_question(question_1);
  while(!getSerialString()){
    //wait here
  }
  int a = atoi(dataBuffer);
  Serial.println("the value of A is::");
  Serial.println(dataBuffer);



}
void loop(){

}

void ask_question(String question){
  Serial.println(question);
}

boolean getSerialString(){
  byte dataBufferIndex = 0;
  while(Serial.available()>0){
    char incomingbyte = Serial.read();

    if(incomingbyte=='\r'){
      dataBuffer[dataBufferIndex] = 0; //null terminate the C string
      //Our data string is complete.  return true
      return true;
    }
    else{
      dataBuffer[dataBufferIndex] = incomingbyte;
      dataBufferIndex++;
    }

  }


  return false;
}

Sorry.....

You are still always resetting dataBufferIndex to 0... I hate repeating myself but.. you have to reset it only after you have received the '\r'.

anilkunchalaece:

#define DATABUFFERSIZE      80

char dataBuffer[DATABUFFERSIZE+1]; //Add 1 for NULL terminator
byte dataBufferIndex = 0;
String question_1 = "Enter a value";
void setup(){
 Serial.begin(9600);
 ask_question(question_1);
 while(!getSerialString()){
   //wait here
 }
 int a = atoi(dataBuffer);
 Serial.println("the value of A is::");
 Serial.println(dataBuffer);

}
void loop(){

}

void ask_question(String question){
 Serial.println(question);
}

boolean getSerialString(){
 byte dataBufferIndex = 0;
 while(Serial.available()>0){
   char incomingbyte = Serial.read();

if(incomingbyte=='\r'){
     dataBuffer[dataBufferIndex] = 0; //null terminate the C string
     //Our data string is complete.  return true
     return true;
   }
   else{
     dataBuffer[dataBufferIndex] = incomingbyte;
     dataBufferIndex++;
   }

}

return false;
}



Sorry.....

You are trying to read the incoming bytes before they are all there.
Once at least 1 byte is available you read it but this happens so fast that there is nothing to read during the next pass through the while loop that checks for serial data.

As a crude test try this

  while(Serial.available()>0)
  {
    delay(100);
    char incomingbyte = Serial.read();

case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte

// terminator reached! process input_line here ...
process_data (input_line);

// reset buffer for next time
input_pos = 0;
break;

guix:
I hate repeating myself but.. you have to reset it only after you have received the '\r'.

Now i get It....
Sorry... I can't help it.., I am a newbie and Slow Learner :slightly_smiling_face: .......

anyway.....thanks for help.

No problem, just so you know, you could have made like this

boolean getSerialString(){
  static byte dataBufferIndex = 0;

That way it is created only once in this scope, thus it won't be reset each time :wink:

But yeah, Nick Gammon's method is much better than yours :wink: