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);
}
}