Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Debounce Problem, please help on: October 12, 2012, 04:18:34 pm
OK, figured it all out. I think it could be more simple though.
heres what i came up with
Quote
const int trigger = 2;
const int modeButton = 4;
const int rled = 9;
const int gled = 10;
const int bled = 11;

boolean lastButton = LOW;
boolean currentButton = LOW;

int colorStep = 0;//Color identifier
int modeStep = 0;//Mode identifier
int dimmingTimer = 2; //Timer for dimming, higher = longer

//Debouce function
boolean debounce(boolean last)
{
 boolean current = digitalRead(modeButton);
  if (last != current)
  {
    delay(5);
    current = digitalRead(modeButton);
  }
  return current;
}

void setup ()
{
  pinMode (trigger, INPUT);
  pinMode (modeButton, INPUT);
  pinMode (rled, OUTPUT);
  pinMode (gled, OUTPUT);
  pinMode (bled, OUTPUT);
}

void loop()
{
  if (colorStep > 3) colorStep = 0;
  if (colorStep == 0) //White
  if (digitalRead (trigger) == HIGH)
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {
    // sets the value (range from 0 to 255):
    analogWrite(rled, fadeValue);   
    analogWrite(gled, fadeValue);
    analogWrite(bled, fadeValue);   
    // wait time between dimming steps   
    delay (dimmingTimer);
    if (fadeValue == 0) colorStep ++;
  } 
  if (colorStep == 1) //Red
  if (digitalRead (trigger) == HIGH)
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {
    // sets the value (range from 0 to 255):
    analogWrite(rled, fadeValue);     
    // wait time between dimming steps   
    delay (dimmingTimer);
    if (fadeValue == 0) colorStep ++;
  }
  if (colorStep == 2) //Green
  if (digitalRead (trigger) == HIGH)
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {
    // sets the value (range from 0 to 255):
    analogWrite(gled, fadeValue);     
    // wait time between dimming steps   
    delay (dimmingTimer);
    if (fadeValue == 0) colorStep ++;
  }
  if (colorStep == 3) //Blue
  if (digitalRead (trigger) == HIGH)
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {
    // sets the value (range from 0 to 255):
    analogWrite(bled, fadeValue);     
    // wait time between dimming steps   
    delay (dimmingTimer);
    if (fadeValue == 0) colorStep = 0;
  }
}
2  Using Arduino / Programming Questions / Re: Debounce Problem, please help on: October 12, 2012, 11:44:19 am
Ok, it doesn't quite work, the leds flicker all kinds of crazy. But i think i see where your going, I'll play around with this code and see what i can accomplish.

Thank you very much for your hard work.
3  Using Arduino / Programming Questions / Re: Debounce Problem, please help on: October 11, 2012, 06:49:09 pm
thanks for your quick reply.
I'm sure monitoring the serial data could help, but i'm not sure where i should put the serial.print() in the code, or what i'm looking for when reading the serial data. I have never done that before.
if you could point me to a specific thing i should be doing i would appreciate that a lot.
4  Using Arduino / Programming Questions / Debounce Problem, please help on: October 11, 2012, 05:35:44 pm
HI,
I am a novice when i comes to arduino and programming, I just picked up mine about two weeks ago and am having some trouble wrapping my head around software debouncing. I've been trying to figure out what I'm doing wrong, it doesn't seem to make any difference no matter what i do with the code.
I have one button attached to pin 2 and an rgb led attached to pin 9 for red, 10 for green, and 11 for blue
the way my program is supposed to work goes like this:
I press the button and it should light up white (all three led's at once) and then fade out.
then if i press it a second time it should light up red then fade out, and the third press goes to green, and the fourth to blue, then back to white.
what happens is most of the times it skips 1 or more of the other colors, sometimes it skips all the way back to the one that just lit.
I'm sure I'm missing something simple here,
my code looks like this:
Quote
int trigger = 2;
int rled = 9;
int gled = 10;
int bled = 11;
int dimmingTimer = 2;
boolean lastButton = LOW;
boolean currentButton = LOW;

void setup ()
{
  pinMode (trigger, INPUT);
  pinMode (rled, OUTPUT);
  pinMode (gled, OUTPUT);
  pinMode (bled, OUTPUT);
}
//Debouce function
boolean debounce(boolean last)
{
  boolean current = digitalRead(trigger);
  if (last != current)
  {
    delay(5);
    current = digitalRead(trigger);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {
    // sets the value (range from 0 to 255):
    analogWrite(rled, fadeValue);  
    analogWrite(gled, fadeValue);
    analogWrite(bled, fadeValue);    
    // wait time between dimming steps    
    delay (dimmingTimer);
  }
    lastButton = currentButton;
    currentButton = debounce(lastButton);
    if (lastButton == LOW && currentButton == HIGH)
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {
    // sets the value (range from 0 to 255):
    analogWrite(rled, fadeValue);      
    // wait time between dimming steps    
    delay (dimmingTimer);
  }
  lastButton = currentButton;
    currentButton = debounce(lastButton);
    if (lastButton == LOW && currentButton == HIGH)
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {
    // sets the value (range from 0 to 255):
    analogWrite(gled, fadeValue);      
    // wait time between dimming steps    
    delay (dimmingTimer);
  }
  lastButton = currentButton;
      currentButton = debounce(lastButton);
    if (lastButton == LOW && currentButton == HIGH)
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {
    // sets the value (range from 0 to 255):
    analogWrite(bled, fadeValue);      
    // wait time between dimming steps    
    delay (dimmingTimer);
  }
  lastButton = currentButton;
}

my button is pulled down to ground wit a 10k resisterand pulls high to 5v when pressed, and I am lighting up a a 12v led strip by controlling 3 mosfetts with pwm signal.

Any help would be much appreciated. Thank you in advance
Pages: [1]