Having issues with loops

Hi everyone. Im currently trying to create a program that uses arduino to receive serial input from another program and use it to decide what position to turn the servo motors to. The issue that I'm having is that one servo is taking both inputs rather than one servo for one input. I tried to implements a loop with a counter so after the first servo writes, the count adds one then the loop for the second servo starts but instead the first servo, "micro", in this case, takes both inputs.

#include <Servo.h>

Servo micro;
Servo hitec1;
Servo hitec2;
String readString;

void setup() {
  // put your setup code here, to run once:
  hitec1.attach(6);
  micro.attach(3);
  Serial.begin(115200);

}

void loop() {

  // put your main code here, to run repeatedly:
while (Serial.available()) {
    delay (100);
  if (Serial.available() > 0) {
    char c = Serial.read();
    readString += c ;
   }
}
   if (readString.length()> 0) {


int i=0;
  
  if (i==0) {

    Serial.println(readString);
    int x;
    x = readString.toInt();
    micro.write(x);
    i = i+1;
    readString="";
    Serial.flush();
  }

    if (i==1) {
    //Serial.println(readString);
    int y;
    y = readString.toInt();
    hitec1.write(y);
    i = i+2;
    readString="";
   }
 

  
    Serial.flush();
    readString="";
    
  }
}

Read the Serial Basics tutorial - the way you try to read from serial (including the delay(100)) is calling for trouble

Also creating a local scoped non static variable i and initializing it to zero and then testing if it's possibly not zero is showing some mistrust in the compiler.... :slight_smile: (hint at where to look for your problem)

Serial.flush() probably does not do what you expect? (why would you need it for your print?)

First, use Ctrl-T in the IDE to format your code into a more common C style...it makes it easier to read. I've added some comments to your code:

#include <Servo.h>

Servo micro;
Servo hitec1;
Servo hitec2;
String readString;

void setup() {
  // put your setup code here, to run once:
  hitec1.attach(6);
  micro.attach(3);
  Serial.begin(115200);

}

void loop() {

  // put your main code here, to run repeatedly:
  while (Serial.available()) {
    delay (100);
    if (Serial.available() > 0) {
      //char c = Serial.read();       // No need for c
      readString += Serial.read();
    }
  }
  if (readString.length() > 0) {
    int i = 0;        // Given that you set i = 0 here...

    if (i == 0) {     // ...how could i be anything but 0 here??

      Serial.println(readString);
      int x = readString.toInt();     // Simplify
      micro.write(x);
      i++;                        // More common way to write this i = i + 1;
      readString = "";
      Serial.flush();             // Usually not needed
    }

    if (i == 1) {           //  Given it was 0, then an i++, how could it be anything else?
      //Serial.println(readString);
      int y = readString.toInt();
      hitec1.write(y);
      i = i + 2;                    // Why are you doing this??
      readString = "";
    }
    Serial.flush();       // Probably not needed
    readString = "";
  }
}

Personally, I would not use the String object, but use C strings build from char arrays instead. They are more reliable and use less memory. You need to print out your values in the code to see what's going on.

readString += Serial.read(); hum... read returns an int not a char.... not exactly sure what that does and what is overloaded in the String class as I don't use it at all. So there was a need for c possibly...

You missed an important comment/hint next to readString = ""; in the i==0 if section... what do you think readString will be in the next if section....

J-M-L:
Read the Serial Basics tutorial - the way you try to read from serial (including the delay(100)) is calling for trouble

Also creating a local scoped non static variable i and initializing it to zero and then testing if it's possibly not zero is showing some mistrust in the compiler.... :slight_smile: (hint at where to look for your problem)

Serial.flush() probably does not do what you expect? (why would you need it for your print?)

I was using Serial.flush() to try to get all of the serial data to be read before moving on. The reason for the odd initialization of i was because i was trying to change things around to see if i could get only the first or second loop to run and write to the servo.

Im reading the serial basics right now. thanks for linking them.

Flush empties the transfer buffer, sends whatever you wrote to Serial out before continuing - not dealing with incoming stuff

(Besides the weird serial management your issue is indeed with your i variable as it will always be reset to 0 and then to 1 as you go through that part of the code, at every loop)

J-M-L:
Flush empties the transfer buffer, sends whatever you wrote to Serial out before continuing - not dealing with incoming stuff

(Besides the weird serial management your issue is indeed with your i variable as it will always be reset to 0 and then to 1 as you go through that part of the code, at every loop)

How would you suggest i get it to go back and forth from 0 to 1 or 1 to 2, rather than being reset each time to 0. is it getting reset because its nested?

No it's reset because it's a local, non static variable.
Make your variable global or static (and use a meaningful name, like status or stage or currentMotor rather than i. you could create an enum to make the state even more readable)