Total noob here trying to learn

Hey all!

I'm new to the forum and have come seeking some help. I've been doing the adafruit lessons and have been using various resources from the internet in an attempt to learn and someday master arduino and the C language.

I'm stuck on a project that should be pretty easy but after a couple of days the code has stumped me. Not looking to be given the answer but rather to learn from my mistakes so I can use that knowledge in the future.

Here it goes,

I have a common anode RGB LED that I want to control by using three tactile switches to turn the LED from Red to Yellow to Green with a press of the corresponding button.

The bread board is setup with 5V to anode on LED, 330 ohm resistors on the color cathodes, red cathode to pin 11, green cathode to pin 10, blue cathode to pin 9. Pin 6 to red tactile switch, pin 5 to green tactile switch, pin 3 to blue tactile switch. All tactile switches to ground rail on breadboard.

The idea is when the arduino powers the LED will be off. When the first tactile button is pressed the LED goes to red high, when the center button is pressed the LED turns yellow, and when the last button is pressed the LED goes green high. (with no color mixing on each press from previous button presses, I,E, when red is high the yellow is mixed in. Should be when yellow is pressed the previous color doesn't run at the same time (if that makes any sense))

Here is what I have in the code so far:

int redLEDPin = 11;
int greenLEDPin = 10;
int blueLEDPin = 9;

int redSwitchPin = 5;
int greenSwitchPin = 5;
int blueSwitchPin = 3;

void setup()
{
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
pinMode(redSwitchPin, INPUT_PULLUP);
pinMode(greenSwitchPin, INPUT_PULLUP);
pinMode(blueSwitchPin, INPUT_PULLUP);
}

Here is what I cant wrap my head around, in the setup can I add something like digitalWrite (redLEDPin, LOW) on all three colors to start the LED off? or because it common anode does it have to be digitalWrite (redLEDPin, HIGH) on all three?

Or maybe I should be asking how to tell the arduino that it is a common anode so it sets the proper colors?

Next how do I set in the loop something to the effect of "if redSwitchPin goes LOW write redLEDPin to (255,0,0)
"if greenSwitchPin goes LOW set redLEDPin to (0,0,0) and greenLEDPin to (0,255,0)

I think I have the idea of what I want it to do, im just not sure how to translate that into the code. After three days of researching and learning, it's just not coming together for me.

Can anybody offer some advice? Much appreciations your way good sir's!

Mike

Hello,

Arduino Pins Can be sink or source for current (beware not exceed 20ma), you can use common anode leds and as well as common cathode leds. With CA leds digitalWiriting Pin to HIGH turns OFF led and LOW turns ON led thats all no other difference between them.

Don't be afraid of making mistakes when you are coding. Experiment more, if you want to learn "if" "else" statements, look for examples, learn by trial and error. Start with simple piece of code just one led and one button, then add second led experiment and add third led then add a photo resistor. Step by step you will notice that you are actually learning.

Arduino - Home > this place is your friend

http://arduino.cc/en/Reference/If

http://www.arduino.cc/en/Tutorial/Pushbutton > Just one button and one led example

I hope I am not be miss understood. I just encourage you to do more experiment

That's great advice! I appreciate anything that helps. It is frustrating to keep experimenting and the code doesn't compile after what feels like the fifty millionth time. I'm sure the feeling of accomplishment is great once it does compile.

Mike

When code does not compile that is you making a syntax error, either in the statement itself or the placement of brackets.
These are the easy ones to spot. Read the manual, there is a goo reference to the language in the help menu of the IDE.

When code compiles but does not do what you want this is a logic error and are much more difficult to find.

Here is what I cant wrap my head around, in the setup can I add something like digitalWrite (redLEDPin, LOW) on all three colors to start the LED off? or because it common anode does it have to be digitalWrite (redLEDPin, HIGH) on all three?

This is exactly correct. In your setup(), you should have digitalWrite(redLEDPin, HIGH); digitalWrite(greenLEDPin, HIGH); and digitalWrite(blueLEDPin, HIGH);

Next how do I set in the loop something to the effect of "if redSwitchPin goes LOW write redLEDPin to (255,0,0)
"if greenSwitchPin goes LOW set redLEDPin to (0,0,0) and greenLEDPin to (0,255,0)

If you're always going full brightness, you can use digitalWrite() all the time. For example:

  if (digitalRead(yellowSwitchPin) == LOW)  // yellow button is pressed
  {
    digitalWrite(redLEDPin, LOW);      // yellow = red + green
    digitalWrite(greenLEDPin, LOW);
    digitalWrite(blueLEDPin, HIGH);
  }

There is some confusion in your program in that you said you want the center pin to turn the LED yellow, but you don't have a yellowSwitchPin defined. Also, you have your redSwitchPin and your greenSwitchPin both defined as pin 5. I expect redSwitchPin should be 6.

That definitely helps to make sense of some of this. The yellow light that will come on from pressing the center button will be a color combo from the RGB to make yellow.

it would probably help me if instead of calling it a blue, call it yellow in the int part of the code.

I'll give it a shot in a little bit and let you know how it worked out!

Thanks again

Mike

once everything works that feeling priceless :slight_smile:

Okay so here is the code but i'm getting an error 40 "}" expected in void loop() on the last line. Any ideas? It looks right but obviously something is wrong and i'm not sure why.

int redLEDPin = 11;
int greenLEDPin = 10;
int blueLEDPin = 9;

int redSwitchPin = 6;
int yellowSwitchPin = 5;
int greenSwitchPin = 3;

void setup()
{
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
pinMode(redSwitchPin, INPUT_PULLUP);
pinMode(yellowSwitchPin, INPUT_PULLUP);
pinMode(greenSwitchPin, INPUT_PULLUP);
digitalWrite (redLEDPin, HIGH);
digitalWrite (greenLEDPin, HIGH);
digitalWrite (blueLEDPin, HIGH);
}

void loop()
{

if (digitalRead(redSwitchPin) == LOW)
{
digitalWrite (redLEDPin, LOW);
digitalWrite (greenLEDPin, HIGH);
digitalWrite (blueLEDPin, HIGH);
}
if (digitalRead(yellowSwitchPin) == LOW)
{
digitalWrite (redLEDPin, LOW);
digitalWrite (greenLEDPin, LOW);
digitalWrite (blueLEDPin, HIGH);
}
if (digitalRead(greenSwitchPin) == LOW)
{
digitalWrite (redLEDPin, HIGH);
digitalWrite (greenLEDPin, LOW);
digitalWrite (blueLEDPin, LOW);
}
error states problem is in the last line with "}"

Yeah, you need another }

Just add a line at the end with a }

