Arduino uno issues

 Pretty straight forward code, user asks for input and then gets results.
 Using Arduino ide 2.2.1 on PC i core 5 ,Windows 10 home.
 So when I use this code, it turns on pin 10 no matter what i put in ,red, green, blue. 

Also I have tried it with a second Arduino and same results, also the computer sees the Uno as com3 but when i plug in a different one it sees it as com6.
Positively no problem with the wiring of this RGB led. If I take 5 vdc to each led through a 330 ohm resistor they all light up correctly.
Short of trying a third board, I am lost . I know that there is no issue with crossed wires because I tested as above . Just wondering if I got static electric that may have messed up the IC.

int redPin=8;
int greenPin=9;
int bluePin=10; 
String msg= "What color led do you want on ?" ;
String myColor;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(msg);
  
  
  while(Serial.available()==0){

  }

myColor=Serial.readString();

  if (myColor==redPin)
  
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);

if(myColor==greenPin);

digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,LOW);

if(myColor==bluePin);

digitalWrite(bluePin,HIGH);
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);

}

you compare a String (text) to a number

1 Like

myColor is declared as a String.

redPin, greenPin and bluePin are all declared as int.

Where did you get the idea that you can compare a String to an int ?

Now, if you did something to the effect of

   int myColorNum = myColor.toInt();
   if (myColorNum == redPin) {
      digitalWrite(redPin,HIGH);
      digitalWrite(greenPin,LOW);
      digitalWrite(bluePin,LOW);
   }

you might begin to get somewhere. Notice the enclosing {...} around the digitalWrite calls?

Also, this

if(myColor==greenPin);

does absolutely nothing. The trailing semicolon ends the if statement.

If you formatted your code properly, you would see something like the following:

int redPin=8;
int greenPin=9;
int bluePin=10; 
String msg= "What color led do you want on ?" ;
String myColor;

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
   pinMode(redPin,OUTPUT);
   pinMode(greenPin,OUTPUT);
   pinMode(bluePin,OUTPUT);
}

void loop() {
   // put your main code here, to run repeatedly:
   Serial.println(msg);
  
  
   while(Serial.available()==0){

   }

   myColor=Serial.readString();

   if (myColor==redPin)
      digitalWrite(redPin,HIGH);
      
   digitalWrite(greenPin,LOW);
   digitalWrite(bluePin,LOW);

   if(myColor==greenPin);

   digitalWrite(redPin,LOW);
   digitalWrite(greenPin,HIGH);
   digitalWrite(bluePin,LOW);

   if(myColor==bluePin);

   digitalWrite(bluePin,HIGH);
   digitalWrite(redPin,LOW);
   digitalWrite(greenPin,LOW);

}

With the exception of the very first digitalWrite(redPin,HIGH) statement, not a single one of your digitalWrite calls are conditionally executed. They are all executed unconditionally. And the very last thing you do is set the bluePin HIGH, and redPin and greenPin LOW. Assuming a common cathode RGB LED, it will light up blue.

You never read from Serial??
Try this maybe..

int redPin = 8;
int greenPin = 9;
int bluePin = 10;
char* msg = "What color led do you want on ?" ;
char myColor;

byte state = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {

  if (state == 0) {
    state++;
    Serial.println(msg);
  } else {

    if (Serial.available()) {
      myColor = Serial.read();
      switch (myColor) {
        case 'r': digitalWrite(redPin, HIGH);
          digitalWrite(greenPin, LOW);
          digitalWrite(bluePin, LOW);
          state = 0;
          break;
        case 'g': digitalWrite(redPin, LOW);
          digitalWrite(greenPin, HIGH);
          digitalWrite(bluePin, LOW);
          state = 0;
          break;
        case 'b': digitalWrite(redPin, LOW);
          digitalWrite(greenPin, LOW);
          digitalWrite(bluePin, HIGH);
          state = 0;
          break;
      }
    }
  }
}

good luck.. ~q

Saw my code mistake and tried to edit my post but it was too late. `

int redPin=8;
int greenPin=9;
int bluePin=10; 
String msg= "What color led do you want on ?" ;
String myColor;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(msg);
  
  
  while(Serial.available()==0){

  }

  myColor=Serial.readString();
  
  if (myColor==redPin){
  
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);
}
if(myColor==greenPin){

digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,LOW);
}
if(myColor==bluePin){

digitalWrite(bluePin,HIGH);
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
}
}

Still got the issue mentioned in Post # 2..
Comparing a String to and int..
~q


int redPin = 8;
int greenPin = 9;
int bluePin = 10;
String msg = "What color led do you want on ?";
String myColor;
void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println(msg);


  while (Serial.available() == 0) {
  }

  myColor = Serial.readString();

if (myColor == redPin) {

    digitalWrite(redPin, HIGH);
}
if (myColor == greenPin) {

    digitalWrite(redPin, HIGH);
}

if (myColor == redPin) {

    digitalWrite(redPin, HIGH);
}
}

More simplified , issue is the Uno is seen as com3 and another on same port when plugged in is seen as com 6 .Nor really concerned about that just wondering why my digital outs are acting strangly.Thanks for your patience.

You have already been told 3 times that you are comparing a String to an int and that it doesn't work.

The first time in Arduino uno issues - #2 by J-M-L

The second time in Arduino uno issues - #3 by van_der_decken

And the third time in Arduino uno issues - #6 by qubits-us

And you were given code to convert the String to an int so you could compare it.

Are you not reading?

This compares the string received to the value contained in redPin which is 8..
Try loading mine..
~q

That's normal behavior..

~q

Sorry everyone, wasn’t getting my messages and kept sending what I thought was a correction. Wasn’t ignoring you. Thanks I will read your message

Thank you, I will try that.

I just wanted to thank all thoes that took their valuable time to read and beat me over the head to try to correct me. I have since found that yes I messed up the code. I also found that one of my Arduinos (Fundrino) actually has an issue. It will not put out any
signal on the digital pins. I replaced it with an Arduino and all works .code below;

int greenPin=9;
int bluePin=10;
String myColor;
String msg= " What Color led do you want ? ";
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println(msg);

while(Serial.available()==0){

}

myColor=Serial.readString();

if (myColor=="red"){


digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,LOW);
}

if (myColor=="green"){
  

digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,LOW);
}
if(myColor=="blue"){
digitalWrite(redPin,LOW);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin,HIGH);
}
}
type or paste code here

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.