I started with electronics and arduino not long ago, and I'm now facing a problem with a project.
I decided to automate my curtains so they could open and close with one toggle switch and two magnetic limit switch( when the switch is moved the curtain should go in one direction until it hit the limit switch and do the same in the other direction) but the switches dosen't seams to do anything.
Do you have any ideas where could it come from?
Here is the code
When posting code, please use code tags not quotes.
I recommend that you use constants for your pin numbers instead of hard-coding the numbers throughout your code. It makes it much easier to spot where you are using the wrong pin, and makes it much easier and safer if you ever need to change the pin number assignments subsequently.
I recommend that you use meaningful names for the variables holding the values read from those inputs. Sensor1Val, sensor2Val, sensor3Val gives me no clue what real-world thing that input corresponds to.
Once you address the comments above, it would be much easier to see whether the logic in your sketch is correct.
Also, I recommend that you confirm that your switch inputs are being detected correctly and that you are able to drive the motor correctly before you move on to test the final solution. Printing out the input states and logging the commands sent to the motor would make it much easier for you (and us) to see what was happening inside the Arduino and guess at the cause when it goes wrong.
Do you realise that with a suitable switch and two limit switches it would be possible to implement the controller entirely in hardware, no microcontroller needed at all?
Hi PeterH
Thanks for the fast awnser.
I've modified the code,I don't know if thats wath you meaned.
I have a few question:
1-How do I confirm that the switches are detected
2-I've got a motor shield (with an 74hct595 chip) with an adafruit librairies but it take to many pins on the uno. Do you know how to use it without a librairie so it took less place.
3-I use some normaly open magnetic limit switch How could I do to make it entirely an hardware setup?
void setup() {
 pinMode(3, INPUT_PULLUP); //top limit switch
 pinMode(4, INPUT_PULLUP); //bottom limit switch
 pinMode(5, INPUT_PULLUP); //toggle switch
 pinMode(12, OUTPUT); //Motor channel
 pinMode(9, OUTPUT); //Brake channel
}
void loop() {
 int toplimitVal = digitalRead(3);
 int bottomlimitVal = digitalRead(4);
 int switchVal = digitalRead(5);
Â
 if (switchVal == HIGH) {
 Â
  if (toplimitVal == LOW) {
 Â
   digitalWrite(12, HIGH); //forward
   digitalWrite(9, LOW); //brake
   analogWrite(3, 255); //speed
   Â
  }
 Â
  else {
 Â
   digitalWrite(9, HIGH); //brake
  }
 Â
 }
Â
 else {
Â
  if (bottomlimitVal == LOW) {
  Â
   digitalWrite(12, LOW); //backward
   digitalWrite(9, LOW); //brake
   analogWrite(3, 255); //speed
  }
 Â
  else {
  Â
   digitalWrite(9, HIGH); //Eengage the Brake for Channel A
  }
 }
Â
}
You're using Pin 3 for your top limit switch, but later, you're using it as a PWM pin to control motor speed. AnalogWrite does not imply that it uses the analog pins, which I suspect may be what led you astray. I note also, that you never try to set the motor speed to anything other than full speed - shouldn't it be turned off when you apply the brake?
I mannaged to finish my project by changing the code and the shematics.
const int switchPin = 8; //the selection switch
const int topswitchPin = 7; //the top limit switch
const int btmswitchPin = 12; //the bottom limit switch
const int motor1Pin1 = 3; //arduino pin 3 l293d pin 2
const int motor1Pin2 = 4; //arduino pin 4 l293d pin 7
const int enablePin = 9; //arduino pin 9 l293d pin 1
void setup() {
Â
 pinMode(switchPin, INPUT);
 pinMode(topswitchPin, INPUT);
 pinMode(btmswitchPin, INPUT);
Â
 pinMode(motor1Pin1, OUTPUT);
 pinMode(motor1Pin2, OUTPUT);
 pinMode(enablePin, OUTPUT);
 digitalWrite(enablePin, HIGH);
}
void loop() {
Â
 if (digitalRead(switchPin) == HIGH) {
 Â
  if (digitalRead(topswitchPin) == LOW) {
   //turn clockwise
   digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D LOW
   digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D HIGH
  }
 Â
  else {
   //stop
   digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D HIGH
   digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D HIGH
   }}
 else {
 Â
  if (digitalRead(btmswitchPin) == LOW) {
   //turn anticlockwise
   digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D HIGH
   digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D LOW
   }
  Â
  else {
   //stop
   digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D HIGH
   digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D HIGH
   }
   }}