i want to do elevator with eight floors but i started with 4 for now. the problem which i faced when i call the elevator from 3rd or 4th floor to the first floor . i need to stop it if somebody presses the button in 2nd floor and also upward the same . this is the code using state machines . if somebody have idea to help please introduce it .
Code:
#include <mega16.h>
#include <delay.h>
#define BUTTON_0 PINC.0
#define BUTTON_1 PINC.1
#define BUTTON_2 PINC.2
#define BUTTON_3 PINC.3
#define BUTTON_4 PINC.4
#define SENSOR_0 PIND.0
#define SENSOR_1 PIND.1
#define SENSOR_2 PIND.2
#define SENSOR_3 PIND.3
#define SENSOR_4 PIND.4
//////////////////////////
#define MOVE_UP PORTB.1
#define MOVE_DOWN PORTB.2
/////////////////////////////////
enum elevator_state {
PARKED_0,PARKED_1,PARKED_2 ,PARKED_3 , PARKED_4 ,
UP_TO_1 , UP_TO_2 , UP_TO_3 , UP_TO_4 ,DOWN_TO_3, DOWN_TO_2,
DOWN_TO_1, DOWN_TO_0
};
////////////////////////
enum motor_direction {
STOP = 0, // send 0 to portb to stop the elevator
};
////////////////////////////
unsigned char state = PARKED_0;
unsigned char motor=STOP;
////////////////////////////
void error(void)
{
motor = STOP;
for(;;) {
PORTB.5 = 1;
}
}
////////////////////////////////
void main(void)
{
PORTB=0x00;DDRB=0xFF;
PORTC=0x00;DDRC=0x00;
PORTD=0x00;DDRD=0x00;
for( ;; ) {
switch( state ) {
case PARKED_0:
if (BUTTON_1 | BUTTON_2 | BUTTON_3 | BUTTON_4) {
state = UP_TO_1;
PORTB.1=1;
}
else{
PORTB.1=0; //clear up
}
break;
case PARKED_1:
if (BUTTON_2 | BUTTON_3 | BUTTON_4) {
state = UP_TO_2;
PORTB.1=1; // motor up
}
else if (BUTTON_0) {
state = DOWN_TO_0;
PORTB.2=1; // /down
}
else{
PORTB.2=0; //stop the motor
}
break;
case PARKED_2:
if (BUTTON_3 | BUTTON_4) {
state = UP_TO_3;
PORTB.1=1;
}
else if (BUTTON_0 | BUTTON_1) {
state = DOWN_TO_1;
PORTB.2=1; // /down
}
break;
////////////////////////////////////////
case PARKED_3:
if (BUTTON_4) {
state = UP_TO_4;
PORTB.1=1;
}
else if (BUTTON_0 | BUTTON_1 | BUTTON_2 ) {
state = DOWN_TO_2;
PORTB.2=1; // /down
}
break;
case PARKED_4:
if (BUTTON_0 | BUTTON_1 | BUTTON_2 | BUTTON_3) {
PORTB.2=1; // down
}
break;
case UP_TO_1:
if (SENSOR_2 | SENSOR_3 | SENSOR_4) {
error();
}
else if (SENSOR_1) {
state = PARKED_1;
PORTB.1=0;
}
else {
PORTB.1=1;
}
break;
case UP_TO_2:
if (SENSOR_2) {
state = PARKED_2;
PORTB.1=0;
}
else {
motor = MOVE_UP;
}
break;
case UP_TO_3:
if (SENSOR_3) {
state = PARKED_3;
PORTB.1=0; //clear up
}
else {
PORTB.1=1;
}
break;
case UP_TO_4:
if (SENSOR_4) {
state = PARKED_4;
PORTB.1=0;
}
else {
motor = MOVE_UP;
}
break;
case DOWN_TO_0:
if (SENSOR_0) {
state = PARKED_0;
PORTB.2=0; //stop the motor
}
else {
PORTB.2=1; // /down
}
case DOWN_TO_1:
if (SENSOR_1) {
state = PARKED_1;
PORTB.2=0; //stop the motor
}
else {
PORTB.2=1; // /down
}
case DOWN_TO_2:
if (SENSOR_2) {
state = PARKED_2;
PORTB.2=0; //stop the motor
}
else {
PORTB.2=1; // /down
PORTB.1=0; // clear up
}
case DOWN_TO_3:
if (SENSOR_3) {
state = PARKED_3;
PORTB.2=0; //stop the motor
}
else {
PORTB.2=1; // /down
PORTB.1=0; // clear up
}
break;
default:
error();
}
}
}