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