Limit switches again

I know I'm missing something very simple here. BUT it's been many years since I did any programming. I want to use a couple of limit switches and I just can't find the info. I need!!! I have a simple simulator program, a relay a motor and two slider switches to represent the limit switches. I hope I don't cause too many guffaws, but this is the code I'm trying to debug.
Help will be appreciated, ta muchly!

<<// C++ code
//

#define LIMIT_SWITCH_PIN 7
#define LIMIT_SWITCH2_PIN 8

void setup()
{
pinMode(12, OUTPUT);
pinMode(LIMIT_SWITCH_PIN, INPUT);
pinMode(LIMIT_SWITCH2_PIN, INPUT);

}

void loop(){

digitalWrite(12,HIGH);

if (digitalRead(LIMIT_SWITCH_PIN) == LOW)
{
digitalWrite(12,LOW);
}
else
{
digitalWrite(12,HIGH);
}

}>>

What have you tried and what were the results? What is the code supposed to do?

Did you get a warning about your code needing to be formatted?

Please use code tags, I'm sure you must have got a reminder about that when you posted.

pinMode(LIMIT_SWITCH_PIN, INPUT);
pinMode(LIMIT_SWITCH2_PIN, INPUT);

Pull up resistors?

Schematic?

aka

digitalWrite(12, digitalRead(LIMIT_SWITCH_PIN));

As I say, I just need a motor to turn one lever and stop at the limits, using two switches, nothing drastic.


Hopefully the tinkercad image will be viewable..

Thanks.

You need to pull up the inputs.

You cannot drive a relay directly from a Uno pin, well, assuming that is just a bare relay with no driver on board. You MUST disconnect the relay, you will damage the output with it like that, and possibly the whole processor.

Thanks for the info!!! The actual device is a relay board with an optocoupler and a separate 5 volt supply. It's the simulation which is causing me problems! But the pull up resistors did help greatly, many thanks again!!

Try very hard to give us as much information as you can.

Do this in the very beginning so we don’t waste our or you time.

An accurate schematic is always required.

A complete explanation of what you need the sketch to do is also a must.

Did you use actual resistors or the built in pull up resistors?

pinMode(LIMIT_SWITCH_PIN, INPUT_PULLUP);

When someone is ACTUALLY trying to debug their program you will see cookie crumbs in the form of "serial.print()" scattered throughout the code.
Paul

I added resistors "in line" as it were on the sim, worked first time! Thanks again..

Sim resistors are free, unlike the real world.

Remove your first line in the beginning of the loop
"digitalWrite(12,HIGH);"
It will already be high at the else part of your statement.

...or maybe the else didn't get executed.

It's a switch it's either pressed or not pressed

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.