Advertisement
baldengineer

reworked motor code

Jun 21st, 2015
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <stdint.h>
  2.  
  3. #define In1   7
  4. #define In2  13
  5. #define pwm1 12
  6. #define pwm2  8
  7.  
  8. void setup() {
  9.   // put your setup code here, to run once:
  10.    
  11.   pinMode (In1,  OUTPUT);
  12.   pinMode (In2,  OUTPUT);
  13.   pinMode (pwm1, OUTPUT);
  14.   pinMode (pwm2, OUTPUT);
  15.  
  16.   digitalWrite (In1,LOW);
  17.   digitalWrite (In2,LOW);
  18. }
  19.  
  20. void motor_dir (bool dir) {
  21.   if (dir) {
  22.     digitalWrite (In1, HIGH);
  23.     digitalWrite (In2,  LOW);
  24.   }
  25.   if (!(dir)) {
  26.     digitalWrite (In1, LOW);
  27.     digitalWrite (In2, HIGH);
  28.   }
  29. }
  30.  
  31. void motor_setup (uint16_t speed_hz, uint16_t time_ms, bool dir) {
  32.   uint16_t speed_ms = 1000/speed_hz;
  33.  
  34.   motor_dir (dir);
  35.   uint32_t startTime = millis ();
  36.   while (millis () - startTime < time_ms){
  37.     if (dir){
  38.       digitalWrite (pwm2, HIGH);
  39.       delay (speed_ms-2);
  40.       digitalWrite (pwm2, LOW);
  41.       delay (speed_ms-2);
  42.     }
  43.     if (!(dir)){
  44.       digitalWrite (pwm1, HIGH);
  45.       delay (speed_ms-2);
  46.       digitalWrite (pwm1, LOW);
  47.       delay (speed_ms-2);
  48.     }
  49.   }
  50. }
  51.  
  52. void loop() {
  53.   // put your main code here, to run repeatedly:
  54.   bool dir;
  55.  
  56.   dir = 1;
  57.   motor_setup (2, 1000, dir);
  58.   dir = 0;
  59.   motor_setup (2, 1000, dir);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement