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';
}
}
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
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)
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.
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
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);
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.