0

As per title, I have 3 LEDs connected to an Uno. Also connected is an HC-05.

edit: forgot to add a question, my bad. Is there any glaring major issues in this code that I've overlooked? Not looking for anyone to write my code for me, just want to understand what I messed up and an idea of how to fix.

const int RED=2;
const int BLUE=4;
const int GREEN=6;

String inputString;
String buttonId;

char junk;

void setup() {
  Serial.begin(9600);
  pinMode(RED, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(GREEN, OUTPUT);
  digitalWrite(RED,LOW);
  digitalWrite(BLUE,LOW);
  digitalWrite(GREEN,LOW);
}

void loop()
{
  if(Serial.available()){
  while(Serial.available())
    {
      char inChar = (char)Serial.read(); //read the input
      inputString += inChar;        //make a string of the characters coming on serial
    }
    Serial.println(inputString);
    while (Serial.available() > 0)  
    { junk = Serial.read() ; }
    if(inputString == "r_on"){      // control red led
      digitalWrite(RED, HIGH);  
    }else if(inputString == "r_off"){   
      digitalWrite(RED, LOW);
    }
    inputString = "";   
    if(inputString == "b_on"){      // control blue led
      digitalWrite(BLUE, HIGH);  
    }else if(inputString == "b_off"){  
      digitalWrite(BLUE, LOW);
    }
    inputString = "";
    if(inputString == "g_on"){      // control green led
      digitalWrite(GREEN, HIGH); 
    }else if(inputString == "g_off"){
      digitalWrite(GREEN, LOW);
    }
    inputString = "";
  }
}

Picture of my App inventor block program in imgur link below

https://imgur.com/DlIdpTH

  • Was there a question to go with this? – Delta_G Apr 28 '20 at 2:09
  • I see a huge bug in the beginning where you are throwing away data without knowing what it is. But I don't know if that's what you're asking about. Reading data is way faster than it arrives, so if you send "r_on" that first available will be true when the 'r' gets there. Nothing else has made it yet so it exits. Now you got a chance that maybe when the '_' gets there its on the first available call, but that might happen right before it gets there and then the second one catches it and throws it away. Now we play the same russian roulette game with the 'o' and the 'n'. – Delta_G Apr 28 '20 at 2:13
  • In the end you don't know which characters you got and which ones you threw away. Throwing away serial data or "clearing the buffer" as noobs like to say is almost never a good idea. There shouldn't be junk in there unless you put it there. There's no reason to throw it away. Just don't send it if you don't want it. – Delta_G Apr 28 '20 at 2:14
  • So, the code just throws away the string before it can even reach the if statements? Or am I misunderstanding? – JacobR Apr 28 '20 at 3:00
  • The one that says junk=Serial.read() and then you don't do anything with junk. That is throwing away data. You don't know that junk was a good character or a bad character. You seem to think that when the first if statement catches that it will read the whole entire string. But there's no guarantee that the whole string has arrived yet. You're at 9600 baud (why so slow?) so there's about a millisecond between characters. In that time your loop function can repeat hundreds of times. It's not going to sit in that while loop waiting for the whole message to get there. – Delta_G Apr 28 '20 at 3:02

Your Answer

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

Browse other questions tagged or ask your own question.