problem in digitalwrite low after certain time

I’m new in programming with Wiring PI,and I want to set time for digital write after 5sec time val be LOW ,but after 5sec val doesn’t change auto :confused: until I press d or a val be LOW and goto first of the loop,what is the problem in function of class ,for automatic goto first of the loop?? :frowning: :frowning:

void digital::loop(int motor_pin_1, int motor_pin_2, char key) {
	unsigned long second;
	printf("press 'd (<--open-->)' or 'a (-->close<--)' key OR 'e' to stop  and  change  mode:\n");

do{
		key = getchar();
		second=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);
			//delayMicroseconds(200);

		}
			else if (key == 'e') {
                                  break;

		}
	
	} while (second<=5000);
}
  do {
    key = getchar();
    second = millis();
  
//other code

  } while (second <= 5000);

millis() will continue to increase in value and not reset back to zero until 49 and a bit days later so will hardly ever be less than 5000

You could do something like this

  unsigned long startTime = millis();
  do {

//other code

  } while (millis() - startTime <= 5000);

I try this but write is continues and value be 1 untill press ‘e’ , change to 0 :frowning: :frowning: :confused: :confused: Could this be another solution?
my main.cpp:

int main() {
	
	int BaudRate = 57600;
	int fd = serialOpen("/dev/ttyAMA0", BaudRate);
	if (fd < 0)
		cout << "serial port is already in use." << endl;

	digital d;
	int motorpin1 = 21;
	int motorpin2 = 20;
	char key;
	
while(true) {
	
		d.setup(motorpin1, motorpin2);
		d.loop(motorpin1, motorpin2, key);
	
}
	return 0;
}

The demo Several Things at a Time illustrates the use of millis() to manage timing.

...R

I can't understand how program Recognize state of the pin, low or high in your post

Which pin ? Where in the program ?

in the link of @Robin2 ,this part :

void updateLed_A_State() {

  if (led_A_State == LOW) {

, I think this post is too far of my question

girl71, I won't try to help. Bob and Robin are more than qualified to do that and are doing a good job. :slight_smile:

I have a question though, just out of interest - is your program for Arduino or for Raspberry Pi?
(I was getting confused, and now I think I know why. :smiley: )

Edit: No need to answer - I just spotted this "I'm new in programming with Wiring PI" in your first post. Somehow I missed it earlier. Obviously it's a Raspberry Pi program. So Wiring Pi is closely related to Arduino, obviously.

@Robin2 - sorry I left you out of this reply - corrected. :smiley:

girl71:
I can't understand how program Recognize state of the pin, low or high in your post

girl71:
in the link of @Robin2 ,this part :

void updateLed_A_State() {

if (led_A_State == LOW) {



, I think this post is too far of my question

The thing called led_A_state is a variable. In my program it is used to set the value of a of the pin. It is not used to test the value of the pin. The code changes the value of the variable to make the LED turn on or off.

...R

I think it is doesn't work for my question beacuse I write for DC motor and both pin in the same time will be write,if I'm wrong please show me explain code for DC motor with your soultion

girl71:
please show me explain code for DC motor with you soultion

Please post YOUR code with your best attempt at a solution.

...R

My problem in getchar() or scanf() function blocks until a carriage return is pressed. and I should use key to
Control motor.

As requested, please post the code that you are having trouble with.

as I said,I think time is ok , my problem in getchar() untill press any key val doesn’t be LOW , I need to program auto goto first of loop after 5 second

void digital::loop(int motor_pin_1, int motor_pin_2, char key) {
	unsigned long second;
	printf("press 'd (<--open-->)' or 'a (-->close<--)' key OR 'e' to stop  and  change  mode:\n");

do{
		key = getchar();
		second=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);
			//delayMicroseconds(200);

		}
			else if (key == 'e') {
                                  break;

		}
	
	} while (second<=5000);
}

@girl71, You have now been asked several times to post your program

Post the complete program if you want help.

...R