School Project: Actions Based on Serial Input Commands

Hello, my name is Erick and I am a student at URI. I’m fairly new to using Arduino but am familiar with coding and programming. I am trying to get a basic comparison of two strings of text. Basically I am trying to move the servo motor one way if the serial monitor input is open, and move it the opposite way if the input is closed. I’ve tried several ways to do this and nothing seems to be working for me. My code is attached below. Please help! Any and all help is greatly appreciated! Thank you!!

#include <Servo.h> //servo library
char mystring[5]; // allow up to ten characters
char openstring[] = "open"; // set this string equal to "open"
char closestring[] = "close"; // set this string equal to "close"
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(8);  // attaches the servo on pin 8 to the servo object 

  Serial.begin(9600);

  myservo.write(0); // set the servo position to zero

while (myservo.attached()== true){ // let the user know that the servo motor is attached and available
    Serial.println("Servo Motor 1 is connected");
    break;
  }
} 
 
void loop() 
{ 
 Serial.readBytes(mystring, Serial.read()); // store whatever is written in the serial monitor into mystring 

  while (Serial.available() > 0){ // look for serial data

  if ( mystring == openstring){ // compare the characters from mystring to openstring. if the two are equal then move the servo from 0 to 180

  for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 15ms for the servo to reach the position 
  } 
}

  }

   while (Serial.available() > 0){

  if ( mystring == closestring){ // compare the characters from mystring to closestring. if the two are equal then move the servo from 180 to 0

  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(10);                       // waits 15ms for the servo to reach the position 
  } 
  }
   }
  Serial.println(mystring); // check if data is being stored correctly
  Serial.println(openstring); // check if characters where stored correctly
  Serial.println(closestring);
}
 Serial.readBytes(mystring, Serial.read()); // store whatever is written in the serial monitor into mystring 

  while (Serial.available() > 0){ // look for serial data

Read stuff from Serial, and then see if there's something to read? This is second time this week I've seen a construct like this - is it some new fashion?

while (myservo.attached()== true){ // let the user know that the servo motor is attached and available
    Serial.println("Servo Motor 1 is connected");
    break;
  }

You attached it about four lines above - why wouldn't it be attached?

@Erick: before you post and while your code is still in the IDE, do a Ctrl-T to auto-format it. It makes it easier for us to help you.

econjack: Is this better?

#include <Servo.h> //servo library
char mystring[5]; // allow up to ten characters
char openstring[] = "open"; // set this string equal to "open"
char closestring[] = "close"; // set this string equal to "close"
Servo myservo;  // create servo object to control a servo 
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position 

void setup() 
{ 
  myservo.attach(8);  // attaches the servo on pin 8 to the servo object 
  Serial.begin(9600);
  myservo.write(0); // set the servo position to zero
} 

void loop() 
{ 
  while (myservo.attached()== true){ // let the user know that the servo motor is attached and available
    Serial.println("Servo Motor 1 is connected");
    break;
  }
  Serial.readBytes(mystring, Serial.read()); // store whatever is written in the serial monitor into mystring 
  while (Serial.available() > 0){ // look for serial data
    while ( mystring == openstring){ // compare the characters from mystring to openstring. if the two are equal then move the servo from 0 to 180
      for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
      {                                  //77 in steps of 1 degree 
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(10);                       // waits 15ms for the servo to reach the position 
      } 
    }
  }
  while (Serial.available() > 0){
    while ( mystring == closestring){ // compare the characters from mystring to closestring. if the two are equal then move the servo from 180 to 0
      for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
      {                                
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(10);                       // waits 15ms for the servo to reach the position 
      } 
    }
  }
  Serial.println(mystring); // check if data is being stored correctly
  Serial.println(openstring); // check if characters where stored correctly
  Serial.println(closestring);
}

Yep!