# Trouble with Serial Input

**URL:** <https://forum.arduino.cc/t/trouble-with-serial-input/521540>\
**Category:** Programming\
**Created:** [April 20, 2018, 9:02pm UTC](https://forum.arduino.cc/t/trouble-with-serial-input/521540 "2018-04-20T21:02:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BARBOZA1](https://avatars.discourse-cdn.com/v4/letter/b/5f8ce5/32.png) [@BARBOZA1](https://forum.arduino.cc/u/BARBOZA1)\
**Post date:** [April 20, 2018, 9:02pm UTC](https://forum.arduino.cc/t/trouble-with-serial-input/521540/1 "2018-04-20T21:02:09Z")

</div>

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.

```cpp
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);

    

}

```

```cpp
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)
}

```

---

<div class="post-metadata">

**Author:** ![GarethMoffatt](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@GarethMoffatt](https://forum.arduino.cc/u/GarethMoffatt)\
**Post date:** [April 20, 2018, 9:07pm UTC](https://forum.arduino.cc/t/trouble-with-serial-input/521540/2 "2018-04-20T21:07:13Z")

</div>

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..

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 20, 2018, 10:43pm UTC](https://forum.arduino.cc/t/trouble-with-serial-input/521540/3 "2018-04-20T22:43:15Z")

</div>

There is a thread that EVERY new Arduino user ought to be required to memorize:  
[http://forum.arduino.cc/index.php?topic=396450.0](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().

```cpp
      }
      {

```

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.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [April 21, 2018, 7:51am UTC](https://forum.arduino.cc/t/trouble-with-serial-input/521540/4 "2018-04-21T07:51:56Z")

</div>

To supplement the link in Reply #2, there is a simple user-input example in [Planning and Implementing a Program](http://forum.arduino.cc/index.php?topic=261445.0)

...R

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:08pm UTC](https://forum.arduino.cc/t/trouble-with-serial-input/521540/5 "2021-05-05T23:08:44Z")

</div>


