Pages: [1]   Go Down
Author Topic: Keypad code RGB  (Read 1221 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So i created this code for my RGB project and it works, sort of (the fading between colors is´nt polished, maybe you guys can help with that?), but i wanna add 2 features, but i dont know how.
I want:
  • When I press 9 i want it to go in a mode where I change the hue with a potentiometer..
  • Another potentiometer to control the brightness.

Can you guys help with this?
I´m pretty new at coding, and i only created this code by looking at some other´s created, and learned it from there..
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My code:

Code:
//----------------------Info------------------------//
// Coded by The Sinding                             //
// 19-01-2011                                       //
// Project: RGB light                               //
//--------------------------------------------------//






//----------------Included libray-------------------//
#include <Keypad.h>

//----------------Rows and Colloms------------------//
const byte myRows = 4;  
const byte myCols = 3;

//----------------Keypad setup----------------------//
char keys[myRows][myCols] =
{
  {'*','0','*'},
  {'7','8','9'},
  {'4','5','6'},
  {'1','2','3'},
};

//--------------------Pin setup---------------------//
byte rowPins[myRows] = {A5, A4, A3, A2}; //array to map keypad to MCU pins
byte colPins[myCols] = {12, 8, 7}; //array to map keypad to MCU pins

//------initialize an instance of class NewKeypad---//
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, myRows, myCols );

//------------------Define Led Pins-----------------//
#define RedPin 11
#define BluePin 10
#define GreenPin 9
#define redPin 6
#define greenPin 5
#define bluePin 3

//---------------------int--------------------------//
int BlackState;

//--------------------------------------------------//


void setup()
{
//----Set the arduino to send data over band 9600---//
  Serial.begin(9600);
  
//------Set the green, red and blue pin to output---//
  pinMode(GreenPin, OUTPUT);
  pinMode(BluePin,  OUTPUT);
  pinMode(RedPin,   OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin,  OUTPUT);
  pinMode(redPin,   OUTPUT);
}
  
