user26203: I have a relay and bluetooth module connected to Arduino Uno. I send data such as digit 1 to the bluetooth module and I view it in the Serial Monitor. If I send 1 from serial monitor I get 4910 printed.
I would like to turn on the relay and have it stay on until I send a different digit. However, in my code below it doesnt work, the relay doesnt stay on. 
Only, if I put the digital.write under while(Serial.available()) it works. 
const int ledPin =  11;
int relayOn;

void setup() {

  //initialize serial port for logs
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);

}

void loop() {

  while (Serial.available()) {

      Serial.println("Data: ");

      Serial.println(Serial.read());

      if(Serial.read())
      {
        relayOn = Serial.parseInt(); 
      }

      while (relayOn > 1)
      {
        digitalWrite(ledPin, HIGH);

      }

    }

}


user37622: Your input is "5\n" with '\n' as line ending setting in Serial Monitor.
In the code you have first Serial.println(Serial.read());. It will read and print the '5'. Then you have if(Serial.read()). It will read the '\n'. It is not 0 so the next line is relayOn = Serial.parseInt(). It will find no input and times out returning 0.
if you want to use parseInt():
void loop() {
  if (Serial.available()) {
    Serial.print("Data: ");
    relayOn = Serial.parseInt(); 
    Serial.println(relayOn);
    if (relayOn > 1) {
      digitalWrite(ledPin, HIGH);
    }
  }
}


user51115: You could try something like (not tested):    
void loop() {
 if(Serial.available()){
   input = Serial.parseInt();
   Serial.print("Data: ");
   Serial.println(input);
   if(input == 1){
      digitalWrite(RELAY, HIGH);
      digitalWrite(LED, HIGH);
   }
   else if(input == 2{
      digitalWrite(RELAY, LOW);
      digitalWrite(LED, LOW);
   }
 }
}

don't forget to declare input variable, use 1 to turn on, 2 off :)

26203: <p>I have a relay and bluetooth module connected to Arduino Uno. I send data such as digit 1 to the bluetooth module and I view it in the Serial Monitor. If I send 1 from serial monitor I get 4910 printed.</p>

<p>I would like to turn on the relay and have it stay on until I send a different digit. However, in my code below it doesnt work, the relay doesnt stay on. </p>

<p>Only, if I put the digital.write under <strong>while(Serial.available())</strong> it works. </p>

<pre><code>const int ledPin =  11;
int relayOn;

void setup() {

  //initialize serial port for logs
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);

}

void loop() {

  while (Serial.available()) {

      Serial.println("Data: ");

      Serial.println(Serial.read());

      if(Serial.read())
      {
        relayOn = Serial.parseInt(); 
      }

      while (relayOn &gt; 1)
      {
        digitalWrite(ledPin, HIGH);

      }

    }

}
</code></pre>
 37622: <p>Your input is "5\n" with '\n' as line ending setting in Serial Monitor.</p>

<p>In the code you have first <code>Serial.println(Serial.read());</code>. It will read and print the '5'. Then you have <code>if(Serial.read())</code>. It will read the '\n'. It is not 0 so the next line is <code>relayOn = Serial.parseInt()</code>. It will find no input and times out returning 0.</p>

<p>if you want to use parseInt():</p>

<pre><code>void loop() {
  if (Serial.available()) {
    Serial.print("Data: ");
    relayOn = Serial.parseInt(); 
    Serial.println(relayOn);
    if (relayOn &gt; 1) {
      digitalWrite(ledPin, HIGH);
    }
  }
}
</code></pre>
 51115: <p>You could try something like (not tested):    </p>

<pre><code>void loop() {
 if(Serial.available()){
   input = Serial.parseInt();
   Serial.print("Data: ");
   Serial.println(input);
   if(input == 1){
      digitalWrite(RELAY, HIGH);
      digitalWrite(LED, HIGH);
   }
   else if(input == 2{
      digitalWrite(RELAY, LOW);
      digitalWrite(LED, LOW);
   }
 }
}
</code></pre>

<p>don't forget to declare input variable, use 1 to turn on, 2 off :)</p>
