Sie sind auf Seite 1von 3

const

const
const
const
const
const
const
const
const

int
int
int
int
int
int
int
int
int

floorSwitch2 = 10;
floorSwitch3 = 11;
floorSwitch1 = 12;
controlPin1 = 2; // connected to pin 7 on the H-bridge
controlPin2 = 3; // connected to pin 2 on the H-bridge
enablePin = 9;
// connected to pin 1 on the H-bridge
floor1 = 4; // connected to the switch for first floor
floor2 = 5; // connected to the switch for second floor
floor3 = 6; // connected to the switch for the third floor

int f1 = 0;
int f2 = 0;
int f3 = 0;

// floor 1 boolean checker


// floor 2 boolean checker
// floor 3 boolean checker

int fs1 = 0;
int fs2 = 0;
int fs3 = 0;
void setup()
{
// intialize the inputs and outputs
pinMode(floor1, INPUT);
pinMode(floor2, INPUT);
pinMode(floor3, INPUT);
pinMode(floorSwitch1, INPUT);
pinMode(floorSwitch2, INPUT);
pinMode(floorSwitch3, INPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW); // sets the motor to off
Serial.begin(9600); // for debugging
}
void loop()
{
f1 = digitalRead(floor1); // reads if button for floor 1 is pressed
f2 = digitalRead(floor2); // reads if button for floor 2 is pressed
f3 = digitalRead(floor3); // reads if button for floor 3 is pressed
fs1 = digitalRead(floorSwitch1);
fs2 = digitalRead(floorSwitch2);
fs3 = digitalRead(floorSwitch3);
Serial.print("Floor 1: ");
Serial.println(fs1);
Serial.print("Floor 2: ");
Serial.println(fs2);
Serial.print("Floor 3: ");

Serial.println(fs3);
if (fs1 == 1 ) // while the elevator is at the first floor
{
if (f2 == 1) // when the button for second floor is pressed
{
while (fs2 == 0)
// this runs the code until the elevator reaches the second floor
{
floorUp(); // calls the function to move the elevator upward
fs2 = digitalRead(floorSwitch2);
}
analogWrite(enablePin, 0); // turns of the motor when the second floor is reached
}
if (f3 == 1) // when the button for the third floor is pressed
{
while (fs3 == 0) // this runs the code until the elevator reaches the third floor
{
floorUp(); // calls the function to move the elevator upward
fs3 = digitalRead(floorSwitch3);
}
analogWrite(enablePin, 0); // turns of the motor when the third floor is reached
}
}
else if (fs2 == 1) // while the elevator is at the second floor
{
if (f1 == 1) // when the button for the first floor is pressed
{
while (fs1 == 0) // this runs the code until the elevator reaches the first floor
{
floorDown(); // calls the function to move the elevator downward
fs1 = digitalRead(floorSwitch1);
}
analogWrite(enablePin, 0); // turns off the motor when the first floor is reached
}
if (f3 == 1) // when the button for the third floor is pressed
{
while (fs3 == 0) // this runs the code until the elevator reaches the third floor
{
floorUp(); // calls the function to move the elevator upward
fs3 = digitalRead(floorSwitch3);
}
analogWrite(enablePin, 0); // turns off the motor when the third floor is reached
}

}
else if (fs3 == 1) // while the elevator is at the third floor
{
if (f1 == 1) // when the button for the first floor is pressed
{
while (fs1 == 0) // this runs the code until the elevator reaches the first floor
{
floorDown(); // calls the function to move the elevator downward
fs1 = digitalRead(floorSwitch1);
}
analogWrite(enablePin, 0); // turns off the motor when the first floor is reached
}
else if (f2 == 1) // when the button for the second floor is pressed
{
while (fs2 == 0) // this runs the code until the elevator reaches the second floor
{
floorDown(); // calls the function to move the elevator downward
fs2 = digitalRead(floorSwitch2);
}
analogWrite(enablePin, 0); // turns off the motor when the first floor is reached
}
}
}
void floorDown() // class to move the elevator downward
{
analogWrite(enablePin, 255); // turns on the motor
digitalWrite(controlPin1, LOW);
// controls the direction
digitalWrite(controlPin2, HIGH); // controls the direction
}
void floorUp() // class to move the elevtor upward
{
analogWrite(enablePin, 255); // turns on the motor
digitalWrite(controlPin1, HIGH); // controls the direction
digitalWrite(controlPin2, LOW); // controls the direction
}

Das könnte Ihnen auch gefallen