Go Down

Topic: add a fading led while the rest of the script keeps running (Read 2274 times) previous topic - next topic


Sep 03, 2011, 10:48 pm Last Edit: Sep 11, 2011, 01:38 pm by AWOL Reason: 1
hi everyone, Iam a newbe on this but Iam willing to learn all about this problem I'm having...

This is my problem:

Iam trying to run a simple script that turns a few relays and leds on and off with one single button.
(this part I've got running well)
The part where it all goes wrong is when I want to add the "Fade" function from the examples in to it when the relays turn off after the first push.
For some reason when the relays are turned on and off (like it should) the script does fade the leds but the script is not resumed.

I don't find that strange, I have to use a Loop or array function for it. (and in this I cannot succeed.)
I tried While, loop, arrays, even breaking up the fade script to check the button state, and now with 2 books and 3 full days further I've got nothing to show for it...  =(

It must be something simple I guess but I don't seem to get it, how do I integrate the fade function into the on/off push button script?

This is what I got so far (basic version) :
Code: [Select]
//D17 control script
//button held low by 10k pull down, button pressed should turn relais on and off for a sec and let ledpin 9 fade after that.
//button pressed again, fading should stop and relays turn on and off again, then nothing should happen unless button is pressed again.

// Pin in/out D17 Model:
// 2D = Magnetic switch
// 3 = Low blue LB
// 5 = Low yellow LY
// 6 = High blue HB
// 9 = High yellow HY
// 10 = Speaker
// 11 = Relay in/out
// 12 = Relay on/off

boolean ledpin3 = 3;  //led pin
boolean ledpin5 = 5;  //led pin
boolean ledpin6 = 6;  //led pin
boolean ledpin9 = 9;  //led pin
boolean relaypin1 = 11; // relay to pull function in /out
boolean relaypin2 = 12; // relay to pull function on /off
boolean button2 = 2;  //button on pin 2
boolean state = 0;    //state of button pin
boolean newState = 0;  //new button state variable

// int needed for fade function
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup(){

  pinMode(button2, INPUT);  //button input
  pinMode(relaypin1, OUTPUT);  //relay output
  pinMode(relaypin2, OUTPUT);  //relay output
  pinMode(ledpin3, OUTPUT);  //ledpin out
   pinMode(ledpin5, OUTPUT);  //ledpin out
    pinMode(ledpin6, OUTPUT);  //ledpin out
     pinMode(ledpin9, OUTPUT);  //ledpin out

void loop() {
  // while the button is pressed, take calibration readings:
  while (state == 1 && newState == 1){
  state = (digitalRead(button2));    //state is value of button
  delay(100);  //button debounce (not needed if debounce circuit exists)

if(state == 0 && newState == 0){  //if state and lastState are both 0 (button unpressed)

         digitalWrite(relaypin1, LOW);   //turn Relay1 off (in/out)
   newState = state; //lastState is still 0
else if(state == 1 && newState == 0){  //if state is 1 (button press) and lastState is 0

   digitalWrite(relaypin2, HIGH);   //turn Relay2 on (off/on)
     digitalWrite(relaypin1, HIGH);   //turn Relay1 on (in/out)
      digitalWrite(relaypin2, LOW); //turn Relay2 off (off/on)
      digitalWrite(relaypin1, LOW); //turn Relay1 off (in/out)
    newState = state; // lastState is now 1
else if(state == 0 && newState == 1){  //if state is 0(button unpressed) and lastState is 1

   //newState = 1;   //lastState is still 1
else if(state == 1 && newState == 1){  //if state is 1(button pressed) and lastState is 1

       digitalWrite(relaypin2, HIGH);   //turn Relay2 on (off/on)
      digitalWrite(relaypin2, LOW); //turn Relay2 off (off/on)
      newState = 0;   //lastState should now be 0 again and loop repeats

void fadefunction() {

  // set the brightness of pin 9:
    analogWrite(3, brightness);   
  analogWrite(5, brightness);   
  analogWrite(6, brightness);   
  analogWrite(9, brightness);   

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  // wait for 30 milliseconds to see the dimming effect   

//rinse and repeat



Have a look at this :)
captain-slow.dk | non contagious!


Hi Bld,

Thanks for your answer...
I did looked into that already before posting, the problem with that script is that I don't get it how to put my script and the specific fade script into one. (and then get it to turn the fading on and off on command, again newbe at work here =()

I was hoping we could use my existing script and put the fading script into that since I need to use that script many times over and over again...



Why have you defined your variables as boolean at the beginning? I may be wrong but these can only be true or false. I think you need int instead.


Well, it works for one thing? :~  (at-least the basics are working)

The other thing is that I modified the script for my needs, I did not build it. (Iam not that smart) :smiley-roll-blue:
I can live without a fading light of something that is just flashing.
But it would be so much cooler if I could get something to work like that. This sunday was a total waste again, I will try one more time on friday after that I have to move on I guess...


Hi guys, just a update on how things are going:

I found this website with a script (on the bottom of the page, "LED Blinking 4") that is after being modded exactly what I want. (a little warning: if you go to project 4, you have to assemble the two sections together)
After that its very easy, thanks to you all...


Go Up