Multiple Reed Switch code

Hello,

Id like to use my arduino to detect multiple reed switches. When each reed switch is active (i e a magnet is near by) an LED blinks. Ive currently added another reed switch onto my board using a very similar configuration as the below image with the additional reed switch being hooked up to pin 2. Please find the code i tried to use below.

const int pinReedA = 2;
const int pinReedB = 12;
const int pinLed = 9;
int StatoSwitchA = 0;
int StatoSwitchB = 0;
void setup(){

pinMode(pinReedA, INPUT); 
pinMode(pinReedB, INPUT);
pinMode(pinLed, OUTPUT);

}

void loop()

{

StatoSwitchA = digitalRead(pinReedA);
StatoSwitchB = digitalRead(pinReedB);

if (StatoSwitchA == HIGH, StatoSwitchB == HIGH)

{

digitalWrite(pinLed, HIGH);
}

else

{

digitalWrite(pinLed, LOW);

}

}

Bear in mind ive only just finished the tutorials and am now looking to try create my own projects but im struggling to string multiple sensors together.

Id also eventually like to put a magnet near my sensor, and that would acctivate an LED to remain on, then when i place the magnet back near the switch it turns off. Id like to do this with multiple LEDS and Reed Switches but again, id like to learn how to code multiple inputs.

Thanks and i appreciate anything you can share.

Hi egtz,

First you have one more thing to read: the guidelines for posting here. I haven't gone through all your code, but this line:

if (StatoSwitchA == HIGH, StatoSwitchB == HIGH)

makes me suspicious. Do you mean:

if (StatoSwitchA == HIGH or StatoSwitchB == HIGH)

?

ChrisTenone:
Hi egtz,

First you have one more thing to read: the guidelines for posting here. I haven't gone through all your code, but this line:

if (StatoSwitchA == HIGH, StatoSwitchB == HIGH)

makes me suspicious. Do you mean:

if (StatoSwitchA == HIGH or StatoSwitchB == HIGH)

?

Hey Chris

instead of or, i was kind of looking for something like and. in the sense, i want the led to turn on only when both switches are activated

Figured it out, tried using 'and' and it worked exactly how i wanted it to.

Thanks chris

Could connect the reed switches in series and use just one input. This would be a hard wired AND connection. Then just need to monitor the input with something like

  if (StatoSwitches == HIGH) {
    // turn led on
  } else {
    // turn led off
 }

egtz:
Figured it out, tried using 'and' and it worked exactly how i wanted it to.

Thanks chris

The words "and" and "or" are human friendly versions of the C words && and ||. You can use either (I prefer or and and, because I love self-documenting code.)

dlloyd:
Could connect the reed switches in series and use just one input. This would be a hard wired AND connection. Then just need to monitor the input with something like

  if (StatoSwitches == HIGH) {

// turn led on
  } else {
    // turn led off
}

And if you wire them in parallel, it's a hardware or. In some cases this is the preferable solution (because hardware logic takes 0 cycles and consumes 0 bytes of memory.) Once you solder it together however, it's more difficult to modify than the software version.

1 Like

egtz:
i appreciate anything you can share.

Perhaps your loop() function should contain such code:

bool ledState=digitalRead(pinReedA)||digitalRead(pinReedB) ;
digitalWrite(pinLed, ledState);

So the LED will be ON if one of the reed contacts is activated, otherwise OFF.

1 Like