Pages: [1]   Go Down
Author Topic: RGB effects/patterns, help needed with switch cases  (Read 786 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
}
« Last Edit: October 17, 2012, 02:40:35 pm by Cryod » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1328
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 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.


 
Logged


Offline Offline
Sr. Member
****
Karma: 7
Posts: 275
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1]   Go Up
Jump to: