0

I am using a Arduino Uno for an embedded systems course. My assignment is that I have to write a function that implements these specifications. Take an input from the user (‘R’, ‘G’, ‘B’) and display Red, Green or Blue. The function name must be dispColor(), the input must be char a and their is no return. My code is below, however whenever I type in an input I receive no output. Where is the error in my code?

String dispColor(char){

  char a = Serial.read();

  if (a == "R")
    Serial.print("Red");
  else if (a == "G")
    Serial.print("Green");
  else if (a == "B")
    Serial.print("Blue");

}

void setup() {
// put your setup code here, to run once:
  Serial.begin(9600);
  String dispColor();
}

void loop() {
// put your main code here, to run repeatedly:

}

My Updated code

void dispColor(char a){
  if(Serial.available()){
        a = Serial.read();
        if(a == 'R')
        Serial.print("Red");
        else if(a == 'G')
        Serial.print("Green");
        else if(a == 'B')
        Serial.print("Blue");
    }
  }

void setup() {
    Serial.begin(9600);   
    Serial.println("Please type in R, G, or B.");
    dispColor();
}

void loop() {

}
7
  • Have you initialized Serial to begin with? If you did, are you sure you opened Serial in the same baudrate? Commented Oct 11, 2019 at 21:11
  • 1
    Use double quotes for strings e.g. "Red". Use single quotes for single characters, e.g. if (a == 'R') Commented Oct 11, 2019 at 21:13
  • @MiradilZeynalli Yes, in the setup function. Commented Oct 11, 2019 at 21:14
  • Then it is as @user3386109 said. You dont compare strings, in a way you did. Using single quotes will solve the problem. If you want to compare strings, use strcmp function Commented Oct 11, 2019 at 21:16
  • 1
    Moreover, you wrote function as follows: function takes an argument of type char and returns a String. However, as you mentioned, function should not return anything so it should be void. Moreover, you should read Serial in loop and call that function while passing read charachter Commented Oct 11, 2019 at 21:19

2 Answers 2

1

As says the comment in setup ("// put your setup code here, to run once:", that code will be executed only once, so when you're ready to "type an input", there will not be any code running to read it.

Thus, one thing you'll definitely need to do is move dispColor to loop.

There are a few more mistakes:

  • You're comparing a char with a String
  • You should be passing a parameter to dispColor, not reading from within it
  • You should probably only be calling dispColor if there's input available.

Have a look at https://www.arduino.cc/reference/en/language/functions/communication/serial/read/ to get started!

Sign up to request clarification or add additional context in comments.

3 Comments

Please edit your question and add a new section with your updated code (don't modify the original code).
I've added another section with the updated the code.
It doesn't look like you've addressed any of the points I listed, but only added a check for whether a signal was available on the serial port. Please change your code to incorporate the points mentioned and post an update. If you don't understand something, feel free to ask specific questions!
0

I figured it out!

My new code

void setup() 
{
Serial.begin(9600);   
Serial.println("Please type in R, G, or B.");  
}

void dispColor(char a)
{
while(!Serial.available());
a = Serial.read();
 if(a == 'R')
    Serial.println("Red");
      else if(a == 'G')
    Serial.println("Green");
      else if(a == 'B')
    Serial.println("Blue");
    Serial.print('\n');
}

void loop() {
char a;
dispColor(a);

}

1 Comment

One thing that remains: you should read from Serial in loop and pass that as to dispColor - at the m.j moment you're ignoring (overwriting) the parameter a in dispColor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.