I am having problems with making the code for an elevator i built, it has 4 floors and the circuit has an H bridge and 4 buttons.
const int button1 = 9;
const int button2 = 10;
const int button3 = 11;
const int button4 = 12;
const int B2A = 3;
const int B2B = 5;
const int halleffect1 = 4;
const int halleffect2 = 6;
const int halleffect3 = 7;
const int halleffect4 = 8;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(halleffect1, INPUT);
pinMode(halleffect2, INPUT);
pinMode(halleffect3, INPUT);
pinMode(halleffect4, INPUT);
pinMode(B2A, OUTPUT);
pinMode(B2B, OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalRead(halleffect1);
digitalRead(halleffect2);
digitalRead(halleffect3);
digitalRead(halleffect4);
if (digitalRead(button1) == HIGH)
{
floor1function();
}
else if (digitalRead(button2) == HIGH)
{
floor2function();
}
else if (digitalRead(button3) == HIGH)
{
floor3function();
}
else if (digitalRead(button4) == HIGH)
{
floor4function();
}
digitalWrite(B2A, LOW);
digitalWrite(B2B, LOW);
}
void floor1function()
{
if (digitalRead(halleffect1) != LOW)
while (digitalRead(halleffect1) == HIGH)
{
motordown();
}
}
void floor2function()
{
if (digitalRead(halleffect3) == LOW || digitalRead(halleffect4) == LOW)
{
while (digitalRead(halleffect2) == HIGH)
{
motordown();
}
}
else if (digitalRead(halleffect1) == LOW)
{
while (digitalRead(halleffect2) == HIGH)
{
motorup();
}
}
}
void floor3function()
{
if (digitalRead(halleffect1) == LOW || digitalRead(halleffect2) == LOW)
{
while (digitalRead(halleffect3) == HIGH)
{
motorup();
}
}
else if (digitalRead(halleffect4) == LOW)
{
while (digitalRead(halleffect3) == HIGH)
{
motordown();
}
}
}
void floor4function()
{
if (digitalRead(halleffect4) != LOW)
{
while (digitalRead(halleffect4) == HIGH)
{
motorup();
}
}
}
void motorup()
{
digitalWrite(B2A, HIGH);
digitalWrite(B2B, LOW);
Serial.println("Going Up");
}
void motordown()
{
digitalWrite(B2A, HIGH);
digitalWrite(B2B, HIGH);
Serial.println("Going Down");
}