void loop()
{
  {
//--------------This is the basic state-------------//
   while (BlackState);
  {
//-When the key * is press, it resets to blackstate-//
    
    char key = kpd.getKey();
    if (key=='*')
    {
//---------This is what it do when you press *------//

//---------It prints the line to the serial---------//
      Serial.println("The leds is off");
      
//----------Sets the all the pins to LOW------------//
      digitalWrite(GreenPin,  LOW);
      digitalWrite(RedPin,    LOW);
      digitalWrite(BluePin,   LOW);
      digitalWrite(redPin,    LOW);
      digitalWrite(greenPin,  LOW);
      digitalWrite(bluePin,   LOW);
      delay(1);

    }
//-------This what happens when you press 1---------//

    if (key=='1')
     {
//---------It prints the line to the serial---------//
      Serial.println("The color is now red");

//-----------It sets the redpin to HIGH-------------//
      digitalWrite(GreenPin,  LOW);
      digitalWrite(RedPin,   HIGH);
      digitalWrite(BluePin,   LOW);
      digitalWrite(redPin,   HIGH);
      digitalWrite(greenPin,  LOW);
      digitalWrite(bluePin,   LOW);
      delay(1);
    }
    
//-------This what happens when you press 2---------//
     if (key=='2')
     {

//---------It prints the line to the serial---------//
      Serial.println("The color is now green");
      
//---------It sets the Greenpin to HIGH-------------//
      digitalWrite(GreenPin, HIGH);
      digitalWrite(RedPin,    LOW);
      digitalWrite(BluePin,   LOW);
      digitalWrite(redPin,    LOW);
      digitalWrite(greenPin, HIGH);
      digitalWrite(bluePin,   LOW);
      delay(1);
     }  

//-------This what happens when you press 3---------//
     if (key=='3')
     {
      
//---------It prints the line to the serial---------//
       Serial.println("The color is now blue");
      
//---------It sets the Bluepin to HIGH--------------//
      digitalWrite(GreenPin,  LOW);
      digitalWrite(RedPin,    LOW);
      digitalWrite(BluePin,  HIGH);
      digitalWrite(redPin,    LOW);
      digitalWrite(greenPin,  LOW);
      digitalWrite(bluePin,  HIGH);
      delay(1);
      }

//-------This what happens when you press 4---------//
     if (key=='4')
     {
      
//---------It prints the line to the serial---------//
       Serial.println("The color is now cyan");
      
//------It sets the blue and green pin to HIGH------//
      digitalWrite(GreenPin, HIGH);
      digitalWrite(RedPin,    LOW);
      digitalWrite(BluePin,  HIGH);
      digitalWrite(redPin,    LOW);
      digitalWrite(greenPin, HIGH);
      digitalWrite(bluePin,  HIGH);
      delay(1);
     }

//-------This what happens when you press 5---------//
     if (key=='5')
     {
      
//---------It prints the line to the serial---------//
       Serial.println("The color is now purple");
  
//------It sets the blue and red pin to HIGH--------//
      digitalWrite(GreenPin,  LOW);
      digitalWrite(RedPin,   HIGH);
      digitalWrite(BluePin,  HIGH);
      digitalWrite(redPin,   HIGH);
      digitalWrite(greenPin,  LOW);
      digitalWrite(bluePin,  HIGH);
      delay(1);
      }
      
//-------This what happens when you press 6---------//
     if (key=='6')
     {
      
//---------It prints the line to the serial---------//
       Serial.println("The color is now yellow");
      
//-----It sets the green and red pin to HIGH--------//
      digitalWrite(GreenPin, HIGH);
      digitalWrite(RedPin,   HIGH);
      digitalWrite(BluePin,   LOW);
      digitalWrite(redPin,   HIGH);
      digitalWrite(greenPin, HIGH);
      digitalWrite(bluePin,   LOW);
      delay(1);
      }
      
//-------This what happens when you press 7---------//
     if (key=='7')
     {
    
//---------It prints the line to the serial---------//
       Serial.println("The color is now white");
      
//--It sets the green, blue and red pin to HIGH-----//
      digitalWrite(GreenPin, HIGH);
      digitalWrite(RedPin,   HIGH);
      digitalWrite(BluePin,  HIGH);
      digitalWrite(redPin,   HIGH);
      digitalWrite(greenPin, HIGH);
      digitalWrite(bluePin,  HIGH);
      delay(1);
      }
      
//-------This what happens when you press 7---------//
     if (key=='8')
     {
       {
        
//---------It prints the line to the serial---------//
         Serial.println("It fades between colors");
        
//---------Now it sets all the pins to low----------//
      digitalWrite(GreenPin,  LOW);
      digitalWrite(RedPin,    LOW);
      digitalWrite(BluePin,   LOW);
      digitalWrite(redPin,    LOW);
      digitalWrite(greenPin,  LOW);
      digitalWrite(bluePin,   LOW);
      delay(1);

//-------Then it fades between differnt colors------//
        for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=10)
          {
          analogWrite(RedPin, fadeValue);
          delay(45);                            
          }
        for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=10)
          {
          analogWrite(RedPin, fadeValue);  
          delay(45);                            
          }
        for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=10)
           {
          analogWrite(BluePin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 255 ; fadeValue >=0; fadeValue -=10)
          {
           analogWrite(BluePin, fadeValue);
          delay(45);
          }
         for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=10)
          {
          analogWrite(GreenPin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 255 ; fadeValue >=0; fadeValue -=10)
          {
          analogWrite(GreenPin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=10)
          {
          analogWrite(BluePin, fadeValue);
          analogWrite(GreenPin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 255 ; fadeValue >=0; fadeValue -=10)
          {
          analogWrite(BluePin, fadeValue);
          analogWrite(GreenPin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=10)
          {
          analogWrite(BluePin, fadeValue);
          analogWrite(RedPin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 255 ; fadeValue >=0; fadeValue -=10)
          {
          analogWrite(BluePin, fadeValue);
          analogWrite(RedPin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=10)
          {
          analogWrite(RedPin, fadeValue);
          analogWrite(GreenPin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 255 ; fadeValue >=0; fadeValue -=10)
          {
          analogWrite(RedPin, fadeValue);
          analogWrite(GreenPin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 0 ; fadeValue <=255; fadeValue +=10)
          {
          analogWrite(RedPin, fadeValue);
          analogWrite(GreenPin, fadeValue);
          analogWrite(BluePin, fadeValue);
          delay(45);
          }
        for(int fadeValue = 255 ; fadeValue >=0; fadeValue -=10)
          {
          analogWrite(RedPin, fadeValue);
          analogWrite(GreenPin, fadeValue);
          analogWrite(BluePin, fadeValue);
          delay(45);
          }
        }      
      }
    }
  }
}
//-------------Now the script ends-----------------//

Did someone created something similar or something that I can look at?
« Last Edit: January 19, 2011, 06:00:23 pm by Sinding » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50366
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()
{
  [glow]{[/glow]
//--------------This is the basic state-------------//
   while (BlackState);
  [glow]{[/glow]
//-When the key * is press, it resets to blackstate-//
    
    char key = kpd.getKey();
    if (key=='*')
    {
//---------This is what it do when you press *------//

//---------It prints the line to the serial---------//
      Serial.println("The leds is off");
Open and close curly braces should be used to define blocks of code, following statements that can have blocks, such as if, while, and for. The highlighted braces are not needed.

The ; following the while statement forms the body of that statement. If BlackState does ever happen to be true, the only way to exit that block is to reset the Arduino. Infinite loops that require reset to exit are hardly ever a good idea.

Comments should be meaningful, and explain what is going to happen, when that is not completely obvious. When the comment states what just happened, and anyone can clearly see what happened, and why, the comment is useless.

Code:
   if (key=='*')
    {
    }
    if (key=='1')
     {
     }
Consistent indenting is a good thing. Is there any value for key that can cause both of these blocks to execute? Is there any way for key to change in the block? If the answer to both questions is no, then you should be using an if/else if/else construct instead of many if statements.

Quote
When I press 9 i want it to go in a mode
The first thing you need to do is learn about functions. Create functions that have meaningful names, like lightsOut(), redOn(), greenOn(), etc. Call those functions when the appropriate key is pressed.

Then, create a function to be called when the 9 key is pressed. In that function, put the code to read the two potentiometers. The output from the analogRead functions can be used to change the PWM value used to control the voltage applied to PWM pins. If the LEDs are connected to PWM pins, you will be able to change the brightness of each color independently.
Logged

Pages: [1]   Go Up
Jump to: