#include <TimerOne.h>
#define STOP_STATE 0
#define UP_STATE 1
#define DOWN_STATE 2
char state;
char floor_button[5] = {2,3,4,5,6};
char floor_light[5] = {12,11,10,9,8};
char index,current_floor,target_floor;
void setup() {
// put your setup code here, to run once:
for (index=0;index<5;index++){
pinMode(floor_button[index], INPUT_PULLUP);
pinMode(floor_light[index], OUTPUT);
}
state = DOWN_STATE;
current_floor = 0;
target_floor = 0;
Timer1.initialize(1000000);
Timer1.attachInterrupt(onesecond);
}
void loop() {
// put your main code here, to run repeatedly:
switch(state){
case STOP_STATE:
for (index=0;index<5;index++){
if (digitalRead(floor_button[index])==0){
target_floor = index;
digitalWrite(floor_light, HIGH);
break;
}
}
if (current_floor<target_floor){
state = UP_STATE;
Timer1.start();
}
else if (current_floor>target_floor){
state = DOWN_STATE;
Timer1.start();
}
break;
case UP_STATE: if (current_floor == target_floor){
state = STOP_STATE;
Timer1.stop();
break;
}
case DOWN_STATE: if (current_floor == target_floor){
state = STOP_STATE;
Timer1.stop();
break;
}
}
for (index=0;index<5;index++){
if (index == current_floor)
digitalWrite(floor_light[index], HIGH);
else digitalWrite(floor_light[index], LOW);
}
}
void onesecond(){
switch(state){
case STOP_STATE:
break;
case UP_STATE:current_floor++ ;
break;
case DOWN_STATE:current_floor--;
break;
}
}