Help with Arduino Uno code for controlling DC Worm gear motor with two micro switches

Discussion in 'Embedded Systems and Microcontrollers' started by astronaut71, Jul 20, 2015.

  1. astronaut71

    Thread Starter New Member

    Jun 11, 2015
    6
    0
    Hi. Im new in this field. I have Pololu High-Power 18V, 15A motor driver, Arduino uno microcontroler and DC worm gear Motor (12V) and two micro switches. Im building a prototype for tracking a solar panel. I would like to ask for a help regarding some sample code, that after tracking the panel with worm gear Motor it returns the panel in their initial position (horizontal). I have two Input PULLUP switches . So the idea is, motor start tracking and rotate 4 sec (defined with const int rotatiOnMillis = 4000) and is off for 2 sec (defined with const int motorBaseInterval = 2000).

    So when (say) right-hand switch is pressed it runs the code to stop the motor and then to move the motor back to the left-hand HOME (Sleep) position. Here, in the HOME (Sleep) position the motor should stop and stay off for (let) say
    15 sec (defined with const int motorSleepInterval = 15000). The problem of the code is that it stop the motor and bring back when end switch is press but it does not sleep for 15sec. And when its in HOME (Sleep ) position does not waje up and start running after those 15 sec. Any help? Here the code


    Code (C):
    1.  
    2. // ----CONSTANTS (won't change)
    3. #define motor_speed 750 //default motor speed @ 0.3rpm
    4.  
    5. const int motorDirection_Pin = 4; //Motor Direction pin
    6. const int motorSpeed_Pin = 5; //Motor PWN pin
    7. const int buttonEnd_Pin = 8; //End Button pin
    8. const int buttonSleep_Pin = 9; //Sleep Button pin
    9. const int rotatiOnMillis = 4000; // number of millisecs for motor rotation
    10. const int motorBaseInterval = 2000; // number of millisecs between motor rotation
    11. const int motorSleepInterval = 15000; // number of millisecs while motor is sleeping
    12.  
    13. //------- VARIABLES (will change)
    14. byte buttonEnd_State = LOW;
    15. byte buttonSleep_State = LOW;
    16. byte motorState = HIGH;
    17. unsigned long currentMillis; // stores the value of millis() in each iteration of loop()
    18. unsigned long previousMotorMillis; // will store last time the motor was updated
    19. unsigned long motorInterval = motorBaseInterval;
    20. unsigned long motorOffMillis = motorSleepInterval;
    21. volatile byte button_flag = 0;
    22. volatile byte button_flag1 = 0;
    23. volatile byte sleep_flag = 0;
    24. //========
    25.  
    26. void setup() {
    27.  
    28. Serial.begin(9600);
    29. // set the Motor and Buttons pins as output, INPUT_PULLUP:
    30. pinMode(motorDirection_Pin, OUTPUT);
    31. pinMode(motorSpeed_Pin, OUTPUT);
    32. pinMode(buttonSleep_Pin, INPUT_PULLUP);
    33. pinMode(buttonEnd_Pin, INPUT_PULLUP);
    34.  
    35. }
    36.  
    37. //=======
    38. void loop() {
    39.  
    40. currentMillis = millis();
    41. checkButtons();
    42. execute_motor();
    43. end_position();
    44. sleep_position();
    45. }
    46.  
    47. //========
    48. void checkButtons() {
    49. buttonEnd_State = digitalRead(buttonEnd_Pin);
    50. buttonSleep_State = digitalRead(buttonSleep_Pin);
    51. }
    52.  
    53. void execute_motor() {
    54. checkButtons();
    55. // nothing happens unless the interval has expired
    56. if (currentMillis - previousMotorMillis >= motorInterval) {
    57. //time is up, its time for another motor rotation
    58. previousMotorMillis += motorInterval;
    59. motorState = ! motorState;
    60. if (motorState == HIGH) {
    61. digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor rotation
    62. analogWrite(motorSpeed_Pin, motor_speed);
    63. motorInterval = rotatiOnMillis;
    64. Serial.println("motor rotation");
    65. }
    66. else {
    67. //nothing happens unless the interval has expired, still motor is not rotating
    68. motorInterval = motorBaseInterval;
    69. analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
    70. Serial.println("motor stop, speed is 0");
    71. }
    72. digitalWrite(motorDirection_Pin, motorState);
    73.  
    74. }
    75. }
    76.  
    77. void end_position() {
    78.  
    79. checkButtons();
    80. /* Check if End Button is pressed, if yes, stop the motor*/
    81. if (buttonEnd_State == HIGH) {
    82.  
    83. analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
    84. Serial.println("end position");
    85. /* Check if Sleep Button is pressed, run the motor to opposite direction till Sleep Button is pressed */
    86. while (buttonSleep_State == LOW) {
    87. digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor
    88. analogWrite(motorSpeed_Pin, motor_speed);
    89. button_flag=1;
    90. checkButtons();
    91. Serial.println("end position, going home");
    92. }
    93. }
    94. }
    95. void sleep_position(){
    96. /* Check if Sleep Button is pressed, if its pressed stop the motor and set the sleep interval */
    97. if (buttonSleep_State == HIGH && button_flag==1) {
    98. analogWrite(motorSpeed_Pin, 0);
    99. //button_flag1=1;
    100. Serial.println("stop, you are in sleep position now");
    101. motorInterval = motorSleepInterval;
    102. Serial.println("sleepinngg bzzzz");
    103. }
    104. }
    105.  
     
  2. theonewho

    New Member

    Jul 9, 2015
    17
    2
    Please format your code and use the code tag to display code in your posts.

    Code (C):
    1. // ----CONSTANTS (won't change)
    2. #define motor_speed 750 //default motor speed @ 0.3rpm
    3.  
    4. const int motorDirection_Pin = 4; //Motor Direction pin
    5. const int motorSpeed_Pin = 5; //Motor PWN pin
    6. const int buttonEnd_Pin = 8; //End Button pin
    7. const int buttonSleep_Pin = 9; //Sleep Button pin
    8. const int rotatiOnMillis = 4000; // number of millisecs for motor rotation
    9. const int motorBaseInterval = 2000; // number of millisecs between motor rotation
    10. const int motorSleepInterval = 15000; // number of millisecs while motor is sleeping
    11.  
    12. //------- VARIABLES (will change)
    13. byte buttonEnd_State = LOW;
    14. byte buttonSleep_State = LOW;
    15. byte motorState = HIGH;
    16. unsigned long currentMillis; // stores the value of millis() in each iteration of loop()
    17. unsigned long previousMotorMillis; // will store last time the motor was updated
    18. unsigned long motorInterval = motorBaseInterval;
    19. unsigned long motorOffMillis = motorSleepInterval;
    20. volatile byte button_flag = 0;
    21. volatile byte button_flag1 = 0;
    22. volatile byte sleep_flag = 0;
    23. //========
    24.  
    25. void setup() {
    26.  
    27.     Serial.begin(9600);
    28. // set the Motor and Buttons pins as output, INPUT_PULLUP:
    29.     pinMode(motorDirection_Pin, OUTPUT);
    30.     pinMode(motorSpeed_Pin, OUTPUT);
    31.     pinMode(buttonSleep_Pin, INPUT_PULLUP);
    32.     pinMode(buttonEnd_Pin, INPUT_PULLUP);
    33.  
    34. }
    35.  
    36. //=======
    37. void loop() {
    38.  
    39.     currentMillis = millis();
    40.     checkButtons();
    41.     execute_motor();
    42.     end_position();
    43.     sleep_position();
    44. }
    45.  
    46. //========
    47. void checkButtons() {
    48.     buttonEnd_State = digitalRead(buttonEnd_Pin);
    49.     buttonSleep_State = digitalRead(buttonSleep_Pin);
    50. }
    51.  
    52. void execute_motor() {
    53.     checkButtons();
    54. // nothing happens unless the interval has expired
    55.     if (currentMillis - previousMotorMillis >= motorInterval) {
    56. //time is up, its time for another motor rotation
    57.         previousMotorMillis += motorInterval;
    58.         motorState = ! motorState;
    59.         if (motorState == HIGH) {
    60.             digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor rotation
    61.             analogWrite(motorSpeed_Pin, motor_speed);
    62.             motorInterval = rotatiOnMillis;
    63.             Serial.println("motor rotation");
    64.         }
    65.         else {
    66. //nothing happens unless the interval has expired, still motor is not rotating
    67.             motorInterval = motorBaseInterval;
    68.             analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
    69.             Serial.println("motor stop, speed is 0");
    70.         }
    71.         digitalWrite(motorDirection_Pin, motorState);
    72.  
    73.     }
    74. }
    75.  
    76. void end_position() {
    77.  
    78.     checkButtons();
    79.     /* Check if End Button is pressed, if yes, stop the motor*/
    80.     if (buttonEnd_State == HIGH) {
    81.  
    82.         analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
    83.         Serial.println("end position");
    84.         /* Check if Sleep Button is pressed, run the motor to opposite direction till Sleep Button is pressed */
    85.         while (buttonSleep_State == LOW) {
    86.             digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor
    87.             analogWrite(motorSpeed_Pin, motor_speed);
    88.             button_flag = 1;
    89.             checkButtons();
    90.             Serial.println("end position, going home");
    91.         }
    92.     }
    93. }
    94. void sleep_position() {
    95.     /* Check if Sleep Button is pressed, if its pressed stop the motor and set the sleep interval */
    96.     if (buttonSleep_State == HIGH && button_flag == 1) {
    97.         analogWrite(motorSpeed_Pin, 0);
    98. //button_flag1=1;
    99.         Serial.println("stop, you are in sleep position now");
    100.         motorInterval = motorSleepInterval;
    101.         Serial.println("sleepinngg bzzzz");
    102.     }
    103. }
     
  3. astronaut71

    Thread Starter New Member

    Jun 11, 2015
    6
    0
    There is no option for C code in code insert tag
     
  4. ScottWang

    Moderator

    Aug 23, 2012
    4,855
    767
    Please attach your schematic, and then the members can check the circuit when they trace the program.
     
  5. astronaut71

    Thread Starter New Member

    Jun 11, 2015
    6
    0
    yes here the attachment. F1 is the photo of the Arduino with motor driver and the two switches. F2 is the schematics
    f1.gif f2.gif
     
Loading...