I am trying to stop a dc motor after a desired amount of time !! And the motor starts upon settling on a condition !! From that very moment i want my motor to just run for 5 seconds and stop!! Below is the pseudocode:

 #define max 150
 #define min -150
 #define pin1 3
 void setup()
   void loop()
        x= some_accelerometer_values;
         If (x>250)

I tried using millis() but it starts time with execution of program and i can't get to implement it just before the motor starts running!

  • 1. Don't use exit(0) on a microcontroller. There is no OS to take charge after the end of the program. Better use an infinite loop, if you want to end the program and do nothing more – chrisl Mar 31 '19 at 19:08
  • 2. Have you understood the BlinkWithoutDelay example, that comes with the Arduino IDE? I explains the use of millis() rather good. You take a timestamp (value of millis()), when the start condition arises, and then you calculate the difference between the current millis() value and the timestamp and compare it to the 5s (5000ms). – chrisl Mar 31 '19 at 19:11
  • Thumbs up for the exit(0); information !! I didin't knew that, nor it was working with it !! But the millis(); is troublesome !! Its counting time since the program started !! And not since the motor started !! – Hrithik Baishakhiya Mar 31 '19 at 19:20
  • @HrithikBaishakhiya You can use a variable to save the time when the motor started. Then always check the difference between now, and when the motor has started. And if the difference is >= then 5 seconds, it turns off – Zunzulla alagaty Mar 31 '19 at 19:38
  • But the millis(); is troublesome !! Its counting time since the program started !! And not since the motor started !! ..... how do you boil an egg for 3 minutes by using a clock, without using a timer? – jsotola Mar 31 '19 at 21:33

Assuming you turn your motor on with analogWrite(pin1, 255); just write analogWrite(pin1, 0); after the delay(5000);.

And using exit(0); isn't as bad as people will most likely say. Using exit(0) is the same as cli(); + while(1). So it disables interrupts and stucks your code in a while loop till your reset the arduino.

  • But if x remains greater than 250 for a while then it will run until x < 250!! – Hrithik Baishakhiya Mar 31 '19 at 19:29
  • @HrithikBaishakhiya since you used exit(0); you want your code to only run once. And if you write analogWrite(pin1, 0); before the exit(0) your motor will turn off. Then the program will stop and not ask about whether x is greater than 250 or not. – Zunzulla alagaty Mar 31 '19 at 19:37
  • @HrithikBaishakhiya if() is triggered by a condition, and will only be executed once per loop() – Zunzulla alagaty Mar 31 '19 at 19:40
  • I respect your effort !! But its not recommended to use exit(0); as written in some contexts and suggest by chrisl above!! I am little confused here !! – Hrithik Baishakhiya Mar 31 '19 at 19:45
  • @HrithikBaishakhiya as of I know, exit(0); disables interrupts and throws the arduino into a while(1) loop. I just tried it on my arduino-uno. It's pretty much a loop() but with disabling interrupts. – Zunzulla alagaty Mar 31 '19 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.