Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Controlling RGB Light with Arduino using a Button
Index -> General Discussion
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Natsu




PostPosted: Tue Sep 24, 2013 11:33 am   Post subject: Controlling RGB Light with Arduino using a Button

Im trying to modify my code so that when I push the button for the first time, the led freeze's on the color. Any other time after the first press, the LED changes to a random color, and while I am not pressing the button, the transition is still smooth:

my button is on Pin 10

code:
int red = 12;
int green = 14;
int blue = 15;

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;

void setup()
{
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
redNow = random(255);
blueNow = random(255);
greenNow = random(255);
redNew = redNow;
blueNew = blueNow;
greenNew = greenNow;
}

#define fade(x,y) if (x>y) x--; else if (x<y) x++;

void loop()
{
analogWrite(blue, blueNow);
analogWrite(red, redNow);
analogWrite(green, greenNow);
redNew = random(255);
blueNew = random(255);
greenNew = random(255);
// fade to new colors
while ((redNow != redNew) ||
  (blueNow != blueNew) ||
  (greenNow != greenNew))
{
  fade(redNow,redNew)
  fade(blueNow,blueNew)
  fade(greenNow,greenNew)
  analogWrite(blue, blueNow);
  analogWrite(red, redNow);
  analogWrite(green, greenNow);
  delay(20);
}
}


i mtrying to modify the code to check the state of the push-button
Sponsor
Sponsor
Sponsor
sponsor
chrisbrown




PostPosted: Tue Sep 24, 2013 11:54 am   Post subject: Re: Controlling RGB Light with Arduino using a Button

Try this: rather than picking colours at the start and waiting for them to fade, pick new colours only when all three transitions are finished. This will let you pause and resume the loop at any time and you should never see an abrupt change in colour.
code:
void loop() {
        if ((redNow == redNew) &&
          (blueNow == blueNew) &&
          (greenNow == greenNew))
        {
                redNew = random(255);
                blueNew = random(255);
                greenNew = random(255);
        }
        fade(redNow,redNew)
        fade(blueNow,blueNew)
        fade(greenNow,greenNew)
        analogWrite(blue, blueNow);
        analogWrite(red, redNow);
        analogWrite(green, greenNow);
        delay(20);
}
Natsu




PostPosted: Tue Sep 24, 2013 3:48 pm   Post subject: Re: Controlling RGB Light with Arduino using a Button

chrisbrown @ Tue Sep 24, 2013 11:54 am wrote:
Try this: rather than picking colours at the start and waiting for them to fade, pick new colours only when all three transitions are finished. This will let you pause and resume the loop at any time and you should never see an abrupt change in colour.
code:
void loop() {
        if ((redNow == redNew) &&
          (blueNow == blueNew) &&
          (greenNow == greenNew))
        {
                redNew = random(255);
                blueNew = random(255);
                greenNew = random(255);
        }
        fade(redNow,redNew)
        fade(blueNow,blueNew)
        fade(greenNow,greenNew)
        analogWrite(blue, blueNow);
        analogWrite(red, redNow);
        analogWrite(green, greenNow);
        delay(20);
}


Kool thanks, much appreciated I understand how this is a better way to do it now

And about the state's to freeze at first press and then random color at next press while its doing the transition, its not working for me? Any ideas why.

I need to change the code to check the state of the push-button. The first time the button is pressed, freeze the current colour. On the second and subsequent presses, jump to a new (random) colour and freeze on the new colour.
Natsu




PostPosted: Tue Sep 24, 2013 10:06 pm   Post subject: RE:Controlling RGB Light with Arduino using a Button

This is what I got so far but still not able to cotrol the LED using the pushbutton

code:

#define ON HIGH
#define OFF LOW
#define fade(x,y) if (x>y) x--; else if (x<y) x++;

int red = 12;
int green = 14;
int blue = 15;

int buttonPin = 10;
int buttonState = 0;

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;

