Hi guys, Im not a programmer, and it is my first task with Arduino, so...
The task is to create 2 relays with delay. I mean not delay(), I mean real delay =)
First "relay": Button pressed and hold. Timer count 5 secs. Led is on. When button released - led is of immediately. But in any time if button pressed again when timer counting 5secs - it should be stop and reset, led should still be ON.
Second "relay": When the button (different one of cause) pressed - led is ON, but when the button released timer starts for 10 secs, after that led is OFF. And the same for pressed button when time is counting - it should stop and reset timer.
I have created such program, but with delay() function. But I have no idea how to use mills() correctly.
Please help me with this.
And sorry for my English =( I'm not an native speaker.
Thanks in advance!
My code (comments in Russian, but the code is clear for understanding)
//???? ????????? ? ????????? ? ?????????? ? ?????????
//?????? 0.7
const int buttonPin1 = 8;//??????? "?????????"
const int buttonPin2 = 9;//??????? "????????"
// ??? ?????? ??? ???????
const int ledPin1 = 13; //???? 1 - ??????????
const int ledPin2 = 12; //???? 2 - ??????????
int offtimer =500; //?????? ?????????? = 500 = 5 ??????.
int ontimer = 0; // ?????? ?????????
// ?????????? ????????? ??????
int buttonState1 = 0; //?????????
int buttonState2 = 0; //?????????
void setup() {
pinMode(ledPin1, OUTPUT); //???? ??? ?????
pinMode(ledPin2, OUTPUT); //???? ??? ?????
pinMode(buttonPin1, INPUT);//???? ??? ????
pinMode(buttonPin2, INPUT); //???? ??? ????
}
void loop()
{
// ?????? ????????? ?????
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == HIGH) //???? ???? ??????????
{
// ?????? ?????? ?? ???? 1
digitalWrite(ledPin1, HIGH);
offtimer = 500; //???????? ?????? - ????????????? ???
} //if1
else { //???? ??????? ???????
if (offtimer == 0) //???? ?????? ??? ?? ???????? (??????)
{
digitalWrite(ledPin1, LOW); //????????? ???? 1
}//if2
else { // ???? ?? ?????? ??????
offtimer = offtimer -1; //????????? ?? 1 (1/10 ???????)
delay (10); //??????? ????? ??????? ??????
}//else2
//
}//else1
if (buttonState2 == HIGH) //???? ???? ??????? ?????????
{
if (ontimer == 300) //???? ??? ?????? 3 ???????
{
digitalWrite(ledPin2, HIGH); //???????? ???? 2
}
else //???? ?? ??????
{
// ontimer = 300;
delay (10);
ontimer = ontimer +1; //?? ????????? 1 ? ???????
}//else 3
//if 4
}//if 3
else //???? ??? ???????
{
digitalWrite(ledPin2, LOW); //????????
ontimer = 0;//? ???????? ??????.
}
}//loop