Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#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
//////////////////////////
/////////////////////////////////
////////////////////////
enum elevator_state {
PARKED_0 = 0x10,
PARKED_1 = 0x11,
PARKED_2 = 0x12,
PARKED_3 = 0x13,
PARKED_4 = 0x14,
UP_TO_1 = 0x21,
UP_TO_2 = 0x22,
UP_TO_3 = 0x23,
UP_TO_4 = 0x24,
DOWN_TO_3 = 0x43,
DOWN_TO_2 = 0x42,
DOWN_TO_1 = 0x41,
DOWN_TO_0 = 0x40,
UNKNOWN = 0xff,
};
enum motor_state {
STOP = 0x00,
MOVE_UP = 0xc0,
MOVE_DOWN = 0x80,
};
// global variables for state-machine state and motor control
//
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;
#define MOTOR_ON PORTB.7
#define UP_NDOWN PORTB.6
for( ;; ) {
switch( state ) {
case PARKED_0:
if (BUTTON_1 | BUTTON_2 | BUTTON_3 | BUTTON_4) {
state = UP_TO_1;
motor = MOVE_UP;
}
else {
motor = STOP;
}
break;
case PARKED_1:
if (BUTTON_2 | BUTTON_3 | BUTTON_4) {
state = UP_TO_2;
motor = MOVE_UP;
}
else if (BUTTON_0) {
state = DOWN_TO_0;
motor = MOVE_DOWN;
}
else {
motor = STOP;
}
break;
case PARKED_2:
if (BUTTON_3 | BUTTON_4) {
state = UP_TO_3;
motor = MOVE_UP;
}
else if (BUTTON_0 | BUTTON_1) {
state = DOWN_TO_1;
motor = MOVE_DOWN;
}
else {
motor = STOP;
}
break;
case PARKED_3:
if (BUTTON_4) {
state = UP_TO_4;
motor = MOVE_UP;
}
else if (BUTTON_0 | BUTTON_1 | BUTTON_2 ) {
state = DOWN_TO_2;
motor = MOVE_DOWN;
}
else {
motor = STOP;
}
break;
case PARKED_4:
if (BUTTON_0 | BUTTON_1 | BUTTON_2 | BUTTON_3) {
state = DOWN_TO_3;
motor = MOVE_DOWN;
}
else {
motor = STOP;
}
break;
case UP_TO_1:
if (SENSOR_2 | SENSOR_3 | SENSOR_4) {
error();
}
if (BUTTON_2 | BUTTON_3 | BUTTON_4) {
state = UP_TO_2;
motor = MOVE_UP;
}
else if (SENSOR_1) {
state = PARKED_1;
motor = STOP;
}
else {
motor = MOVE_UP;
}
break;
case UP_TO_2:
if (BUTTON_3 | BUTTON_4) {
state = UP_TO_3;
motor = MOVE_UP;
}
else if (SENSOR_2) {
state = PARKED_2;
motor = STOP;
}
else {
motor = MOVE_UP;
}
break;
case UP_TO_3:
if (BUTTON_4) {
state = UP_TO_4;
motor = MOVE_UP;
}
else if (SENSOR_3) {
state = PARKED_3;
motor = STOP;
}
else {
motor = MOVE_UP;
}
break;
case UP_TO_4:
if (SENSOR_4) {
state = PARKED_4;
motor = STOP;
}
else {
motor = MOVE_UP;
}
break;
case DOWN_TO_3:
if (BUTTON_2 | BUTTON_1 | BUTTON_0) {
state = DOWN_TO_2;
motor = MOVE_DOWN;
}
else if (SENSOR_3) {
state = PARKED_3;
motor = STOP;
}
else {
motor = MOVE_DOWN;
}
break;
case DOWN_TO_2:
if (BUTTON_1 | BUTTON_0) {
state = DOWN_TO_1;
motor = MOVE_DOWN;
}
else if (SENSOR_2) {
state = PARKED_2;
motor = STOP;
}
else {
motor = MOVE_DOWN;
}
break;
case DOWN_TO_1:
if (BUTTON_0) {
state = DOWN_TO_0;
motor = MOVE_DOWN;
}
else if (SENSOR_1) {
state = PARKED_1;
motor = STOP;
}
else {
motor = MOVE_DOWN;
}
break;
case DOWN_TO_0:
if (SENSOR_0) {
state = PARKED_0;
motor = STOP;
}
else {
motor = MOVE_DOWN;
}
break;
default:
error();
} // end switch(state)
PORTB.5 = 1;
PORTB.5 = 0;
PORTB = motor;
}
}
Hi,
why a shift register? Why not a simple flip flop?
I can´t see how a shift register can be useful.
Klaus
Hi,
Is there any alternative?
Klaus
Do you see any other solution?Is that mean that i should use flip flop for each floor?
Brad,
you get on a floor 1 and want to go to floor 10
call comes in on floor 5 up
while passing floor 2 a call comes in from floor 4 up
shift register design would pass floor 4 to pick up floor 5
floor 4 would have to wait until the elevator is going back up to pick up floor 4.
.