Problem with multiple inputs

Hello all ... I am a total newbie working on my first project.

I have a toy gate on rollers which closes at start and then it can be opened by pressing a momentary switch or with an IR sensor.

The gate's limits are controlled by limit switches. The gate motor is controlled by a L298N board.

I have wired the limit switchs' common to 5V (on a breadboard) and their NO pins to two different inputs on the Arduino.

I have connected the momentary switch the same way -- one pole to 5V and the other pole to an Arduino input.

The sketch is as follows:

// Motor connections
int enA = 9;
int in1 = 8;
int in2 = 7;

//SENSORS
int in3 = 3; //IR Sensor
int in4 = 4; //Close limit Switch
int in5 = 5; //Open Limit Switch
int in6 = 6; //Button

int IRSensor = digitalRead(3);
int CloseLimit = digitalRead(4);
int OpenLimit = digitalRead(5);
int ButtonValue = digitalRead(6);

void setup() {
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);

// Set all sensor pins to inputs
pinMode(in3, INPUT);
pinMode(in4, INPUT);
pinMode(in5, INPUT);
pinMode(in6, INPUT);

}

void CloseGate ();

void loop() {

if (ButtonValue == HIGH || IRSensor == HIGH) {

void OpenGate ();

}
}

//Close Gate - Initial State

void CloseGate()

{

if (CloseLimit == LOW) {
analogWrite(enA, 240);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}

else {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}

void OpenGate()

{

analogWrite (enA, 240);
digitalWrite (in1, LOW);
digitalWrite (in2, HIGH);

if (OpenLimit == HIGH) {

digitalWrite (in1, LOW);
digitalWrite (in2, LOW);
delay (5000);
}

}


My problem is this - nothing happens when I upload it and reset. I turned serial on to check the values of the variables. They all record 0 -- but if either limit switch or the button is pressed, they all turn to 1.

Any help you can give shall be appreciated. Thanks!

In your loop() you don't do anything to update ButtonValue or IRSensor.

Using a pin mode of INPUT without pullup or pulldown resistors is a recipe for disaster. I predict a request for more help for the intermittent operation.

I have connected the momentary switch the same way -- one pole to 5V and the other pole to an Arduino input.

Always do it the other way round. Switched between input and ground and enable the internal pull up resistors. Then look for a low to occur when you press the button.

 int IRSensor = digitalRead(3);
int CloseLimit = digitalRead(4);
int OpenLimit = digitalRead(5);
int ButtonValue = digitalRead(6);

That is silly, you only set those values here so they are fixed for all time as those lines are never executed again.

I think you haven't understood that digitalRead() samples the input once - it doesn't set up some sort
of magical connection between the input and the variable that happens to receive the return value from
calling digitalRead.

This isn't doing anything useful:

//SENSORS
int in3 = 3; //IR Sensor
int in4 = 4; //Close limit Switch
int in5 = 5; //Open Limit Switch
int in6 = 6; //Button

You'd be better off naming the pins with their function:

const byte IR_sensor_pin = 3 ;
const byte close_limit_pin = 4 ;
const byte open_limit_pin = 5 ;
const byte button_pin = 6 ;

Then you write something like this:

  if (digitalRead (button_pin) || digitalRead (IR_sensor_pin)) {

instead of the broken:

  if (ButtonValue == HIGH || IRSensor == HIGH) {

A HIGH value is treated as true, so you don't need to compare to HIGH - the value from
digitalRead is a truth value already.

However if you do use switches to ground you might want to handle the interpretation of input
values in a function like this:

bool switch_pressed (byte pin)
{
  return digitalRead (pin) == LOW ; // only line in the program that has to know buttons are active LOW
}
...
...
  if (switch_pressed (button_pin) || switch_pressed (IR_sensor_pin))
...

One important principles in coding is to "separate concerns" - details about how the buttons are wired
shouldn't be scattered throughout the code, they should be in one place. Then if you need to change
it ever, its easy to do without error.

If in doubt write a well-named function to do something, makes code readable, helps separate concerns.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

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:

  if (ButtonValue == HIGH || IRSensor == HIGH) {

    void OpenGate ();

  }

And that isn't the way you call a function. Lose the 'void'. It's only needed when you define the function NOT when you use it.

Steve

A HIGH value is treated as true, so you don't need to compare to HIGH - the value from
digitalRead is a truth value already.

Correct, but because that is the way that it is currently. digitalRead(...) is documented to return LOW or HIGH. It happens that these are currently equivalent to true and false and this makes coding easier but it is not guaranteed in the future.

That having been said, a lot of software will break if this ever changes. My preference is to update the documentation.

vaj4088:
That having been said, a lot of software will break if this ever changes.

Yes but what demented software writer would ever change this? I know compiler writers are not the sanest of people but there are limits.

Grumpy_Mike, I agree with you, but the documentation has NEVER been updated for this.

In the non-Arduino world, I have seen things change that I would have thought would never change. Sigh.

This is open source, so the code is part of the documentation and I can see

#define HIGH 0x1
#define LOW  0x0

So I know they are interconvertible with bool's true and false