void setup() {
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  redNow = random(255);
  blueNow = random(255);
  greenNow = random(255);
  redNew = redNow;
  blueNew = blueNow;
  greenNew = greenNow;
}

void loop() {
 
  analogWrite(blue, blueNow);
  analogWrite(red, redNow);
  analogWrite(green, greenNow);

  buttonState = digitalRead(buttonPin);
  int firstTime = 0;
 
  if (buttonState == HIGH and firstTime == 0) {   
    digitalWrite(blue, ON); 
    digitalWrite(red, OFF);
    digitalWrite(green, OFF);
    firstTime = 1; //been pressed once
  }
  else if (buttonState == HIGH and firstTime == 1){
    changeColor();
  }
 
}


void changeColor() {
 
        if ((redNow == redNew) &&
          (blueNow == blueNew) &&
          (greenNow == greenNew))
        {
                redNew = random(255);
                blueNew = random(255);
                greenNew = random(255);
        }
        fade(redNow,redNew)
        fade(blueNow,blueNew)
        fade(greenNow,greenNew)
        analogWrite(blue, blueNow);
        analogWrite(red, redNow);
        analogWrite(green, greenNow);
        delay(20);
}
chrisbrown




PostPosted: Wed Sep 25, 2013 12:13 am   Post subject: Re: Controlling RGB Light with Arduino using a Button

You're almost there.

First thing: declare and initialize firstTime outside loop(). The way it is now, you reset firstTime every time through the loop, which pretty much defeats its purpose.

Next, you want to changeColor() when the button is unpressed, correct? Use this:
code:
else if (buttonState == HIGH and firstTime == 1){
    // do nothing
}
else {
    changeColor();
}
Natsu




PostPosted: Thu Sep 26, 2013 1:20 pm   Post subject: RE:Controlling RGB Light with Arduino using a Button

What it does now, is it only goes through the transition while the button is held down. I pressed multiple times and nothing happens

As soon as its plugged in, it should start going through all the colors smoothly

Once you press the button first time (without holding down) it should freeze on that colour.

Any other press after that should randomly go to any color (again without holding down)

code:
#define ON HIGH
#define OFF LOW
#define fade(x,y) if (x>y) x--; else if (x<y) x++;

int red = 12;
int green = 14;
int blue = 15;

int buttonPin = 7;
int buttonState = 0;
int firstTime = 0;

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;

void setup() {
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  redNow = random(255);
  blueNow = random(255);
  greenNow = random(255);
  redNew = redNow;
  blueNew = blueNow;
  greenNew = greenNow;
}

void loop() {
 
  analogWrite(blue, blueNow);
  analogWrite(red, redNow);
  analogWrite(green, greenNow);

  buttonState = digitalRead(buttonPin);
 
  if (buttonState == HIGH and firstTime == 0) {   
    digitalWrite(blue, ON); 
    digitalWrite(red, OFF);
    digitalWrite(green, OFF);
    firstTime = 1; //been pressed once
  } else if (buttonState == HIGH and firstTime == 1){
    // do nothing
  } else {
    changeColor();
  }
}


void changeColor() {
 
  if ((redNow == redNew) &&
     (blueNow == blueNew) &&
     (greenNow == greenNew))
     {
        redNew = random(255);
        blueNew = random(255);
        greenNew = random(255);
     }
  fade(redNow,redNew)
  fade(blueNow,blueNew)
  fade(greenNow,greenNew)
  analogWrite(blue, blueNow);
  analogWrite(red, redNow);
  analogWrite(green, greenNow);
  delay(20);
}
chrisbrown




PostPosted: Thu Sep 26, 2013 3:42 pm   Post subject: RE:Controlling RGB Light with Arduino using a Button

Quote:
it only goes through the transition while the button is held down
Ok, think carefully about that. changeColor() is getting called when the button is pressed. Looking at your if-statements, what does that imply about the value of buttonState when it is pressed vs unpressed.

Hint: buttons are not always active-high.
Display posts from previous:   
   Index -> General Discussion
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: