Maker Pro
Maker Pro

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

Bojan Andonovski

Jun 11, 2015
4
Joined
Jun 11, 2015
Messages
4
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


// ----CONSTANTS (won't change)
#define motor_speed 750 //default motor speed @ 0.3rpm

const int motorDirection_Pin = 4; //Motor Direction pin
const int motorSpeed_Pin = 5; //Motor PWN pin
const int buttonEnd_Pin = 8; //End Button pin
const int buttonSleep_Pin = 9; //Sleep Button pin
const int rotatiOnMillis = 4000; // number of millisecs for motor rotation
const int motorBaseInterval = 2000; // number of millisecs between motor rotation
const int motorSleepInterval = 15000; // number of millisecs while motor is sleeping

//------- VARIABLES (will change)
byte buttonEnd_State = LOW;
byte buttonSleep_State = LOW;
byte motorState = HIGH;
unsigned long currentMillis; // stores the value of millis() in each iteration of loop()
unsigned long previousMotorMillis; // will store last time the motor was updated
unsigned long motorInterval = motorBaseInterval;
unsigned long motorOffMillis = motorSleepInterval;
volatile byte button_flag = 0;
volatile byte button_flag1 = 0;
volatile byte sleep_flag = 0;
//========

void setup() {

Serial.begin(9600);
// set the Motor and Buttons pins as output, INPUT_PULLUP:
pinMode(motorDirection_Pin, OUTPUT);
pinMode(motorSpeed_Pin, OUTPUT);
pinMode(buttonSleep_Pin, INPUT_PULLUP);
pinMode(buttonEnd_Pin, INPUT_PULLUP);

}

//=======
void loop() {

currentMillis = millis();
checkButtons();
execute_motor();
end_position();
sleep_position();
}

//========
void checkButtons() {
buttonEnd_State = digitalRead(buttonEnd_Pin);
buttonSleep_State = digitalRead(buttonSleep_Pin);
}

void execute_motor() {
checkButtons();
// nothing happens unless the interval has expired
if (currentMillis - previousMotorMillis >= motorInterval) {
//time is up, its time for another motor rotation
previousMotorMillis += motorInterval;
motorState = ! motorState;
if (motorState == HIGH) {
digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor rotation
analogWrite(motorSpeed_Pin, motor_speed);
motorInterval = rotatiOnMillis;
Serial.println("motor rotation");
}
else {
//nothing happens unless the interval has expired, still motor is not rotating
motorInterval = motorBaseInterval;
analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
Serial.println("motor stop, speed is 0");
}
digitalWrite(motorDirection_Pin, motorState);

}
}

void end_position() {

checkButtons();
/* Check if End Button is pressed, if yes, stop the motor*/
if (buttonEnd_State == HIGH) {

analogWrite(motorSpeed_Pin, 0); //set 0 speed for hinge motor
Serial.println("end position");
/* Check if Sleep Button is pressed, run the motor to opposite direction till Sleep Button is pressed */
while (buttonSleep_State == LOW) {
digitalWrite(motorDirection_Pin, LOW); //set direction for hinge motor
analogWrite(motorSpeed_Pin, motor_speed);
button_flag=1;
checkButtons();
Serial.println("end position, going home");
}
}
}
void sleep_position(){
/* Check if Sleep Button is pressed, if its pressed stop the motor and set the sleep interval */
if (buttonSleep_State == HIGH && button_flag==1) {
analogWrite(motorSpeed_Pin, 0);
//button_flag1=1;
Serial.println("stop, you are in sleep position now");
motorInterval = motorSleepInterval;
Serial.println("sleepinngg bzzzz");
}
}
 
Top