Pages: [1]   Go Down
Author Topic: add a fading led while the rest of the script keeps running  (Read 915 times)
0 Members and 1 Guest are viewing this topic.
netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Yeah, we all love daylight...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...  smiley-cry

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:
//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

//variables
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){
    fadefunction();
  }
  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)
    delay(1000);
      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)
    delay(1000);
      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   
  delay(30);                           
}

//rinse and repeat
« Last Edit: September 11, 2011, 06:38:39 am by AWOL » Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://captain-slow.dk/2010/10/31/using-millis/

Have a look at this smiley
Logged

captain-slow.dk | non contagious!

netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Yeah, we all love daylight...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-cry)

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...
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1143
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is another explanation on how and why to avoid using ddelays.
Logged

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Yeah, we all love daylight...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, it works for one thing? smiley-confuse  (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...
Logged

netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Yeah, we all love daylight...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
 
http://www.e-shore.com.my/homepage/all-projects/119-led-blinking-using-arduino-nano.html
 smiley-mr-green
Logged

Pages: [1]   Go Up
Jump to: