Fading LED from 50% to 100% on press

Hiya, I've got this project that's due very soon and I'm out of alternatives so I'm just asking for help now.

I've got a string of LEDs that need to run constantly at 50% and when a button is pressed and held down it will fade up to 100% in 1000ms and hold at 100% as long as the button is pressed, then fade back to 50% when the button is released again in 1000ms.

I tried modifying the code from this thread arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1265105743/2 but i just botched it :(

any help would be appreciated or point to sample code I can modify.

Thanks!

probably you should post your best effort. how is your string of leds and your button connected?

well i used this. i really only need 1 LED fade but i didn't bother cutting out the second one. i just don't quite understand where the "default LED brightness" is in the code. wherever it is it needs to be value 150 or so. I've got the bank of LEDs running through a transistor, that works fine if i load "fading" to the arduino. the other part of the mystery to me is where can i tell the code to HOLD the led level once the button is pressed.

any tips will be appreciated!

#define LED1 11
#define LED2 10
#define BUTTON 13

int val = 0;
int oldVal = 0;
int state = 0;

void  setup(){

    pinMode(LED1,OUTPUT);
    pinMode(LED2,OUTPUT);
  
    pinMode(BUTTON,INPUT);
    Serial.begin(9600);
}

void loop(){
  val = digitalRead(BUTTON);

  //Serial.println(val);

  if((val==HIGH)&&(oldVal==LOW)){
    state=1-state;
    delay(10);
  }

  oldVal=val;

  if(state==1){

     digitalWrite(LED1,HIGH);
     digitalWrite(LED1,HIGH);
    
    delay(3000);
    fading();


    state=0;

  }
  else{
    
      digitalWrite(LED1,LOW);
      digitalWrite(LED1,LOW);
    }
}

void fading(){
  
  int max1=0;

  
   for(int i = 255; i >= 0; i--){  //fade out 1st LED
      analogWrite(LED1,i);
      delay(10);
      max1=i;
      Serial.println(max1);
   }

//I know I can't get to max1 outside the loop, thats why its not working
  
   if(max1==128){  
        for(int i = 255; i >= 0; i--){  //fade out 2nd LED
          analogWrite(LED2, i);
          delay(10);
        }
      }
}

wait, hehe i think i found the default brightness :D

wait… nevermind… I’m staring at
int val = 0;
int oldVal = 0;
int state = 0;

i tried changing those to the value i wanted and it doesn’t do it.

this is definitely above my head :\

Disclaimer: I have not tried this and it looks a bit clunky but maybe it will give you the idea. I think it will probably give odd results for quick button pushes.

#define LED1 11
#define BUTTON 13

int val = 0;
int oldVal = 0;

void  setup(){
    pinMode(LED1,OUTPUT);
    pinMode(BUTTON,INPUT);
    analogWrite(LED1,128);
}

void loop(){
  val = digitalRead(BUTTON);

  if((val==HIGH)&&(oldVal==LOW)){
    fadeup();
  }
  else if((val==LOW)&&(oldVal==HIGH)){
      fadeback();
  }
  oldVal=val;
}

void fadeup(){ //go from 128 to 255 in 1000 ms
   for(int i = 128; i <= 255; i++){  //fade in 1st LED
      analogWrite(LED1,i);
      delay(8); //8*(255-128)=1000-ish
   }
}
void fadeback(){ //go from 255 to 128 in 1000 ms
   for(int i = 255; i >= 0; i--){  //fade out 1st LED
      analogWrite(LED1,i);
      delay(8);
   }
}

interesting, MUCH closer than i got lol

when it first loads its at 150, button press fades to 255 then it fades to 0.

i'm gonna poke at it and see what happens.

Thanks!

if anything its a lot simpler to look at and isn't making my eyes cross :D

oops the fade to 0. right near the beginning of fadeback you can change that to 128,

The code in setup uses analogWrite to turn the led on half power. It will stay that way til something changes it.

every time you go into loop it checks button. If it's gone from low to high, it calls fadeup to analogWrite the led up from halfway to full - 255. If it's gone from high to low it does the reverse.

woo!
pretty close! i needed closer to 50 it seems, came out perfect!

thanks a bunch! and i have a little better understanding as well.

#define LED1 11
#define BUTTON 13

int val = 0;
int oldVal = 0;

void  setup(){
    pinMode(LED1,OUTPUT);
    pinMode(BUTTON,INPUT);
    analogWrite(LED1,50);
}

void loop(){
  val = digitalRead(BUTTON);

  if((val==HIGH)&&(oldVal==LOW)){
    fadeup();
  }
  else if((val==LOW)&&(oldVal==HIGH)){
      fadeback();
  }
  oldVal=val;
}

void fadeup(){ //go from 128 to 255 in 1000 ms
   for(int i = 50; i <= 255; i++){  //fade in 1st LED
      analogWrite(LED1,i);
      delay(8); //8*(255-128)=1000-ish
   }
}
void fadeback(){ //go from 255 to 128 in 1000 ms
   for(int i = 255; i >= 50; i--){  //fade out 1st LED
      analogWrite(LED1,i);
      delay(8);
   }
}

Ah, makes sense. thanks a bunch! I'm dangerous now.. I'll post the project here when its done.

okay, so after pulling my hair out and having 2 professional programmers look at the code i had one of them clean it up as much as possible. but the problem still existed that when the button was pressed the LEDs faded up, then faded down even though the button was held down. after both programmers picked it apart we finally figured out it wasn’t software, it was hardware.
pin 13 of the arduino board is hard wired to an LED, it was causing the problem. moved the digital pin to pin8 and BOOM fixed :smiley:

bottom line… RTFM… lesson learned though.

here be the final code for whoever is interested.

#define LED1 11
#define BUTTON 8

int val = 0;
int oldVal = 0;

void  setup(){
    pinMode(LED1,OUTPUT);
    pinMode(BUTTON,INPUT);
    analogWrite(LED1,50);
}

void loop(){
  val = digitalRead(BUTTON);

  if(val==HIGH&&oldVal==LOW){
    fadeup();
    oldVal = val;
  }
  if(val==LOW&&oldVal==HIGH){
    fadeback();
    oldVal = val;
  }

}

void fadeup(){ //go from 128 to 255 in 1000 ms
   for(int i = 50; i <= 255; i++){  //fade in 1st LED
      analogWrite(LED1,i);
      delay(8); //8*(255-128)=1000-ish
   }

}
void fadeback(){ //go from 255 to 128 in 1000 ms
   for(int i = 255; i >= 50; i--){  //fade out 1st LED
      analogWrite(LED1,i);
      delay(8);
   }
}