sliding gate project

hi guys!
i have a licence project i'm working on and i'm using Arduino Uno R3 to control a H bridge to operate a miniature sliding gate, with 2 limitswitches. when i press a button, the gate should open untill it hits one limitswitch. The same logic is applied when the gate should close. I don't understand why the board is acting funny because the source code is "correct", and the button and switches are new. please can anyone help??

const int led1 = 11;
const int led2 = 9;
int buton = 7;
int sw1 = 4;
int sw2 = 2;


int buttonstate = 0;
int buttonstate_old = 0;
int sw1state = 0;
int sw2state = 0;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buton, INPUT);
pinMode(sw1, INPUT);
pinMode(sw2, INPUT);
}
void loop() {
  buttonstate = digitalRead(buton);
  sw1state = digitalRead(sw1);
  sw2state = digitalRead(sw2); 

    if((buttonstate == HIGH) && (buttonstate_old == LOW) &&
        (sw1state == LOW) && (sw2state == HIGH )) 
    {
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
    }
   if((sw1state == HIGH) && (sw2state == LOW))
   {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
   }
   if((buttonstate == HIGH) && (buttonstate_old == LOW)
     &&(sw1state == HIGH) && (sw2state == LOW))
    {
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
    }
     if((sw1state == LOW) && (sw2state == HIGH))
   {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
   }
   if((buttonstate == HIGH) && (buttonstate_old == LOW)
      && (sw1state == LOW) && (sw2state == HIGH)) 
    {
      digitalWrite(led1, HIGH);
      digitalWrite(led2, LOW);
    }
}

Post the code according to the sticky post --- how to use this forum.
Most mobile devices do not know how to cope with a .ino file.

i modified the post. i'm not sure whether the board is broken, or the code...i've been stuck like this for a week and i still can't find the problem

Do you use pull up or pull down resistors on the switches, and what are the values.
You can also use the internal pullup resistors, with the switch connected from pin to ground.
pinMode(buton, INPUT_PULLUP);

Are you using normally closed limit switches (safer).
Post a circuit diagram.
Leo..

Hi,
Welcome to the Forum

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

don't understand why the board is acting funny because the source code is "correct"

No it is not. That code is quite wrong.
Think about it. Once the motor is going there will be no change in the limit switch. Once a limit switch is triggered the motor must stop. Then the button will make the motor go again, but initially the limit switch is still triggered. So that code you wrote thinks the limit has been reached immediately and so stops the motor again before it has had a chance to move.

You first need to use logic variables to indicate what way the motor is going. Then you will know what limit switch to look at in order to stop the motor when it reaches the other end. Your code is far too simplistic.

Wawa:
Do you use pull up or pull down resistors on the switches, and what are the values.
You can also use the internal pullup resistors, with the switch connected from pin to ground.
pinMode(buton, INPUT_PULLUP);

Are you using normally closed limit switches (safer).
Post a circuit diagram.
Leo…

yes i’m using normally closed limitswitches and they are connected to pull-down resistors. The thing is: my project worked well when i first put it together (with the code that i posted). After i introduced another condition to the program, it stopped working. i dunno! maybe i’m not seeing something in the code…it’s weird that the first time it worked, and now…nothing…

i dunno! maybe i’m not seeing something in the code

You are not seeing reply #5

it’s weird that the first time it worked, and now…nothing…

No it is not weird, you said:-

After i introduced another condition to the program, it stopped working.

The code was never any good in the first place, it did not do what you thought it did.

To understand why it doesn’t work, you should do what
logic designers do.
They draw a Karnaugh map ( if you don’t know what that is, look it up ).
You have 4 input conditions of switches.
That is 16 states. Make 4 columns with 16 lines.
At the end of each state, show
what the motor is doing in each state.
Look at each state and see how it could get to one of the other
states by changing only one switch.
If you do the map, you’ll see that you have conflicting
and overlapping states.
Dwight

Hi,

The thing is: my project worked well when i first put it together (with the code that i posted).

So does the code you posted, that did work, work now?
Do you have a DMM to measure the signals at the arduino input and output?

Thanks.. Tom... :slight_smile:

In you setup code, I suggest you check the status of your limit switches to see if there was a power failure and your gate is in some mid-position. If so, move the gate to what you want for an initial condition.

Paul

Hello Jack
Did you draw the Karnaugh map as I suggested?
The reason your code failed will be obvious.
You might even learn something as well.
If you have problems drawing a Karnaugh map and
filling in the values come on back and we can go through it.
Dwight

I think the OP has gone away. I got an abusive PM from him. It seems he did not appreciate being told his code was wrong.

One can only hope he will learn to save each iteration of his program so he can compare the old to the new and find the errors.

Paul

Since there was nothing wrong with his code, I guess
there is nothing we can help him with.
Dwight