continuous serial input, code only looping twice instead of infinitely

Hello, I apologize if this is posted in the wrong place, but as I am working with FSR sensors and Linear Actuators, I felt this Forum page fit best. I’m trying to have the serial monitor continuously monitoring for input, while continuing to loop through the void loop at the same time. Code is currently only looping through a state twice and then exiting the loop for some reason. For clarification:

  1. Uploads code and opens serial monitor
  2. nothing happens
  3. Input’s “a” into serial monitor.
  4. state is set to 1
  5. optionA runs twice (used print statements to verify this in the serial monitor) and then stops looping for some reason, even through state is still equal to 1
  6. if I enter “a” into the serial monitor again, optionA proceeds for another 2 loops.

Here’s my code:

// ---------------------------------------- 
#define AIN11 4
#define AIN21 2
#define PWMA1 3
#define STBY1 5

// -----------------------------------------------
const int FSR_PIN1 = A2; //Pin connected to FSR/resistor ****
const int feedback1 = A0; //potentiometer from actuator
int fsrADC1;

char inByte;
int state = 0;
void setup() {
  pinMode(feedback1, INPUT);//feedback from actuator

  pinMode(PWMA1, OUTPUT);
  pinMode(AIN11, OUTPUT);
  pinMode(AIN21, OUTPUT);
  pinMode(STBY1, OUTPUT);


void loop() {
  // read the sensor:
  if (Serial.available()) {
    inByte = (char);
      if (inByte == 'a')
        state = 1;

      if (inByte == 'b')
        state = 2;

      if (inByte == 'c')
        state = 3;
      if (state == 1) optionA();
      if (state == 2) optionB();

void fwd1(int speed1)
{  digitalWrite(AIN11, HIGH);
   digitalWrite(AIN21, LOW);
   analogWrite(PWMA1, speed1);
void rev1(int speed1)
{  digitalWrite(AIN11, LOW);
   digitalWrite(AIN21, HIGH);
   analogWrite(PWMA1, speed1);
void brake1()
{  digitalWrite(AIN11, HIGH);
   digitalWrite(AIN21, HIGH);

void standby1()
{  digitalWrite(STBY1, LOW);
void enable1()
{  digitalWrite(STBY1, HIGH);

void optionA ()
        fsrADC1 = analogRead(FSR_PIN1);                  
          int actuator_position1 = analogRead(feedback1);
        // -----------------  Define ROM Limits  -----------------------------------------

        if (actuator_position1> 700 || actuator_position1< 100)
        { fwd1(0);
       // ---------- Move Linear Actators from FSR value -------------------------------
        // LA 1
        if (fsrADC1 > 50 && actuator_position1<700)
          {fwd1(80); }
        else if (fsrADC1 < 10 && actuator_position1> 150)
          {rev1(80); }

void optionB()

Thanks for posting your code properly, on a first post!

as I am working with FSR sensors and Linear Actuators

Nothing jumps out at me in the code, but when motors are involved, unexpected behavior is very often due to power, wiring and electrical interference problems.

Forum members will want to see links to the product pages or data sheets for all the devices, motor drivers, the power supply and a wiring diagram (not Fritzing) that clearly shows how everything is powered and grounded.

See the "How to use this forum" post for details, and do tell us which Arduino you are using.