Go Down

Topic: Fading up LED PWM with fade down on button press. Delay problem (Read 680 times) previous topic - next topic

mellink

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: [Select]

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);
}

PaulS

Code: [Select]
      if(Switch1state==1){
Where does Switch1state get set?

CrossRoads

Missing a ( here?

switchcheckp);

Should this be 'void' instead of 'int'?
int PWMcontrollerLED(){
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up