Hi,
Yesterday I started a thread with a different problem for this topic. As I changed the code significantly, I am starting a new thread and giving some more accurate info (thanks Tom).
I am trying to build a roll-up curtain automated system. It consists of 2 switched (up and down), 2 hall effect switches for the end of stroke (will be embedded into the fabric of the curtain), Arduino One, THIS motor shield and THIS motor.
The configuration of the switches is with a pull-up resistor (real, not the one in Arduino, learning!) and the hall effect sensors are normally closed, thus they always give a HIGH result except when they are activated with a magnet.
My logic is: (IF) you press up or down button AND the end of stroke is not active, move the motor (WHILE) none of the following events happens: the end of stroke is reached or any of the buttons is switched, in which case, stop the motor.
This means, I want it to stop at maximum strokes automatically or press any button to keep it where I want manually.
The code is below (there are some Serial.println to monitor where the code is running) and this is what happens:
Push any switch - motor starts correctly
Placing a magnet on the hall sensor - stops the motor correctly
However: pushing the switch when the motor is moving - stops the motor momentarily (actually, the WHILE loop stops) but it restarts to move. If the button I push is the opposite as the one that started the movement, the motor turns the opposite way.
This tells me that, even though at the beginning of the loop there is a reading of the switch status (and it should be HIGH as the switch is not pressed), the value of the button remains as LOW (as if the switch was pressed).
I hope I made myself clear
If any of you could show me the way to the light I will be more than grateful.
Here is the CODE:
/* ROLLER CURTAIN AUTOMATIC STOP SYSTEM WITH MANUAL CONTROL
THIS PROGRAM INTENDS TO OPERATE A MOTOR TO ROLL UP AND DOWN A FABRIC CURTAIN
THE SET UP IS:
ARDUINO UNO
MOTOR SHIELD HiLetgo L298P DC Motor Drive Module
12VDC GEARED MOTOR
2X HALL EFFECT SENSORS AS END OF STROKE SWITCHES - NORMALLY CLOSED (NORMAL STATE HIGH), ACTIVATED WITH A MAGNET EMBEDDED INTO THE CURTAIN
2X MECHANICAL SWITCHES IN PULL-UP RESISTOR CONFIGURATION (NORMAL STATE HIGH)
OPERATION:
PUSH UP OR DOWN BUTTON TO START ROLLING UP OR DOWN THE CURTAIN, UNLESS THE END OF STROKE OF EACH SIDE IS ACTIVE (MEANING THE CURTAIN IS COMPLETELY OPEN OR CLOSED). IN SUCH CASE NOTHING SHOULD HAPPEN
THE CURTAIN SHOULD STOP IF EITHER THE USER PUSHES ANY BUTTON (UP OR DOWN) OR THE END OF STROKE IS REACHED.
*/
//PIN SELECTION - (SOME PINS NOT AVAILABLE DUE TO THE SHIELD)
int pinUp = 3;
int pinDown = 5;
int eosUp = 6;
int eosDown = 7;
//VARIABLES ASSIGNATION
int buttonUp = 0;
int buttonDown = 0;
int endOfStrokeUp = 0;
int endOfStrokeDown = 0;
//MOTOR PINS SELECTION (SHIELD)
int pinMotor = 10; //ANALOG ---- SPEED OF THE MOTOR
int pinDirection = 12; //DIGITAL: HIGH AND LOW DETERMINE DIRECTION
void setup() {
pinMode(pinUp, INPUT);
pinMode(pinDown, INPUT);
pinMode(eosUp, INPUT);
pinMode(eosDown, INPUT);
pinMode(pinMotor, OUTPUT);
pinMode(pinDirection, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println("beginning of the loop");
buttonUp = digitalRead(pinUp); //READ THE INPUT VARIABLES
buttonDown = digitalRead(pinDown);
endOfStrokeUp = digitalRead(eosUp);
endOfStrokeDown = digitalRead(eosDown);
if (buttonUp == LOW && endOfStrokeUp == HIGH) // IF UP BUTTON IS PRESSED AND UPPER END OF STROKE IS NOT ACTIVATED
{
Serial.println("Entered IF up button activated");
delay(500); //DELAY TO LET TIME FOR THE BUTTON BE RELEASED
buttonUp = HIGH; //GIVE BACK THE VALUE "HIGH" TO THE VARIABLE TO ALLOW THE WHILE LOOP TO START
while (endOfStrokeUp == HIGH && buttonUp == HIGH && buttonDown == HIGH) //WHILE THERE ARE NO BUTTONS PRESSED, THE END OF STROKE IS NOT ACTIVE
{
analogWrite(pinMotor, 255); // MOVE MOTOR MAX SPEED
digitalWrite(pinDirection, HIGH); // MOVE MOTOR UP DIRECTION
Serial.println("Entered in While UP, motor up moving");
buttonUp = digitalRead(pinUp); //READ VARIABLES INSIDE THE LOOP
buttonDown = digitalRead(pinDown);
endOfStrokeUp = digitalRead(eosUp); //READ UPPER END OF LINE (NO NEED TO READ THE LOW BOTTOM END OF STROKE BECAUSE THE MOTOR IS MOVING THE CURTAIN UP)
}
Serial.println("Exit while up, proceeding to stop motor from up movement");
analogWrite(pinMotor, 255); //MAKE A REVERSE MOVEMENT TO BREAK AND STOP MOTOR
digitalWrite(pinDirection, LOW);
delay(100);
analogWrite(pinMotor, 0);
digitalWrite(pinDirection, LOW); //THIS LINE CAN POSSIBLY BE OMITTED: TRY BOTH WAYS
Serial.println("Motor stopped");
}
if (buttonDown == LOW && endOfStrokeDown == HIGH) //IF DOWN BUTTON IS PRESSED (PULLUP RESISTOR) AND UPPER END OF STROKE IS NOT ACTIVATED (HALL EFFECT SENSOR, N.C.)
{
Serial.println("Entered IF down button pushed");
delay(500); //DELAY TO LET TIME FOR THE BUTTON BE RELEASED
buttonDown = HIGH; //GIVE BACK THE VALUE HIGH TO THE VARIABLE TO ALLOW THE WHILE LOOP TO START
while (endOfStrokeDown == HIGH && buttonUp == HIGH && buttonDown == HIGH) //WHILE THERE ARE NO BUTTONS PRESSED, THE END OF STROKE IS NOT ACTIVE
{
analogWrite(pinMotor, 255); // MOVE MOTOR MAX. SPEED
digitalWrite(pinDirection, LOW); // MOVE MOTOR DOWN DIRECTION
Serial.println("Entered in While DOWN, motor up moving");
buttonUp = digitalRead(pinUp); //READ VARIABLES INSIDE THE LOOP
buttonDown = digitalRead(pinDown);
endOfStrokeDown = digitalRead(eosDown); //NO NEED TO READ THE LOW BOTTOM END OF STROKE BECAUSE THE MOTOR IS MOVING UP
Serial.println("Exit while down, proceeding to stop motor from down movement");
}
analogWrite(pinMotor, 255); //MAKE A REVERSE MOVEMENT TO BREAK AND STOP MOTOR
digitalWrite(pinDirection, HIGH);
delay(100);
analogWrite(pinMotor, 0);
digitalWrite(pinDirection, HIGH);
Serial.println("Motor stopped from down movement");
}
Serial.print("Loop finished");
}