I’m trying to write an arduino sketch that makes a dc motor turns one full rotation every 60s similar to a clock. The arduino will be connected to the gate of a typical mosfet and the dc motor will be connected to the drain of the mofset such that the arduino controls the speed of the motor. Any suggestions on how I go about this? I’m using an arduino mega 2560 in case that’s relevant.
The best way would be to check the datasheet: https://ww1.microchip.com/downloads/en/devicedoc/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf and set an interrupt on one of the timer.
Example code of this application can be found on this stack here : How to make 1 second delay more accurate?
Another way of doing it if you don't need to do anything between rotation would be to wait by just waiting using delay in a for loop. Be aware that this is less precise than interrupts driven wait. https://www.arduino.cc/reference/en/language/functions/time/delay/
edit : if you want to complete one full rotation every 60s
int i = 0;
while(1) {
i++;
delay(1000);
if( i == 60){
doFullRotation();
i = 0;
}
}
-
1Sorry my bad, I was not clear in the question. I don’t want it to start turning every 60s, I want it to complete one full rotation every 60s. – John Oct 31 '20 at 1:10
-
oh wait, you mean you want it to rotate one turn over 60 sec? 1 turn per 60 sec? – Marianne Primeau Breton Oct 31 '20 at 2:23
-
1In that case, you will have trouble with dc motor, since you can't precisely make them turn over a precise angle without feedback from an encoder. Id suggest getting something like a servo or a stepper motor depending on your needs – Marianne Primeau Breton Oct 31 '20 at 2:26
delay()
or (better) viamillis()
(like in theBlinkWithoutDelay
example). Have you tried these? – chrisl Oct 30 '20 at 21:54