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.
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.