I am using a lesson on line, it calls for an if, but it will not work. I am trying to light up an rgb led. Everything works by direct 5v lead. I have also used 2 different arduino boards. I can get the program to work with an else comand which I believe overrides the if. Frustrated Hicksy
Got any code we can review?
Are you using current limit resistors also?
Hicksy:
I am using a lesson on line, it calls for an if, but it will not work. I am trying to light up an rgb led. Everything works by direct 5v lead. I have also used 2 different arduino boards. I can get the program to work with an else comand which I believe overrides the if. Frustrated Hicksy
We cannot possibly help you without seeing your sketch. I am certain if works fine and you have a bug or a wiring issue.
Please provide your sketch and a wiring diagram/description.
You haven't done this if (someCondition);
by any chance?
This is a copy of the program that Paul McWorter put out lesson 21 RGB, LEDS.
I have proven that the circuit is ok by placing 5v directly to pin 8,9, & 10.
int redPin=8;
int greenPin=9;
int bluePin=10;
String myColor;
String msg="What Color 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(bluePin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite(redPin,LOW);
}
}
I also placed another command after the first if.
I placed this
else{
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWrite,(blePin,LOW);
When I addwd this too the program the leds worked??????
I believe that this overrode the if and tells me that the if does not work for some reason.
Hicksy
Line ending on the serial monitor?
Please remember to use code tags when posting code.
Please rest assured that if is not broken.
Trust us, if works, the issue is something else you haven't thought of, and as pointed out the most
likely candidate is you're passing a newline character from the Serial monitor, so your myColor string
is either "red\n", "green\n" or "blue\n", none of which match an if clause.
readString sits reading characters for a second before returning - it doesn't strip newlines. Perhaps
you should use readStringUntil() ?
Note that these functions return String, not string, so care may be needed in places. I think the ==
operator knows what to do here, but its worth checking that
String("red") == "red"
works as expected
I do appreciate all of the replies that you sent but I have to remind you that the lesson laid out was exactly as I wrote it and I can't deviate for it.. yes what you said may work but Paul wrote this program out I copied it exactly the way he did it on u-tube and it works. I still am questioning if my if does not work correctly. I don't know if that is a Arduino program fault that I have or is my computer not identifying the if. I also have two Arduino boards and both fault.
Again please do not take offence for your effort but I have to make it come out the same way he wrote it.
Hicksy
PS Don't forget the fact that I placed an else command in there and that made it work which I think actually overrides the if statement. Open for comments on this thanks
else{
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWritbluePin,LOW);
}
Hicksy:
PS Don't forget the fact that I placed an else command in there and that made it work which I think actually overrides the if statement. Open for comments on this thankselse{
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,LOW);
digitalWritbluePin,LOW);
}
The else works because none of the strings matched because most likely your problem is the line ending configuration of the serial monitor...as mentioned numerous times. Change that and I bet it works.
You put 5V on an output pin that was in the LOW state? :o
Yes. I used pin 8,9,10. And I took a lead from 5v and touched the led to each one and the led light up so I know that the mechanics of the circuit are ok. Hicksy
Have you sorted out the line ending yet?
How did that go?
Hicksy:
Yes. I used pin 8,9,10. And I took a lead from 5v and touched the led to each one and the led light up so I know that the mechanics of the circuit are ok. Hicksy
Well, they were OK, right up to the point you shorted an output pin.
Hicksy:
I do appreciate all of the replies that you sent but I have to remind you that the lesson laid out was exactly as I wrote it and I can't deviate for it.. yes what you said may work but Paul wrote this program out I copied it exactly the way he did it on u-tube and it works.
Just to reiterate what people have been saying above, are you sure that your serial monitor is set to "No line ending"?
To use that code "as-is" you need to make sure the serial monitor is not adding characters to the string "behind the scenes."
Hicksy:
but I have to remind you that the lesson laid out was exactly as I wrote it and I can't deviate for it..
Did you notice the line-ending setting in Paul's video (around 2:54). And obviously you can deviate if needed.
Hicksy:
Yes. I used pin 8,9,10. And I took a lead from 5v and touched the led to each one and the led light up so I know that the mechanics of the circuit are ok. Hicksy
And in the process, you might have damaged the Arduino. Although it's safe to feed 5V into a pin that's configured as input, it's not safe to do so on a pin that is configured as output (where it can be LOW). So if it's all not working after fixing the line-ending, you will have to try 3 different pins. For the example code that you showed, you can use the analog (input) pins, configured as outputs. That might not be possible later in the lesson (I did not go any further than 2:54).
Did you notice the line-ending setting in Paul's video (around 21:00).
It's lesson 20, not 21,BTW.
(That's half an hour of my life I'm not getting back )
Hicksy:
I am using a lesson on line, it calls for an if, but it will not work. I am trying to light up an rgb led. Everything works by direct 5v lead. I have also used 2 different arduino boards. I can get the program to work with an else comand which I believe overrides the if. Frustrated Hicksy
Actually it’s tutorial 20.
At 20:58 observe the serial monitor window.
At the bottom of the window are some pop-down menu choice widgets, there are three on the right hand side, the first of those should read “no line ending”.
You have not given any indication that you have seen the advices here and confirmed that that selection has been properly made. Or already is, which seems unlikely, as it would explain your difficulty.
Since this is independent of the code, you are allowed and in fact it is necessary for you to adjust in this way your serial monitor’s behavior to match that of your tutor’s.
I fault him for not pointing that out. Well he may have done, but I am not going to watch the whole video, I’m on the older side...
a7