Light an LED using Analog Pins when a button is pushed

Building an elevator control simulator and the first block of code is to read the pushbuttons to see what floor the elevator is called to. If a button is pressed, light the corresponding LED so the passenger knows the button press was recognized...you know, otherwise they'd keep pressing over and over again! If no buttons are pressed, keep reading the pins until one is pressed.

I have other components coming later to control the motor and doors, so I need LOTS of digital pins. I read that I can use the Analog Pins as digital pins, so I thought they would be useful to light the LEDs and save the digital pins for other components.

To test the code, I only used two buttons and two LEDs, but the code will always light the LED connected to A1, even if a button is never pressed. Button 1 is connected to DP1, Button 2 is connected to DP2. Pressing button #2 does not light LED 2.

Any ideas?

THANKS!

MIKE

+++++++++++++++++++++++++

int DesiredFloor=0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

pinMode(A1, OUTPUT);//sets up analog pin to use as a digital pin and light an LED when triggered by button
pinMode(A2, OUTPUT);//sets up analog pin to use as a digital pin and light an LED when triggered by button
pinMode(A3, OUTPUT);//sets up analog pin to use as a digital pin and light an LED when triggered by button
pinMode(A4, OUTPUT);//sets up analog pin to use as a digital pin and light an LED when triggered by button

pinMode(1,INPUT); // Floor #1 pushbutton
pinMode(2,INPUT); // Floor #2 pushbutton
pinMode(3,INPUT); // Floor #3 pushbutton 
pinMode(4,INPUT); // Floor #4 pushbutton 

digitalWrite(1, LOW); //resets the call button to low so it can be read again
digitalWrite(2, LOW); //resets the call button to low so it can be read again
digitalWrite(3, LOW); //resets the call button to low so it can be read again
digitalWrite(4, LOW); //resets the call button to low so it can be read again

digitalWrite(A1, LOW); //resets the pin to LOW before execution
digitalWrite(A2, LOW); //resets the pin to LOW before execution
digitalWrite(A3, LOW); //resets the pin to LOW before execution
digitalWrite(A4, LOW); //resets the pin to LOW before execution

}

void loop() {
//put your main code here, to run repeatedly:

 if (digitalRead(1) == HIGH){DesiredFloor=1;
      digitalWrite(A1, HIGH);
     }
else if (digitalRead(2) == HIGH){DesiredFloor=2;
           digitalWrite(A2, HIGH);
     }

}

pinMode(1,INPUT); // Floor #1 pushbutton
pinMode(2,INPUT); // Floor #2 pushbutton
pinMode(3,INPUT); // Floor #3 pushbutton 
pinMode(4,INPUT); // Floor #4 pushbutton 

digitalWrite(1, LOW); //resets the call button to low so it can be read again
digitalWrite(2, LOW); //resets the call button to low so it can be read again
digitalWrite(3, LOW); //resets the call button to low so it can be read again
digitalWrite(4, LOW); //resets the call button to low so it can be read again

Why are you writing to input pins ?

You should look at switches for a change in state not the switch level.

Wire your switches as S3 is wired, look for a LOW for a switch press.

If you are using a Arduino Uno, then don't use pin 0 and 1. Those are the pins of the Serial port, you need those to send messages to the Serial Monitor, and to upload a sketch.

Hello mudhenmike

Don´t use magic Numbers.
Provide names for the port pins used.

Take some time and read how to use enum's, array's and data structures.
These instructions from the C++ toolbox will help you to program a compact and maintainable sketch to avoid code duplication. The use of C++ classes aren´t needed.

That's not how inputs work. They don't latch. They don't have to be 'reset'.

If you choose INPUT rather than INPUT_PULLUP you must provide an external pull-up or pull-down resistor. I recommend using INPUT_PULLUP and buttons between the pin and Ground. The pin will read HIGH when the button is open and LOW when the button is closed.

An elevator can have more than one floor selected at a time. Part of the fun is deciding which direction the elevator should go next based on the current direction, the selected floors, and the call buttons at each floor.

consider the following that can handle many buttons and leds

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

the "elevator algorithm" is used on disk drives to minimize sweeps of the arm across the platters, not sequentially as sectors are requested.

the arm move in one direction and reads all the sectors as it crosses them, only reversing direction after reading the farthest sector,
doing the same in the opposite direction

Many thanks to all who have jumped in to help, there are clearly some big brains on this forum!

I was able to make the code work with the original logic by adding the pull down resistor, but Larry's comment about shorting to ground has me spooked so I'll rework to seek the LOW condition and keep power off the pins.

THANKS!

Mike

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