Go Down

Topic: keypad control of RGB LEDs (Read 495 times) previous topic - next topic

DanKainen

So Far:
I am controlling a Velman RGB shield with a keypad. I assign three values for RGB to some of the keys to make the color I want by pressing a single key. The code I'm including shows examples.

By using a for/else if loop with a delay, I can increment R, G, or B by pressing one of six keys. For instance, pressing "A" will increment Red, "B" will increment Green, and "C" will increment Blue. The time delay does the job in 10 seconds for each. I've not yet implemented decrement, but that seems straightforward.

The Problem:
I can't stop the increments at any point. Once I press the key, it increments til done, with no way to stop it at a certain level. It would be nice to be able to hold a key down until it got to the right level and then release the key to stop it. Any advice?

Here's a sample of code:

#include <Keypad.h>

//standard keypad code to be integrated with Velman RGB shield so RGB values can
//be input with numeric keypad

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
// shift input pins so they won't interfere with Velman RGB shield
byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 4}; //connect to the column pinouts of the keypad


//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

//Pin numbers for Velman RGB shield
int BLUE = 6;                   
int GREEN = 5;
int RED = 3;

void setup(){
}

void loop(){
char customKey = customKeypad.getKey();

{if (customKey =='1'){  //green-white color
analogWrite(RED, 255);
analogWrite(GREEN, 75);
analogWrite(BLUE, 0);
}
else if (customKey == '2') // amber color
{
analogWrite(RED, 255);
analogWrite(GREEN, 40);
analogWrite(BLUE, 0);
}
else if (customKey == '3')
{
analogWrite(RED, 255);  // bluer white
analogWrite(GREEN, 75);
analogWrite(BLUE, 20);
}
else if (customKey == '4')   //dimmer bluer white
{
analogWrite(RED, 127);
analogWrite(GREEN, 37);
analogWrite(BLUE, 10);
}
else if (customKey == 'A')  //increment red
{for (int i=0; i <= 255; i++){
analogWrite(RED, i);
delay(10);
}

} else if (customKey == 'B')   //increment green
{for (int i=0; i <= 255; i++){
      analogWrite(GREEN, i);
      delay(10);

}
}else if (customKey == 'C')   //increment blue
{for (int i=0; i <= 255; i++){
      analogWrite(BLUE, i);
      delay(10);
}}
else if (customKey == '8')  //turn all LED's off
{analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
}}}


 
   


Delta_G

Yes, that is what a for loop does.  It runs from the beginning to the end State before it finally exits and lets the rest of the program run.  If that's not the behavior you want then how about just adding one to the variable there and get rid of the for loop altogether.  You should probably also include some code there to make sure the value stays in bounds (constrain function). 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up