How set certain time for digitalwrite
Posted: Mon Nov 02, 2015 4:48 am
I'm new in programming with wiring pi
and I want to set time for digital write with get time(NULL) after get key then stop writing (after certain time or with e key)and goto first of loop , my program doesn't work ,what is the problem??
I try with millis() but after 5sec val doesn't change auto to LOW until I press d or a val be LOW and goto first of the loop,Could this be another solution?
Code: Select all
void digital::loop(int motor_pin_1, int motor_pin_2, char key) {
time_t now,after;
time_t second=10;
printf("press 'd (<--open-->)' or 'a (-->close<--)' key OR 'e' to stop and change mode:\n");
do {
key = getchar();
now = time(NULL);
after= now+ second;
if (key == 'd') {
printf("<--open-->\n");
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
}
else if (key == 'a') {
printf("-->close<--\n");
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_1, LOW);
//delayMicroseconds(200);
// digitalWrite(motor_pin_1, LOW);
// digitalWrite(motor_pin_2, LOW);
}
} while (key != 'e' || now<after);
}Code: Select all
void digital::loop(int motor_pin_1, int motor_pin_2, char key) {
unsigned long startTime;
printf("press 'd (<--open-->)' or 'a (-->close<--)' key OR 'e' to stop and change mode:\n");
do{
key = getchar();
startTime = millis();
if (key == 'd') {
printf("<--open-->\n");
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
}
else if (key == 'a') {
printf("-->close<--\n");
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_1, LOW);
}
else if (key == 'e') {
break;
}
}while (millis() - startTime <= 5000);