Go Down

Topic: RGB effects/patterns, help needed with switch cases (Read 877 times) previous topic - next topic

Cryod

Oct 07, 2012, 08:08 pm Last Edit: Oct 17, 2012, 09:40 pm by Cryod Reason: 1
Hi,

I'm trying to control rgb lightstrip with arduino. I'm driving the strip with ULN2003a, and everything works on that side. I have 4x4 keypad attached, and I'm mapping different patterns to different buttons, like 1 for red, 2 for blue etc. Everything on the hardware side works. My current code bases on cases and it seems to work except few hiccups.
Edit. I also implemented bluetooth control, it works great when using microController BT software on my Galaxy S3. I'm planning to write a how-to for this "mood lamp", as it seems to be different than all the ones found on the internet, mainly by being controllable.

First problem:
I'd like to be able to turn color on AND off by pressing the corresponding button. I tried making redState integer and boolean, but I wasn't able to get them working.

Edit. I got it working, propably typo or something.

Second problem:
I'm able to set individual color levels with minimum and maximum levels and apply them instantly. I want that to work with controlling all three levels at the same time. I have the limits, but I don't know how to apply these levels instantly. If I use the same method I used on individual colors, all will be lit even thought they weren't lit before. Any ideas?

Edit. And again solved, with previously broken redState variables.

Third problem:
I have two buttons left, and I'd like to have two different fading patterns, first with traditional color circle and the second one could be random, or something completely different, I'm open for ideas! I have found several fading codes, but haven't got them working in my code, as they all were thought to be run on their own. Has anyone made something like this and have the code lying around? And just to be modest with my wishes, it would be neat to be able control the fading speed.

Fourth problem:
And to be a total ass, the lamp has to work as a strobe light. I don't have a clue how to apply these patterns inside switch cases, any help? I tried, as you can see, but that doesn't work at all.

I appreciate the help already!

Here's the code, feel free to copy!
I left the bluetooth part away as it wasn't important for this purpose, will attach the whole code when ready!

Code: [Select]

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
const int r = 9;     // Red Ledpin
const int g = 10;    // Green Ledpin
const int b = 11;    // Blue Ledpin
int rBri = 255; // initial red brightness
int gBri = 255; // initial green brightness
int bBri = 255; // initial blue brightness
int am = 25;   // brightness change amount
int incomingByte = 0; // for incoming serial data
int rState = 0; // red state
int gState = 0; // green state
int bState = 0; // blue state
int strobe=0; //strobe state
int dA=50; //initial strobe amount
int dAc=10; // strobe change amount

