Pages: [1]   Go Down
Author Topic: Fading up LED PWM with fade down on button press. Delay problem  (Read 580 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 76
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,
getting annoyed with this now.
I want to add a debounce to the switch, but trying to use the example hasnt worked out. Can someone show me the right code to add where?

The idea of the code is to fade up a transistor that has a load of leds on it. However, I want to be able to turn if off while it is still fading up. Thats why i havnt just used for(i=0;i=256;i++); delay(30); .

So basically. Button press > Fade up ... full brightness stop... Button press > Fade down .. Button press>Fade up from where the fade down got to.

Thanks for any help.

Code:
int LEDpwmPIN = 9;    // LED connected to digital pin 9
int Switch1 = 8;
int powerup = 0;
int Switch1state = 0;
int PWMfullon = 255;
int PWMfulloff = 0;
int PWMstepping = 5;
int PWMcurrent = 0;
int delaytime = 40;

void setup()  {
  Serial.begin(9600);
  pinMode(Switch1,INPUT);
  pinMode(LEDpwmPIN,OUTPUT);
  Serial.println("Arduino Connected");
}

void loop()  {
  switchcheckp);
  PWMcurrent=PWMcontrollerLED();
  analogWrite(LEDpwmPIN,PWMcurrent);
  delay(delaytime); //This is just to stop the up/donw LED power being too fast
}

void switchcheck(){
      if(Switch1state==1){
     if(powerup==0){
      powerup=1;
    }
      else{
        powerup=0;
      }
    }
}

int PWMcontrollerLED(){
    
  if(powerup==1 && PWMcurrent != PWMfullon){
    PWMcurrent = PWMcurrent + PWMstepping;
  }
  else if(powerup==0 && PWMcurrent != PWMfulloff){
    PWMcurrent = PWMcurrent - PWMstepping;
  }
  
  return(PWMcurrent);
}
Logged

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

Code:
      if(Switch1state==1){
Where does Switch1state get set?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 435
Posts: 23611
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Missing a ( here?

switchcheckp);

Should this be 'void' instead of 'int'?
int PWMcontrollerLED(){
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: