Trouble with Serial Input

Hi,

I have this sketch in were I change (int maxnum) the number of cycles needed, in the code. I want to be able to input the number of cycles through the serial port. The second set of code shows my attempt at this. When I run the code the loop only runs the line of code once. I don't know what I am doing wrong.

int valve1 =  11;
int valve2 = 12;    
int maxnum = 10;  // CHANGE VALUE TO TOTAL AMOUNT OF CYCLES NEEDED !!!   
int cycles = 0;      
int count = 0;


void setup()   {                
 pinMode(valve1, OUTPUT); 
 pinMode(valve2, OUTPUT);    
 Serial.begin(9600);
 Serial.println("Cycles"); 
}


void loop()                     
{

 if (cycles < maxnum)
   digitalWrite(valve1, HIGH);   
   delay(500);                  
   digitalWrite(valve1, LOW);    
   delay(1000);  

if (cycles < maxnum)
   digitalWrite(valve2, HIGH);  
   delay(500);            
   digitalWrite(valve2, LOW); 
   delay(1000);  

   count = count+1; 
   cycles = cycles+1; 

 if (count < maxnum)
   Serial.println(count);

    



}
int valve1 = 11;
int valve2 = 12;    
int cycles = 0;      
int result;
char rx_byte = 0;
String rx_str = "";
boolean not_number = false;    
 
 void setup() {
  Serial.begin(9600);
  Serial.println("ENTER NUMBER OF CYCLES");
  pinMode(valve1, OUTPUT); 
  pinMode(valve2, OUTPUT);    
  
  
}

void loop() {
  if (Serial.available() > 0) {    // is a character available?
    rx_byte = Serial.read();       // get the character
    
    if ((rx_byte >= '0') && (rx_byte <= '9')) {
      rx_str += rx_byte;
    }
    else if (rx_byte == '\n') {
      // end of string
      if (not_number) {
        Serial.println("Not a number");
      }
       {

   if (cycles < result)
   digitalWrite(valve1, HIGH);   
   delay(500);                  
   digitalWrite(valve1, LOW);    
   delay(1000);      
   
    
   digitalWrite(valve2, HIGH);   
   delay(500);                  
   digitalWrite(valve2, LOW);    
   delay(1000);   

  cycles = cycles+1;

   }
      not_number = false;         // reset flag
      rx_str = "";                // clear the string for reuse
    }
    else {
      // non-number character received
      not_number = true;    // flag a non-number
    }
  } // end: if (Serial.available() > 0)
}

To get an idea of what is happening, and therefore how to fix it, try printing (Serial.println) rx_str at strategic points in your code..

There is a thread that EVERY new Arduino user ought to be required to memorize:
http://forum.arduino.cc/index.php?topic=396450.0

There are several others that discuss the stupidity of using the String class on the Arduino that should be required reading.

STOP using the String class NOW.

STOP using delay().

      }
       {

START paying attention to where curly braces are required, where they are optional, and where they are stupid. I'll leave it to you to figure out which category each of these falls into, but I'll bet that you can guess, since I mentioned them at all, that one of them falls into the last category.

To supplement the link in Reply #2, there is a simple user-input example in Planning and Implementing a Program

...R