Pages: [1]   Go Down
Author Topic: RGB LED Program  (Read 438 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int button1Pin = 2;  // pushbutton 1 pin
const int button2Pin = 3;  // pushbutton 2 pin
const int ledPin =  13;    // LED pin
const int LED3 = 10;

void setup()
{
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);

  // Set up the LED pin to be an output:
  pinMode(ledPin, OUTPUT);     
}


void loop()
{
  int button1State, button2State;  // variables to hold the pushbutton states

  // Since a pushbutton has only two states (pushed or not pushed),
  // we've run them into digital inputs. To read an input, we'll
  // use the digitalRead() function. This function takes one
  // parameter, the pin number, and returns either HIGH (5V)
  // or LOW (GND).

  // Here we'll read the current pushbutton states into
  // two variables:

  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  // Remember that if the button is being pressed, it will be
  // connected to GND. If the button is not being pressed,
  // the pullup resistor will connect it to 5 Volts.

  // So the state will be LOW when it is being pressed,
  // and HIGH when it is not being pressed.
 
  // Now we'll use those states to control the LED.
  // Here's what we want to do:
 
  // "If either button is being pressed, light up the LED"
  // "But, if BOTH buttons are being pressed, DON'T light up the LED"
 
  // Let's translate that into computer code. The Arduino gives you
  // special logic functions to deal with true/false logic:
 
  // A == B means "EQUIVALENT". This is true if both sides are the same.
  // A && B means "AND". This is true if both sides are true.
  // A || B means "OR". This is true if either side is true.
  // !A means "NOT". This makes anything after it the opposite (true or false).
 
  // We can use these operators to translate the above sentences
  // into logic statements (Remember that LOW means the button is
  // being pressed)
 
  // "If either button is being pressed, light up the LED"
  // becomes:
  // if ((button1State == LOW) || (button2State == LOW)) // light the LED
 
  // "If BOTH buttons are being pressed, DON'T light up the LED"
  // becomes:
  // if ((button1State == LOW) && (button2State == LOW)) // don't light the LED

  // Now let's use the above functions to combine them into one statement:
 
  if (((button1State == LOW) || (button2State == LOW))  // if we're pushing button 1 OR button 2
      && !                                               // AND we're NOT
      ((button1State == LOW) && (button2State == LOW))) // pushing button 1 AND button 2
                                                        // then...
  {
    digitalWrite(ledPin, HIGH);  // turn the LED on
    digitalWrite(LED3, HIGH);
    delay(500);
}
 
  else
  {
    digitalWrite(ledPin, LOW);  // turn the LED off
    digitalWrite(LED3, LOW);
   
}
     
  // As you can see, logic operators can be combined to make
  // complex decisions!

  // Don't forget that we use = when we're assigning a value,
  // and use == when we're testing a value for equivalence.
}


Here, I have a simple program that works perfectly fine. It turns on a LED light when either of two push buttons are pressed. My question is, I wanted to change the color of a RGB LED light with a push button. For example, when it the button is pressed, it will turn green, pressed again, it will turn blue etc.

Please help, I searched google and got nothing, just more complicated projects such as RFID LED light cubes (what is that??)
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

many people are look at this post, yet no one is replying????
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 602
Posts: 33373
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

many people are look at this post, yet no one is replying????
Because the question is very vague.

Look at the State change detection example in the File -> Examples -> 02 Digital
Use button pushes to increment a count.
use the vale of that count to turn on what LEDs you want.

Don't post code that is full of totally irrelevant comments.
Logged

Czech Republic
Offline Offline
Sr. Member
****
Karma: 3
Posts: 311
Absolute Beginner, yet.
Newbie in Arduino
since May 2013
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My question is, I wanted to change the color of a RGB LED light with a push button.

You must connect R to pinX , G to pinY , and B to pinZ (X, Y and Z - any digital output pins), see here for my solution:
Garage door alarm project
Logged

Arduino Uno R3, Arduino Mega 2560 R3, Ethermega (R3), and some Ethernet Shields (R3), Chronodots, TMP36 sensors, DS18B20 sensors, LCDs/TFTs, RS232C to learn and play...

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25795
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you waggling the built-in pullup on pin "LED3" ?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int redLEDPin = 11;
int greenLEDPin = 10;
int blueLEDPin = 9;
 
int redSwitchPin = 7;
int greenSwitchPin = 6;
int blueSwitchPin = 5;
 
int red = 0;
int blue = 0;
int green = 0;
 
void setup()
{
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT); 
  pinMode(redSwitchPin, INPUT_PULLUP);
  pinMode(greenSwitchPin, INPUT_PULLUP);
  pinMode(blueSwitchPin, INPUT_PULLUP);
}
 
void loop()
{
  if (digitalRead(redSwitchPin) == LOW)
  {
    red ++;
    if (red > 255) red = 0;
  }
  if (digitalRead(greenSwitchPin) == LOW)
  {
    green ++;
    if (green > 255) green = 0;
  }
  if (digitalRead(blueSwitchPin) == LOW)
  {
    blue ++;
    if (blue > 255) blue = 0;
  }
  analogWrite(redLEDPin, red);
  analogWrite(greenLEDPin, green);
  analogWrite(blueLEDPin, blue); 
  delay(10);
}

here is a code for changing the light intesity that I came up with. I actually wanted to change color but instead it changes the intensity of the light
I am sorry if I had made any rude comments.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

many people are look at this post, yet no one is replying????

Quote
   
RGB LED Program
« on: September 06, 2013, 10:08:32 pm »
...
« Reply #1 on: September 06, 2013, 10:19:24 pm »

That is truly shocking. A whole 11 minutes have elapsed, and no reply! No wonder you bumped the thread.

How to use this forum

Now read point 13. Forum etiquette.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 602
Posts: 33373
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Replace:-
Code:
red ++;
With:-
Code:
red += random(20);

Similarly
Code:
// green ++;
green += random(25);
...........
 //blue ++;
 blue += random(30);


Finally add this line in the setup function
Code:
randomSeed(analogRead(0));
Logged

Pages: [1]   Go Up
Jump to: