Problem with Serial [solved]

Hi, I am new on arduino. And I have a problem!
I’m working with Serial, I wrote a code to execute different tasks if I insert different numbers.
But when I insert one number, I have to wait until it executes the whole part asked for this number and only then I can insert another one .
Except, what I really wanted is for the code to stop executing when I insert a new number, and start executing the new part right away!
I hope I was clear and I hope someone could help :slight_smile:

Here is my code for now (I am working on OTTO Bot if you are familiar with it _)

if(Serial.available()){
danceNum = Serial.parseInt();
switch(danceNum){
case 1:
Serial.println(“PRIMERA PARTE”);
servos();
primera_parte();
break;
case 2:
Serial.println(“SEGUNDA PARTE”);
//for(int i=0;i<4;i++) servo*.SetPosition(90);*

  • servos();*
  • segunda_parte();*
  • break;*
  • case 3:*
  • Serial.println(“moon walk”);*
  • servos();*
    _ moonWalkLeft(4,t2);_
    _ moonWalkRight(4,t
    2);_
  • break;*
  • case 4:*
  • Serial.println(“crusaito”);*
  • servos();*
    _ crusaito(1,t*8);_
  • break;*
  • case 5:*
  • Serial.println(“crusaito”);*
  • servos();*
    _ drunk(t*8);_
  • break;*
  • case 6:*
  • Serial.println(“crusaito”);*
  • servos();*
  • kickLeft(t);*
  • kickRight(t);*
  • break;*
  • default:*
  • Serial.print(“no dance was received”);*
  • }*
  • delay(500);*
    _ for(int i=0;i<4;i++) servo*.SetPosition(90);_
    _
    delay(500);_
    _ for(int i=0;i<4;i++) servo.detach();
    //tone(buzzer,1000,2000);// tone of 1000 frequency for 2 seconds*
    * }*_

A) In that case, your functions should at least check Serial.available() once in a while and break off if characters are present. There may be other ways, but this may be the cleanest. Your functions may have to return a result that indicates that they did not complete because the user wanted to make a change.

B) Your original code probably did not have smileys or italics. To avoid this and other issues, please follow these instructions…

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.
  5. Please provide links to any libraries that are used
    (look for statements in your code that look like #include ). Many libraries
    are named the same but have different contents.

Before posting again, you should read the three locked topics by Nick Gammon near the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

Your parts are causing delays, either in delay() calls or loops.

Put “if (Serial.available())return;” inside every loop.

Turn delay() calls into loops: delay(MILLISECONDS) becomes:

  for (unsigned long m = millis(); millis() - m < MILLISECONDS;) 
    if (Serial.available())
      return;

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:

One you have read how to use the forum, check out this tutorial Serial Input Basics

johnwasser:
Your parts are causing delays, either in delay() calls or loops.

Put "if (Serial.available())return;" inside every loop.

Thank you, it was very helpful . Problem solved