# Four Reed Switches Input for LED Output

A magnet is placed on the switch to illuminate a light. Once four magnets are placed and all four LED’s are lit, a separate group of five LED lights will light in a specific pattern.

Right now, without any magnets placed, the first LED is lit indicating a magnet is near when it is not. The separate LED pattern is activated without any magnets activating the switches.

When magnets are placed on the switches nothing happens.

``````//Code for left side
//lights will come on when jar is placed correctly
//inside lights will blink on pattern when the puzzle is complete

int led1=5;
int led2=6;
int led3=7;
int led4=8;
int switch1=1;
int switch2=2;
int switch3=3;
int switch4=4;

void setup() {
Serial.begin (9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
pinMode (switch1, INPUT);
pinMode (switch2, INPUT);
pinMode (switch3, INPUT);
pinMode (switch4, INPUT);

}

void loop() {
if (digitalRead(switch1) == HIGH)
{
digitalWrite(led1, LOW);
Serial.println("Switch 1 On");
}
else {
digitalWrite(led1, HIGH);
Serial.println("Switch 1 is Off");
}

if (digitalRead(switch2) == HIGH)
{
digitalWrite(led2, LOW);
Serial.println("Switch 2 On");
}
else {
digitalWrite(led2, HIGH);
Serial.println("Your 2 is Off");
}

if (digitalRead(switch3) == HIGH)
{
digitalWrite(led3, LOW);
Serial.println("Switch 3 On");
}
else {
digitalWrite(led3, HIGH);
Serial.println("Your 3 is Off");
}

if (digitalRead(switch4) == HIGH)
{
digitalWrite(led4, LOW);
Serial.println("Switch 4 On");
}
else {
digitalWrite(led4, HIGH);
Serial.println("Your 4 is Off");

}

if (digitalRead(switch1) == HIGH && digitalRead(switch2) == HIGH && digitalRead(switch3) == HIGH && digitalRead(switch4) == HIGH)

}
else
{
delay (2500);
delay (2500);
delay (2500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
Serial.println("Puzzle is solved");
delay (5000);
}
}
``````

Can’t see your resistors.

You need to wire the reed switches from the respective pins to ground, and set the pinMode to INPUT_PULLUP. Inputs will read LOW when the switch is closed - that’s how you wire switches. (OK, the tutorials are misleading. )

LEDs do need series resistors to set the current.

To take a usable photo, take the assembly outside in full daylight - but not in the sun.

The LED lights have built-in integral resistors. The blue LED’s are connected to the pin on the arduino and ground, and so are the white LED’s.

Here is a tutorial that is not misleading Inputs

int switch1=1;

Pin1 is the TX pin of an Uno, and is already used by the USB<>Serial chip.

If you run out of digital pins, then use the analogue pins.
A0-A5 are just digital pins with the added functionality of analogue-in.

int switch1 = A0;
or
int switch1 = 14; // ok on an Uno
or, better (less memory use)
const byte switch1 = A0; // value is never changed
Leo…

davidsebek71:
The LED lights have built-in integral resistors.

Interesting!

davidsebek71:
The blue LED’s are connected to the pin on the Arduino and ground, and so are the white LED’s.

So why are they connecting to the bus with the red trace? You still need to take it outside into daylight to take a usable photo.

Here is a better picture, so I should change the reed switches to the analog pins?

I only said that you can't use digital pin1 of an Uno (like you have in your code).
I don't see any wires connected to it.

You only need five wires for the switches, not a ratsnest like that.

Connect one wire of each switch to the ground rail of the breadboard
(assuming you have it also connected to Arduino ground).
Then connect the other wire of the switches to four input pins.
NO resistors.

Then enable internal pull up in code:
pinMode(switch1, INPUT_PULLUP);
etc.
Reverse logic now, so
if (digitalRead(switch1) == HIGH)
might have to change to
if (digitalRead(switch1) == LOW) // low when the button is pressed
Leo..

I cleaned up the switches and changed the code to INPUT_PULLUP.

When I place a magnet on the switch it takes about 10 seconds for the light to turn blue indicating the item has been put in the right place.

The five blinking switches are still working through their pattern with no input from the magnets.

The puzzle should be four magnets placed in the right position, with each object placed correctly one blue led lights up. When all four switches are activated the blinking light sequence should begin.

Still not sure where my logic is breaking down now.

Post your new code.
Leo..

``````//Wizard table code for left side
//lights will come on when jar is placed correctly
//inside lights will blink on pattern when the puzzle is complete

int led1=5;
int led2=6;
int led3=7;
int led4=8;
int switch1=A0;
int switch2=2;
int switch3=3;
int switch4=4;

void setup() {
Serial.begin (9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
pinMode (switch1, INPUT_PULLUP);
pinMode (switch2, INPUT_PULLUP);
pinMode (switch3, INPUT_PULLUP);
pinMode (switch4, INPUT_PULLUP);

}

void loop() {
if (digitalRead(switch1) == LOW)
{
digitalWrite(led1, LOW);
Serial.println("Switch 1 On");
}
else {
digitalWrite(led1, HIGH);
Serial.println("Switch 1 is Off");
}

if (digitalRead(switch2) == LOW)
{
digitalWrite(led2, LOW);
Serial.println("Switch 2 On");
}
else {
digitalWrite(led2, HIGH);
Serial.println("Your 2 is Off");
}

if (digitalRead(switch3) == LOW)
{
digitalWrite(led3, LOW);
Serial.println("Switch 3 On");
}
else {
digitalWrite(led3, HIGH);
Serial.println("Your 3 is Off");
}

if (digitalRead(switch4) == LOW)
{
digitalWrite(led4, LOW);
Serial.println("Switch 4 On");
}
else {
digitalWrite(led4, HIGH);
Serial.println("Your 4 is Off");

}

if (digitalRead(switch1) == LOW && digitalRead(switch2) == LOW && digitalRead(switch3) == LOW && digitalRead(switch4) == LOW)

}
else
{
delay (2500);
delay (2500);
delay (2500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
Serial.println("Puzzle is solved");
delay (5000);
}
}
``````

Oh my!

It has delay() calls in it!

Your current code goes through the "else...puzzle is solved" block (with all the delays) when even one switch is NOT active (off).
Is that what you want?

If you want the opposite, then change the four conditions from LOW to HIGH in the long if() statement.
Leo..

Paul__B:
Oh my!

It has delay() calls in it!

I don't see a problem there, because that else statement is only executed when the puzzle is solved.
18 seconds to "puzzle is solved" is a long wait though.
And it repeats if the switches stay in the same state.
Leo..

Your current code goes through the "else...puzzle is solved" block (with all the delays) when even one switch is NOT active (off).
Is that what you want?

That is not what I want. I want to place one magnet and one led blue light comes on. I do not want to run the sequence until all four magnets are in place. Thank you for your help so far!

So if all four switches are active (LOW), you want the five LEDs to blink in sequence and print "solved" ?

Then swap the "if block" with the "else block".

``````  if (digitalRead(switch1) == LOW && digitalRead(switch2) == LOW && digitalRead(switch3) == LOW && digitalRead(switch4) == LOW)
{ // if ALL switches are active(low)...
delay (2500);
delay (2500);
delay (2500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
delay (1500);
Serial.println("Puzzle is solved");
delay (5000);

} else
{ // if NOT ALL switches are low