Thank you very much for your answer! Here is my CODE. But is not good because, when I push the button and keep push, is OK. But I need only ON/OFF. When I push the button (TL2) (not keep pressed) only push, the program go in to Start mode (R+G led light and relay1) for 3 sec. And again when I push the same button, the program is going to OFF mode (Red LED and Relay1 and Relay2 + Green LED is OFF).
const int button = 0; //button on my shield TL2
const int relay1 = 8; //relay1 this relay on for 3 sec when LED_R + LED_G is light (in shield is YELLOW LED)
const int relay2 = 7; //relay2 this relay is on when the LED_G is light (in shield is BLUE LED)
const int led_R = 5; //this LED light when is START mode or OFF mode
const int led_G = 6; //this LED ligh when is START mode or ON mode
//record the current button state
enum { Off = LOW, On = HIGH }; // relay on when HIGH
int buttonState;
#define RelayPeriod 3000 // how long to relay1 is ON
unsigned long msecRelay;
int relayState = Off;
void setup () {
pinMode (button, INPUT_PULLUP);
pinMode (relay1, OUTPUT);
pinMode (relay1, OUTPUT);
pinMode (led_R, OUTPUT);
pinMode (led_G, OUTPUT);
digitalWrite (relay1, Off);
digitalWrite (relay2, Off);
digitalWrite (led_R, On);
digitalWrite (led_G, Off);
buttonState = digitalRead (button);
}
void loop () {
unsigned long msec = millis ();
// check for button press
byte but = digitalRead (button);
if (buttonState != but) {
buttonState = but;
delay (25); // debounce
if (LOW == but) {
if (On == digitalRead (relay1)) {
digitalWrite (relay1, Off);
digitalWrite (relay2, Off);
relayState = 0;
digitalWrite (led_G, Off);
digitalWrite (led_R, On);
}
else {
digitalWrite (relay1, On);
digitalWrite (relay2, Off);
digitalWrite (led_R, On);
digitalWrite (led_G, On);
relayState = 1;
msecRelay = msec;
}
}
}
if (relayState && (msec - msecRelay) >= RelayPeriod) {
// turn off
digitalWrite (relay1, Off);
digitalWrite (relay2, On);
digitalWrite (led_G, On);
digitalWrite (led_R, Off);
relayState = 0;
}
}
PS: i found this code and I editting for me, but is not good. Origina this CODE : Switch a fan on for 5 seconds and then turn off - #4 by gcjr