need Programming help

Hello all Arduino Lovers,

Hope, you running well,

I am trying to make program which can execute one instruction continuous on one serial command and and after receive another command at any time starts executing other instruction.
I need this program for five different serial commands namely a,b,c,d,e,

note that, command can be anytime interrupt program and according to command the instruction should be executed until not receive another command.

Please help.

I made it for 2 command but i don't know why it not running. it executes only on 2nd command.
first command executed

in_out.ino (447 Bytes)

Please read up HOW TO POST YOUR CODE
I can’t see it here on my tablet, because it’s noy inline with </> code tags
Edit the original in this case and we can take a look.

</code

void setup() {

Serial.begin(9600);
}

void loop()
{
char data;

while(Serial.available()>0)
{
data = Serial.read();
Serial.println(data);
}

if(data=='a')
{
Serial.println("omkar");
delay(200);
Serial.println(data);
data='a';

}
if(data=='b');
{
Serial.println("dave");
delay(200);
Serial.println(data);
data='b';
}
}

Good start, try again with CODE TAGS

[/
void setup() {

Serial.begin(9600);
}

void loop()
{
char data;

while(Serial.available()>0)
{
data = Serial.read();
Serial.println(data);
}

if(data=='a')
{
Serial.println("omkar");
delay(200);
Serial.println(data);
data='a';

}
if(data=='b');
{
Serial.println("dave");
delay(200);
Serial.println(data);
data='b';
}
}
]

omkarengineer:
I made it for 2 command but i don't know why it not running. it executes only on 2nd command.
first command executed

Are you saying that even if you send 'a' the code for 'b' is run?

I don't think it is relevant but this line is not doing anything useful as the variable data already has the value 'a'

data='a';

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

PS... You need to post your code between [code] [/code] tags

  if (data == 'b');What is that semicolon doing there ?

With it in place the code in the following block will be executed unconditionally

  if(data=='a')
  {
    Serial.println("omkar");
    delay(200);
    Serial.println(data);
    data='a';// the only reason you got here is because data already has the value 'a'.
    
  }

[
void setup() {

Serial.begin(9600);
}

void loop()
{
char data;

while(Serial.available()>0)
{
data = Serial.read();
Serial.println(data);
}

if(data=='a')
{
Serial.println("omkar");
delay(200);
Serial.println(data);
data='a';

}
if(data=='b')
{
Serial.println("dave");
delay(200);
Serial.println(data);
data='b';
}
}
]

OK, I’ll help

void setup() {
   Serial.begin(9600);
}
     
void loop()  {
   char data;
   if (Serial.available()>0)  {
       data = Serial.read();
       Serial.println(data);
       if (data=='a') {
          Serial.println("omkar");
          delay(200);
          Serial.println(data);
          // data='a';
       }
       if (data=='b')  {
           Serial.println("dave");
           delay(200);
           Serial.println(data);
           // data='b';
        }
     }
  }

I’m not saying this code works, it’s just the OPs code put inside tags.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

I finally make this code but its not working:

code:

void setup() 
{
   Serial.begin(9600);
}
     
void loop()  
{
   char data;

   if (Serial.available()>0)
  {
       data = Serial.read();

       Serial.println(data);

       if (data=='a') {

          Serial.println("omkar");

          delay(200);

          Serial.println(data);

                 }

       if (data=='b') 
{

           Serial.println("dave");

           delay(200);

           Serial.println(data);

        }
     }
  }

Maybe you would have more success if you listened to people here and answered some of their questions.

Robin2:
Are you saying that even if you send 'a' the code for 'b' is run?

I finally make this code but its not working:

It works as expected for me. What does it do when you run it ?

I don’t think OP liked my tight, indented code :wink:

BJHenry:

Are you saying that even if you send 'a' the code for 'b' is run?

Maybe you would have more success if you listened to people here and answered some of their questions.

You obviously have not yet helped with very many questions here or you would realize how often the symptoms are described in a very fuzzy way.
All you needed to say in Reply was "yes".
Have you carefully studied the link I gave you?

Apologies to everyone. Please ignore the above.

I had mistakenly thought that @BJHenry was the OP for this Thread. (Senior Moment)

...R