Ahhh! I see, because I opened with a { at the void loop but never closed the loop, I had only closed the last statement in the code. You guys are amazing!

I love learning new stuff

Mike

If I understand what you are trying to accomplish. Use something like

void loop()
if(redbutton == HIGH){
digitalWrite(red, HIGH);
}
else{
  digitalWrite(red, LOW);
}

And repeat for other colors.

Use something like

Except that if you have a read button (switch), you should have a red pin, not a red. Just a pet peeve of mine.

I'm going to play around with some of the code a bit more and try to recreate what an actual traffic light would do. I.E. if red is triggered cycle yellow first for delay(5000) etc. Just learning all the steps. I'm sure as the lessons keep coming i'll have a bunch more questions. Especially when I start working on the big project that I want to do.

Mike

Is your yellow a nice yellowy color? Because a lot of times with RGB LEDs, the mixed colors are a little bit too green, or too red...

You can use the PWM that the Arduino supplies for you to adjust the green-ness of the color. Instead of doing digitalWrite(greenLEDPin, LOW); you can do analogWrite(greenLEDPin, 100); or so. In your case, since you're using common anode, 0 would be full on and 255 full off. 100 would theoretically be about 60%, if it were linear. But try it and see if you like it less green.

Since it is a clear RGB the color isn't really yellow but more a clearly defined red and green with a slight yellow hue. Ill have to try playing with a PWM to get it right or try diffusing it.

Okay another question,

Is it possible to set a known command on an integer that will output on two pins? I.E.

I have int yellowLEDPin = 9; and void setup (){ pinMode (yellowLEDPin, OUTPUT);

in the setup when I trigger the appropriate switch to LOW is there a way to say yellowLEDPin is redLEDPin, LOW and greenLEDPin, LOW instead of typing individual lines of code to create the yellow?

Mike

n the setup when I trigger the appropriate switch to LOW is there a way to say yellowLEDPin is redLEDPin, LOW and greenLEDPin, LOW instead of typing individual lines of code to create the yellow?

No.

Well... yes. There are many ways to do that in the program. Eventually it has to put out the bits to the port. The easy way for most of us to understand is to use digitalWrite(). That's why they have those. But the digitalWrite() routine calculates which port to set the bits on, and which bits to set. You could easily enough do it at a lower level if you want.

Red = pin 11 = B3
Green = pin 10 = B2
Blue = pin 9 = B1

So all three colors are on the B port. if the bit is 0, the LED is on. If the bit is 1, the LED is off.

Red = xxxx011x
Yellow = xxx001x
Green = xxx101x

Here's one way to code that:

const byte RGBMask = 0b11110001;
const byte RedBit = 0b00000110;
const byte YellowBits = 0b00000010;
const byte GreenBit = 0b00001010;

...

  if (digitalRead(redSwitchPin) == LOW)
    PORTB = PORTB & RGBMask | RedBit;
  else if (digitalRead(YellowSwitchPin) == LOW)
    PORTB = PORTB & RGBMask | YellowBits;
  else if (digitalRead(GreenSwitchPin) == LOW)
    PORTB = PORTB & RGBMask | GreenBit;

It's still a two step process. The compiler converts the PORTB assignment into two instructions. One for the AND and one for the OR. But your code has it in one statement.

But if you just want to do it with one statement, it's easier to just define a function to do it.

void GoRed()
{
  digitalWrite(redLEDPin, LOW);
  digitalWrite(greenLEDPin, HIGH);
  digitalWrite(blueLEDPin, HIGH);   // Actually, you could ignore the Blue LED Pin because it is never on
}

void GoYellow()
{
  digitalWrite(redLEDPin, LOW);
  digitalWrite(greenLEDPin, LOW);
  digitalWrite(blueLEDPin, HIGH);
}

void GoGreen()
{
  digitalWrite(redLEDPin, HIGH);
  digitalWrite(greenLEDPin, LOW);
  digitalWrite(blueLEDPin, HIGH);
}
...
  if (digitalRead(redSwitchPin) == LOW)
    GoRed();
  else if (digitalRead(yellowSwitchPin) == LOW)
    GoYellow();
  else if (digitalRead(greenSwitchPin) == LOW)
    GoGreen();

If you just don't want to type in all those digitalWrite(xxxLEDPin); instructions, you can just define a couple of arrays:

const byte switchPinArray[] = {6, 5, 3};  // Red, Yellow, Green
const byte ledPinArray[] = {11, 10, 9};  // Red, Green, Blue
const byte stateTable[][3] = {{LOW, HIGH, HIGH}, {LOW, LOW, HIGH}, {HIGH, LOW, HIGH}};

void setup()
{
  for (byte i = 0; i < 3; ++i)
  {
    pinMode(switchPinArray[i], INPUT_PULLUP);
    pinMode(ledPinArray[i], OUTPUT);
    digitalWrite(ledPinArray[i], HIGH);
  }
}

void loop()
{
  for (byte i = 0; i < 3; ++i)
    if (digitalRead(switchPinArray[i]) == LOW)
      for (byte j = 0; j < 3; ++j)
        digitalWrite(ledPinArray[j], stateTable[i][j]);
}