Hello
Could you support with sketch
There is IR sensor that sucsessfully starts/stops DC motor in different directions to open/close sliding door.
2 limit switches stop DC motor when the door reaches these limites.
The problem is that each limit switch stops DC motor permanently and IR sensor doesn`t start/stop a motor anymore. Can you suggest how to continie starting/stoping DC motor via IR sensor while limit switch is pressed (signal is HIGH)?
Thank you in advance.
boolean oldState = LOW;
boolean newState = LOW;
byte state = 1;
void setup() {
pinMode(10, OUTPUT); // outputs for motor
pinMode(12, OUTPUT);
pinMode(5, INPUT); //IR sensor
pinMode(6, INPUT); //limit switch 1
pinMode(7, INPUT); //limit switch 2
}
void loop()
{
int stopButtonState1 = digitalRead(6);
int stopButtonState2 = digitalRead(7);
if (stopButtonState1 == HIGH) { //limit switch 1 is pressed
digitalWrite(10, LOW); //motor is stopped
digitalWrite(12, LOW);
}
if (stopButtonState2 == HIGH) { //limit switch 2 is pressed
digitalWrite(10, LOW); //motor is stopped
digitalWrite(12, LOW);
}
if (newState != oldState)
{
if (newState == LOW) {
state++;
if (state > 1) {
state = 0;
}
digitalWrite(10, LOW); digitalWrite(12, LOW);
if (state == 0) {
digitalWrite(10, HIGH);
digitalWrite(12, LOW);
}
if (state == 1) {
digitalWrite(12, HIGH);
digitalWrite(10, LOW);
}
}
}
oldState = newState;
}
artu:
We read sensor status (from 5 pin) and it allows to rotate motor in both directions but limit switch stops it forever
Change your code so that it only checks for LOW to HIGH transitions on limit switch inputs. That way it will stop the motor when the transition occurs but allow it to move for the sensor even though the input remains HIGH.
I don't see any variable to store the motor direction. If you hit a limit switch, it should stop but if you want it to move again, it should only move away from the limit. For that, you need to allow movement in one direction but not the other.
Also, by this:
if (newState == LOW) {
state++;
if (state > 1) {
state = 0;
}
Did you mean,
if (newState == LOW)
state = not state;
and while we're on the subject, what does State represent? The state of what?
aarg:
I don't see any variable to store the motor direction. If you hit a limit switch, it should stop but if you want it to move again, it should only move away from the limit. For that, you need to allow movement in one direction but not the other.
Also, by this:
if (newState == LOW) {
state++;
if (state > 1) {
state = 0;
}
Did you mean,
if (newState == LOW)
state = not state;
and while we're on the subject, what does State represent? The state of what?
Yes, if it releases the switch button it can moves again but if it move away from the limit it open a door a little bit...
State means direction of motor rotation (opening or closing the door)
artu:
Yes, if it releases the switch button it can moves again but if it move away from the limit it open a door a little bit...
State means direction of motor rotation (opening or closing the door)
Then why not eliminate a lot of confusion, and rename the variable "direction"?
I think nobody can help you until you describe the operation of your door in more detail. When should it move? In what direction? When should it stop? etc...
aarg:
Then why not eliminate a lot of confusion, and rename the variable "direction"?
I think nobody can help you until you describe the operation of your door in more detail. When should it move? In what direction? When should it stop? etc...
The logic is simple: I move my hand, sensor detects an object (my hand), starts to rotate motor in one direction and opens the door, limit switch stops motor. I move my hand again, sensor detects an object, starts to rotate motor in other direction and close the door untill limit switch stops that. Cycle is finish.
The original code works in part to detect and start motor in one direction.When limit switch is pressed the code stops. It must be started again in the other direction when the sensor detects an object, but the limit switch is pressed...