// Define Keymap
char keys[ROWS][COLS] = {
 {'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'E','0','F','D'}
};
// First pin to arduino 12, others to 2,3,4
byte rowPins[ROWS] = { 12, 2, 3, 4 };
// Last 4 pins to arduino
byte colPins[COLS] = { 5, 6, 7, 8 };

// Create the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// Set ledpins and serial
void setup()
{
 pinMode(r, OUTPUT);
 pinMode(g, OUTPUT);
 pinMode(b, OUTPUT);
 Serial.begin(9600);
}

void loop()
{
 char key = kpd.getKey();
 if(key)  // Check for a valid key.
 {
   switch (key)
   {
     case '1': // Red color, if on, should switch off
       if( rState == 0)
      {
       analogWrite(r, rBri);
       Serial.println(rBri);
       Serial.println(key);
       rState = 1;
      }
     else {
       rState=0;
       digitalWrite(r,LOW);
       Serial.println(rBri);
       Serial.println(key);
     }
       
       break;
       
     case '2': // Green color, if on, should switch off
       if( gState == 0)
      {
       analogWrite(g, gBri);
       Serial.println(gBri);
       Serial.println(key);
       gState = 1;
      }
     else {
       gState=0;
       digitalWrite(g,LOW);
       Serial.println(gBri);
       Serial.println(key);
     }
       
       break;
       
     case '3': // Blue color, if on, should switch off
       if( bState == 0)
      {
       analogWrite(b, bBri);
       Serial.println(bBri);
       Serial.println(key);
       bState = 1;
      }
     else {
       bState=0;
       digitalWrite(b,LOW);
       Serial.println(bBri);
       Serial.println(key);
     }
       
       break;
       
     case '4': // Raise red brightness if possible
       if (rBri < 231) {
         rBri = rBri + am;
       analogWrite(r, rBri); }
       Serial.println(rBri);
       Serial.println(key);
       break;
       
     case '5': // Raise green brightness if possible
       if ( gBri < 231) {
         gBri = gBri + am;
       analogWrite(g, gBri); }
       Serial.println(gBri);
       Serial.println(key);
       break;
       
     case '6': // Raise blue brightness if possible
       if ( bBri <231) {
       bBri = bBri + am;
       analogWrite(b, bBri); }
       Serial.println(bBri);
       Serial.println(key);
       break;
       
     case '7': // Lower red brightness if possible
       if ( rBri >29) {
       rBri = rBri - am;
       analogWrite(r, rBri); }
       Serial.println(rBri);
       Serial.println(key);
       break;
       
     case '8': // Lower green brightness if possible
       if ( gBri >29) {
       gBri = gBri - am;
       analogWrite(g, gBri); }
       Serial.println(gBri);
       Serial.println(key);
       break;
       
     case '9': // Lower blue brightness if possible
       if ( bBri >29) {
       bBri = bBri - am;
       analogWrite(b, bBri); }
       Serial.println(bBri);
       Serial.println(key);
       break;
               
     case 'A': // All on, if on, should switch off
       analogWrite(r, rBri);
       analogWrite(g, gBri);
       analogWrite(b, bBri);
       Serial.println(key);
       break;
       
     case 'B': // Totally random color
       analogWrite(r, random(255));
       analogWrite(g, random(255));
       analogWrite(b, random(255));
       
       Serial.println(key);
       break;
     
     case 'C': // Color circle should come here
     
       Serial.println(key);
       break;
       
     case 'D': // How to implement strobe?
       if (strobe == 0) {
         strobe=1; }
       else {
         strobe=0;}
       Serial.println(key);
       Serial.println(strobe);
       break;
       
     case 'E': // Lower all colors if possible
               // Lower fade speed when possible
       if (rBri >29) {
       rBri = rBri - am; }
       if (gBri >29) {
       gBri = gBri - am; }
       if (bBri >29) {
       bBri = bBri - am; }
       if (rState==1) {
         analogWrite(r, rBri);}
       if (gState==1) {
         analogWrite(g, gBri);}
       if (bState==1) {
         analogWrite(b, bBri);}
       Serial.println(rBri);
       Serial.println(gBri);
       Serial.println(bBri);
       Serial.println(key);
       break;
       
     case 'F': // Raise all colors if possible
               // Raise fade speed when possible
       if (rBri <231) {
       rBri = rBri + am; }
       if (gBri <231) {
       gBri = gBri + am; }
       if (bBri <231) {
       bBri = bBri + am; }
       if (rState==1) {
         analogWrite(r, rBri);}
       if (gState==1) {
         analogWrite(g, gBri);}
       if (bState==1) {
         analogWrite(b, bBri);}
       Serial.println(rBri);
       Serial.println(gBri);
       Serial.println(bBri);
       Serial.println(key);
       break;
       
     case '0': // Reset to default, everything off
               // Remember to add fade speed
       digitalWrite(r, LOW);
       digitalWrite(g, LOW);
       digitalWrite(b, LOW);
       rBri = 255;
       gBri = 255;
       bBri = 255;
       Serial.println(key);
       break;
       
     default:
       Serial.println(key);
   }
 }
 
 if(strobe==1){
   if(rState==1){
     analogWrite(r, rBri);}
   if(gState==1){
     analogWrite(g,gBri);}
   if(bState==1){
     analogWrite(b,bBri);}
   delay(dA);
   digitalWrite(r, LOW);
   digitalWrite(g, LOW);
   digitalWrite(b, LOW);
}
}

Cryod

Just bumping up, made some major edits to first post!

cyclegadget


Here is a rainbow led effect code thread. http://arduino.cc/forum/index.php/topic,95019.30/topicseen.html


In my opinion, if your code that you posted works you need to set it to the side and focus on one problem at a time. It would be easier to help you make LED pattern codes if minimize your code to just what it takes to make a desirable pattern run on the LEDs. Then, each time you get a pattern you like, you can add it to the program you currently have.



Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

Headroom

Judging by your code I guess your are trying to fade all LEDs in the LED strip at once ?

A very efficient method to change all three colors at the same time ( well...mor or less) is to implement a 3D bresenham algorithm. Think of it as drawing a line trough a rasterized 3D space with the three coordinates not being x, y and z but red, green and blue.

With that being your base algorithm, you can simply fade between any given color mix. You define two points and the algorithm draws the line resulting in smooth fading between the two ( color)points.

If that's what you a looking for, I've implemented it in my LED lighting systems trippylighting.com and I'd be glad to share. It's still quite work in progress but works nicely as is already.
http://trippylighting.com

http://ledshield.wordpress.com/

Go Up