Simple led won't do what I want it to do

Hi

I just started learning Arduino. My knowledge of programming is also very limited, but I’m a quick learner and understand logic. I’m going to be constructing stuff for an awesome escape room so I’m trying to learn the basics to be able to do so. (I hate padlocks).

This is my first project and I already got confused. Oh right. I don’t know anything about electronics. Never soldered a thing in my life. Google and youtube helped a lot there.

I’m trying to have a white led lit untill the right combination of other leds (just greens and none of the yellows) are lit. I’m currently using self-locking switches that are connected to leds and send signal to arduino when the led is on.

The problem is that it works like it’s supposed to for just a little while after being reseted. Then the white led just stays on. No matter what buttons are pressed.

int GreenSwitch1 = 50;
int GreenSwitch2 = 51;
int GreenSwitch3 = 53;
int YellowSwitch1 = 30;
int YellowSwitch2 = 31;
int ledPin = 22;

void setup() {
  // Set up switches as INPUTS and the LED as OUTPUT
  pinMode(GreenSwitch1, INPUT);
  pinMode(GreenSwitch2, INPUT);
  pinMode(GreenSwitch3, INPUT);
  pinMode(YellowSwitch1, INPUT);
  pinMode(YellowSwitch2, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // Check if the right LEDs are on
  if (digitalRead(GreenSwitch1) == HIGH && 
      digitalRead(GreenSwitch2) == HIGH && 
      digitalRead(GreenSwitch3) == HIGH && 
      digitalRead(YellowSwitch1) == LOW && 
      digitalRead(YellowSwitch2) == LOW)
  {
    digitalWrite(ledPin, LOW); // Shut down the LED
  }
  else
  {
    digitalWrite(ledPin, HIGH); // Keep the LED on
  }
}

Have you got pullup resistors on the input switches ??

You could try adding this code for each one of your switches pinMode(xxxxxxxxxxx, INPUT_PULLUP);, The xxxxxx are your switches. This is providing that your switches are switching ground. If you are switching 5V then you will need to add pull down resistors or you could try this method for each switch

pinMode(xxxxxxxxxxxx, INPUT);                  //xxxxx switch. Grounded in MAN position.
    digitalWrite(xxxxxxxxxxx, LOW);              // set port low to be pulled high

Thank you. INPUT_PULLUP fixed it. Now working perfectly.

Next thing to figure out how to make Arduino track a sequence of button pushes for the other projects.

EDIT: Now that I realised what INPUT_PULLUP does, seems weird that I had to useit as I already have 10k resistors hooked to the switches.

If you are switching 5V then you will need to add pull down resistors or you could try this method for each switch Code: [Select] pinMode(xxxxxxxxxxxx, INPUT); //xxxxx switch. Grounded in MAN position. digitalWrite(xxxxxxxxxxx, LOW); // set port low to be pulled high

No, that won't work, at least not on a regular Arduino. The atmega chip has internal pull-up resistors, but it has no internal pull-down resistors.

Now that I realised what INPUT_PULLUP does, seems weird that I had to useit as I already have 10k resistors hooked to the switches

Your 10K should be between the input pin and 5V, with the switch between the pin and ground. Not in series or in parallel with the switch.

Next thing to figure out how to make Arduino track a sequence of button pushes for the other projects.

Then you need to learn about several new concepts including detecting button state changes and button debouncing.

Start with a simple project to count the number of times a button has been pressed and echo the count to serial monitor.

So I should rewire this and try to get it working without the INPUT_PULLUP?

|500x375

Currently it's set up so ground to 10k resistor to signal in on arduino and one pin on the switch. The other pin from the switch is connected to led by wire and the led is conncted to 220 resistor that connects to 5v.

Lose the resistors (use input_pullup)
Let the local police know that you have an escape room, so they can let you out.

Now I understand your circuit a little better.

This is what I would recommend: 5V --> switch --> Arduino pin --> led --> 220R --> ground

Don't use INPUT_PULLUP or the 10K.

The led/series resistor will pull the Arduino pin down when the switch is not pressed. When the switch is pressed, the Arduino pin will read high and the led will be on.

I will try that after I get back home from this 22h shift. Thank you for the help. You are being awesome!

The end product won't need the leds, but I wanted to see something happening without serial monitor :)

In that case, wire them like this: 5V --> 220R --> led --> Arduino pin --> switch --> ground

Change your code to use INPUT_PULLUP and swap HIGH for LOW and vice versa in your sketch (where it is the value read from a button pin).

That means you can remove the led & 220R later and the circuit will continue to work with the sketch unaltered

This is just me testing things and figuring stuff out. The final product will be having around 40 inputs with multiple right combinations. Each triggering a different thing. Still trying to decide if I should use some kind of matrix for that. But the mega has enough inputs and it seems easier to code it without one.

Parallel to Serial ICs like the 74HC165 series.

They take 8 digital inputs (like your buttons) and can be cascaded (so you'd want 5 of these chips for 40 inputs). A few arduino pins can then be used to read the status of each button.

http://playground.arduino.cc/Code/ShiftRegSN74HC165N

PaulRB: Now I understand your circuit a little better.

This is what I would recommend: 5V --> switch --> Arduino pin --> led --> 220R --> ground

Don't use INPUT_PULLUP or the 10K.

The led/series resistor will pull the Arduino pin down when the switch is not pressed. When the switch is pressed, the Arduino pin will read high and the led will be on.

Works like a charm. Thank you.

PaulRB: Start with a simple project to count the number of times a button has been pressed and echo the count to serial monitor.

Wrote something that seems to work nicely. Couldn't keep it as simple as you wanted tho :D

//Checks if the right switches are pressed down (green)

int GreenSwitch1 = 50;
int GreenSwitch2 = 51;
int GreenSwitch3 = 52;
int YellowSwitch1 = 30;
int YellowSwitch2 = 31;
int ledPin = 22;
boolean lastGreenSwitch1 = LOW;
boolean lastGreenSwitch2 = LOW;
boolean lastGreenSwitch3 = LOW;
boolean lastYellowSwitch1 = LOW;
boolean lastYellowSwitch2 = LOW;
int puzzleCount = 0;

void setup() {
  // Set up switches as INPUTS and the LED as OUTPUT
  pinMode(GreenSwitch1, INPUT);
  pinMode(GreenSwitch2, INPUT);
  pinMode(GreenSwitch3, INPUT);
  pinMode(YellowSwitch1, INPUT);
  pinMode(YellowSwitch2, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
}

void loop() 
{

// Green buttons need to be pressed in order 1,2,3. Wrong order or any yellow button resets the puzzle.

//Green Switch 1
  if (digitalRead(GreenSwitch1) == HIGH && lastGreenSwitch1 == LOW) //check if the button was pressed
  {
    delay(5); //Delay and check again for debounce
    if (digitalRead(GreenSwitch1) == HIGH && lastGreenSwitch1 == LOW)
    {
      lastGreenSwitch1 = HIGH; //set the last known status to pressed
      if (puzzleCount == 0) //check if the puzzle count is at right number for the button to be pressed
      {
        puzzleCount++; //advance the puzzle and...
        Serial.println("Puzzle advanced to count: "); //...send message to console
        Serial.println(puzzleCount);
      }
      else //wrong button pressed
      {
        puzzleCount = 0; //reset the puzzle and...
        Serial.println("You failed. Puzzle reset."); //...send message to console
      }
    }
  }
  else
  {
    lastGreenSwitch1 = digitalRead(GreenSwitch1); //set the last known status to current status of the button
  }

//Green Switch 2
  if (digitalRead(GreenSwitch2) == HIGH && lastGreenSwitch2 == LOW)
  {
    delay(5); //Delay and check again to ensure debounce
    if (digitalRead(GreenSwitch2) == HIGH && lastGreenSwitch2 == LOW)
    {
      lastGreenSwitch2 = HIGH;
      if (puzzleCount == 1) 
      {
        puzzleCount++;
        Serial.println("Puzzle advanced to count: ");
        Serial.println(puzzleCount);
      }
      else 
      {
        puzzleCount = 0;
        Serial.println("You failed. Puzzle reset.");
      }
    }
  }
  else
  {
    lastGreenSwitch2 = digitalRead(GreenSwitch2);
  }

//Green Switch 3
  if (digitalRead(GreenSwitch3) == HIGH && lastGreenSwitch3 == LOW)
  {
    delay(5); //Delay and check again to ensure debounce
    if (digitalRead(GreenSwitch3) == HIGH && lastGreenSwitch3 == LOW)
    {
      lastGreenSwitch3 = HIGH;
      if (puzzleCount == 2) 
      {
        Serial.println("Puzzle Cleared!");
        digitalWrite(ledPin, HIGH);
      }
      else 
      {
        puzzleCount = 0;
        Serial.println("You failed. Puzzle reset.");
      }
    }
  }
  else
  {
    lastGreenSwitch3 = digitalRead(GreenSwitch3);
  }

//Yellow Switch 1 (Resets the puzzle)
  if (digitalRead(YellowSwitch1) == HIGH && lastYellowSwitch1 == LOW)
  {
    delay(5); //Delay and check again to ensure debounce
    if (digitalRead(YellowSwitch1) == HIGH && lastYellowSwitch1 == LOW)
    {
        lastYellowSwitch1 = HIGH;
        puzzleCount = 0;
        Serial.println("You failed. Puzzle reset.");
    }
  }
  else
  {
    lastYellowSwitch1 = digitalRead(YellowSwitch1);
  }

//Yellow Switch 2 (Resets the puzzle)
  if (digitalRead(YellowSwitch2) == HIGH && lastYellowSwitch2 == LOW)
  {
    delay(5); //Delay and check again to ensure debounce
    if (digitalRead(YellowSwitch2) == HIGH && lastYellowSwitch2 == LOW)
    {
        lastYellowSwitch2 = HIGH;
        puzzleCount = 0;
        Serial.println("You failed. Puzzle reset.");
    }
  }
  else
  {
    lastYellowSwitch2 = digitalRead(YellowSwitch2);
  }
}

I wonder if that could be done with some clean loops instead of copy paste for each button?