Beginner project - Simple RGB LED control with switches

Status
Not open for further replies.

Matt_

Member
Hi everyone,

I am a newcomer to the world of programming and microcontrollers. I am slowly learning how to use the Teensy with Teensyduino, and as a first simple project I am trying to control the color and brightness of a single RGB LED. As a start, I have followed these tutorials, and taken bits of code from them:

- https://www.pjrc.com/teensy/tutorial2.html (use of analog/PWM outputs & variables)
- https://www.pjrc.com/teensy/tutorial3.html (inputs/switches)

What I wanted was to be able to control the brightness of each color in 7 steps, with a + and a - pushbutton for each color. Here is how it looks:

RGB_breadboard.JPG


And the code:

Code:
int redPin =  12;
int greenPin =  15;
int bluePin =  14;

void setup()   {

  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

void loop()                     
{
  analogWrite(greenPin, greenIntensity);
  analogWrite(bluePin, blueIntensity);
  analogWrite(redPin, redIntensity);
   
  //red LED control
  if (digitalRead(7) == LOW) {
  redIntensity = redIntensity - 10;}
  delay(25);
  
  if (digitalRead(8) == LOW) {
  redIntensity = redIntensity + 10;}
  delay(25);
    
  if (redIntensity > 70) {
  redIntensity = 70;}
    
  if (redIntensity < 0) {
  redIntensity = 0;}
    
  //green LED control
  if (digitalRead(5) == LOW) {
  greenIntensity = greenIntensity - 10;}
  delay(25);
  if (digitalRead(6) == LOW) {
  greenIntensity = greenIntensity + 10;}
  delay(25);
    
  if (greenIntensity > 70) {
  greenIntensity = 70;}
    
  if (greenIntensity < 0) {
  greenIntensity = 0;}

  //blue LED control
  if (digitalRead(3) == LOW) {
  blueIntensity = blueIntensity - 10;}
  delay(50);
  if (digitalRead(4) == LOW) {
  blueIntensity = blueIntensity + 10;}
  delay(50);
    
  if (blueIntensity > 70) {
  blueIntensity = 70;}
    
  if (blueIntensity < 0) {
  blueIntensity = 0;}
    
  }

So, what I'm doing is limit the PWM to 70 for each color, and have each press on a button increase or decrease the PWM by 10, which give me my 7 steps. I used delay() to ensure that one keypress won't jump several steps when a button is held down. The max brightness at 70 is satisfying, so I'm not bothered by the fact that I'm not using the full range of values.

Right now, it works as intended, but I guess that my code may be ineficient; is there anything you can think of that I can read/learn to

The next steps of the project will involve using a key switch matrix (4x3) to have more controls without using too many pins, and modify the code to be able to control the overall brightness without affecting the color, but I'll have a bit of reading to do before I can tackle that.
 
Excellent!
The efficiency of your code will be more of an issue as you progress in your abilities and learn from working through tutorials and from what other people have posted. For now, I'd suggest to continue to work through the PJRC tutorials. Also there are a large number of tutorials In Adafruits lerning system that over a broad range of things that can be useful.
 
Status
Not open for further replies.
Back
Top