1

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

      }

    }

}
  • You can make it easier by using commands which are only 1 character. For example '1' or 'A' to turn it on and '0' or 'a' to turn it off. Start with a good description of the commands and the communication. A Serial.read() read a character, and you can not read it again with another Serial.read(). – Jot Mar 29 at 8:36
  • But can I store the data from Serial.read() inside a variable without having to keep sending the data continuously, I want the relay to stay on until I send different data 0 or 1 etc – condo1234 Mar 29 at 8:40
  • @condo1234 You are rushing ahead. First describe the data/commands that you transmit. Do you want, for example, make it hard for yourself and be able to send the number "15"? Then step by step write it in code, if possible without the parseInt. – Jot Mar 29 at 8:56
  • 1
    Actually the second "Serial.read()" will always return -1, this should be interpreted as true. – Dorian Mar 29 at 13:35
  • 1
    @Dorian you are correct!! That was the issue – condo1234 Mar 30 at 2:27
1

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);
    }
  }
}
  • Ok thanks that makes sense, I will try send with 1 and 0 and see if that works – condo1234 Mar 29 at 10:33
  • Sorry I meant I already tried Serial.parseInt() and that outputs 0 when output relayOn in serial monitor, tried the same as your above code – condo1234 Mar 29 at 10:35
  • it is not about what number you send, but how you read it. the answer explains the 0 – Juraj Mar 29 at 10:35
  • 1
    It's not fair to delete an answer with usefull comments from OP then after the issue was solved to show up with it. The answer wasn'there when I made my comments. – Dorian Mar 30 at 8:34
  • 1
    @Dorian, I am really sorry. I deleted the answer, because OP indicated that the problem is something else and the answer didn't solve it. I wrote first a similar comment then yours. It was 3th or 4th comment and was totally ignored by OP and other commenters. Then I wrote the answer and deleted the comment. Then the answer seemed to be off... – Juraj Mar 30 at 9:33
3

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 :)

0

Try using serial interrupt in arduino

void loop() {
}

void serialEvent() 
{
  while (Serial.available()) 
    {
       char inChar = (char)Serial.read();
      Serial.print("Data: ");
      Serial.println(inChar);
       if (inChar == '1') 
        {
              digitalWrite(ledPin,HIGH);
              break;
        }
       else
        {
              digitalWrite(ledPin,LOW);
              break;
        }
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.