Maker Pro
Maker Pro

Arduino - trying to change a variable at one point in a program to be used by another part

Robert Hill

Mar 5, 2015
112
Joined
Mar 5, 2015
Messages
112
Hi there,

I'm trying to make a circuit and arduino program to simulate a lift. I've got 4 LEDs which represent the 4 floors and 4 push buttons which represent the lift call buttons on each floor. Below is my Arduino program which is a work in progress.

So far i've coded enough to make the LEDs turn on in order from the 1st to the 4th when the 4th push button is pressed.

I've then started to code something for the button representing the 2nd floor. I want this section of code to be able to tell if the 'lift' has gone up to the 4th floor or if it is still on the 1st floor. I've tried to define a variable 'currentfloor' at the start of the loop section, then tried to modify it at the end of the code for the 4th floor button. I then try to get the 2nd floor button code to try and read this variable in order to determine whether it should make the LEDs go on and off from the 4th, to the 3rd and then the 2nd.

However, it doesn't seem to work as the code for the 2nd floor button operates even if I haven't pressed the 4th floor button. I think I need a little more help on defining, modifying and using variables if anyone is able to help me?

Thanks in advance,
Code Below:

const int led1 = 13;
const int led2 = 12;
const int led3 = 11;
const int led4 = 10;// Set up Pins wired up to LEDs to represent floors
const int button1 = 7;
const int button2 = 6;
const int button3 = 5;
const int button4 = 4;//Set up pushbuttons to represent lift call buttons on each floor
const int numfloor = 4;
const int floorarray [numfloor] = {led1, led2, led3, led4}; // Set up Array to represent the possible number of floors and enable them to be cycled through
const int timer = 1000;

void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
digitalWrite(button1, HIGH); // turn on pullup resistor
digitalWrite(button2, HIGH); // turn on pullup resistor
digitalWrite(button3, HIGH); // turn on pullup resistor
digitalWrite(button4, HIGH); // turn on pullup resistor

for (int i=0; i< numfloor; i++){
pinMode (floorarray, OUTPUT);}
}

void loop()
{int currentfloor = 0;// Sets up an integer to be modified by the following 'if' statements. This aim of this integer is to keep track of which 'floor' the lift has been summoned to.
if (digitalRead (button4) == LOW){ // This IF statement represents someone pressing the buttton on the fourth floor and causes the LEDs to cycle from 1 up to 4 to simulate the lift moving from the 1st to 4th floor.
for (int i= 0; i < numfloor; i++){
digitalWrite (floorarray, HIGH);
delay (timer);
digitalWrite (floorarray, LOW);
currentfloor = 4; // This is supposed to set the variable currentfloor to 4 so the rest of the programme knows the 'lift' is on the 4th floor so it knows whether to go up or down to the floor whose button has just been pressed.
}}

if (digitalRead (button2) == LOW)// If the button representing the lift being clled to the 2nd floor has been pushed
{
if (currentfloor > 2); // And the current floor the lift is at is greater than 2
{for(int i= numfloor -1; i>= 1; i--){// light the LEDs up in order until it reaches the 2nd floor.
digitalWrite (floorarray, HIGH);
delay (timer);
digitalWrite (floorarray, LOW);
currentfloor = 2;
}}}}
 

Robert Hill

Mar 5, 2015
112
Joined
Mar 5, 2015
Messages
112
Aha, i've solved it.
New code below if anyone is interested!

Now the pushbutton for floor 2 only works after the button for floor 4 has been pushed.

const int led1 = 13;
const int led2 = 12;
const int led3 = 11;
const int led4 = 10;
const int button1 = 7;
const int button2 = 6;
const int button3 = 5;
const int button4 = 4;
const int numfloor = 4;
const int floorarray [numfloor] = {led1, led2, led3, led4};
const int timer = 1000;
int currentfloor = 0;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
digitalWrite(button1, HIGH); // turn on pullup resistor
digitalWrite(button2, HIGH); // turn on pullup resistor
digitalWrite(button3, HIGH); // turn on pullup resistor
digitalWrite(button4, HIGH); // turn on pullup resistor

for (int i=0; i< numfloor; i++){
pinMode (floorarray, OUTPUT);}
}

void loop()
{
if (digitalRead (button4) == LOW){
for (int i= 0; i < numfloor; i++){
digitalWrite (floorarray, HIGH);
delay (timer);
digitalWrite (floorarray, LOW);
currentfloor = 4;
}}

if (digitalRead (button2) == LOW && currentfloor == 4) {
{for(int i= numfloor -1; i>= 1; i--){
digitalWrite (floorarray, HIGH);
delay (timer);
digitalWrite (floorarray, LOW);
currentfloor = 2;
}}}}
 
Top