Pages: [1]   Go Down
Author Topic: RGB with button and fading  (Read 442 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 417
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Adapted some code from lady ada and modified it slighty, also adapted code from a forum member notrabdrahcir (his fading code works quite well!)

Anyways I just combined the 2, modified a little bit:
Code:


int redVal   = 1; // Variables to store the values to send to the pins
int greenVal = 1;   // Initial values are all off
int blueVal  = 1;

int i = 0;     // Loop counter    
int wait = 15; // 50ms (.05 second) delay; shorten for faster fades


int switchPin = 2; // pushbutton on digital pin 2
int redPin = 5;  // RGB cathode leg to digital pin 5
int greenPin = 6; // RGB cathode leg to digital pin 6
int bluePin = 3; // RGB cathode leg to digital pin 3



int val;
int val2;
int buttonState;

int lightMode = 0;
void setup()
{
  
  
  pinMode(switchPin, INPUT);

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  Serial.begin(9600);
  buttonState = digitalRead(switchPin);
}

void loop(){
  val = digitalRead(switchPin);
  delay(10);
  val2 = digitalRead(switchPin);
  if (val == val2) {
    if (val != buttonState) {
      if (val == LOW) {
        if (lightMode == 0) {
          lightMode = 1;
        }
        else {
          if (lightMode == 1) {
            lightMode = 2;
          }
          else {
            if (lightMode == 2) {
              lightMode = 3;
            }
            else {
              if (lightMode == 3) {
                lightMode = 4;
              }
              else {
                lightMode = 0;
              }
            }
          }

        }

      }
    }
    buttonState = val;
  }

  //
  if (lightMode == 0) {
    analogWrite(redPin, 0);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 0);
  }

  if (lightMode == 1) { // RED
    analogWrite(redPin, 255);
    analogWrite(bluePin, 255);
    analogWrite(greenPin, 0);
  }

  if (lightMode == 2) { // BLUE
    analogWrite(redPin, 255);
    analogWrite(bluePin, 0);
    analogWrite(greenPin, 255);


  }
  if (lightMode == 3)  { // GREEN
    analogWrite(redPin, 0);
    analogWrite(greenPin, 255);
    analogWrite(bluePin, 255);
  }
  

    if (lightMode == 4) {
       i += 1;      // Increment counter
  {
  redVal =((i<255)*i)+((i>=255)*255)+((i>511)*(512-i))+((i>766)*(i-766))+((i>=1276)*(i-1276))+((i>1530)*(1530-i))+((i>1786)*(1786-i));
  greenVal =(i<256)*(1)+(i>255)*(i-255)+(i>510)*(510-i)+(i>1020)*(1020-i)+(i>1274)*(i-1274)+(i>1530)*(i-1531)+(i>1785)*(3571-(2*i));
  blueVal =(i<764)*(1)+(i>765)*(i-765)+(i>1020)*(1020-i)+(i>1786)*(1786-i);
  }

  if (i > 2040) //
  {
    i = 1;
  }  

  // we do "255-redVal" instead of just "redVal" because the
  // LEDs are hooked up to +5V instead of Gnd
  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin,  blueVal);  

  delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
}


 
Logged

Pages: [1]   Go Up
Jump to: