elevator programming with atmega16

Thread Starter

mah

Joined Mar 15, 2010
393
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();
      }
    } 
    }
 

Art

Joined Sep 10, 2007
806
You might be wanting to use a couple of parallel eight element arrays to store button pushes for up/down.
What if it's on the eighth floor and an up or down button for every other floor is pushed before it's moved?
On the way down you could check each floor of the down array and stop if the bit for that floor is set.
On the way up, you could check each floor of the up array.
You would then clear each bit for up or down as the elevator doors have open & closed on that floor,
and that is the time to also turn off the light behind the button that was pushed.
 

Thread Starter

mah

Joined Mar 15, 2010
393
You might be wanting to use a couple of parallel eight element arrays to store button pushes for up/down.
excellent idea and perfect explanation , but why should it be couple of parallel , Don't 1 array of eight elements (for 8 floors) be enough?
 

Thread Starter

mah

Joined Mar 15, 2010
393
i tried this code in code vision

Code:
#include <mega16.h>


/* define bit buffer array type*/
typedef struct {
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
} bits_t;

/* Set Bit Function */
void SetBit(bits_t* array, unsigned char loc, bit val)
{
switch(loc) /* loc has values in between 0 and 7 */
{
case 0: array->b0 = val; break;
case 1: array->b1 = val; break;
case 2: array->b2 = val; break;
case 3: array->b3 = val; break;
case 4: array->b4 = val; break;
case 5: array->b5 = val; break;
case 6: array->b6 = val; break;
case 7: array->b7 = val; break;
}
}

/* Get Bit Function */
bit GetBit(bits_t* array, unsigned char loc)
{
bit result;

switch(loc) /* loc has values in between 0 and 7 */
{
case 0: result = array->b0; break;
case 1: result = array->b1; break;
case 2: result = array->b2; break;
case 3: result = array->b3; break;
case 4: result = array->b4; break;
case 5: result = array->b5; break;
case 6: result = array->b6; break;
case 7: result = array->b7; break;
}

return result;
}

/* Main */
void main(void)
{
unsigned char i;

bits_t bit_array[10]; // Declare 10 bytes long bit array

SetBit(&bit_array[1], 3, 1); // Set bit3 of bit_array[1]

if(GetBit(&bit_array[1], 3)) // Check bit3 of bit_array[1]
SetBit(&bit_array[1], 0, 1); // Set bit0 of bit_array[1]

for(i=0;i<8;i++) // Use for loop to access the bits
SetBit(&bit_array[0], i, 1);

while(1)
{}
but it give error , however every thing is ok

Error: C:\Users\madoo\Music\8051\new.c(18): 'bit' type not allowed in this context " referred bit val in Setbit function argu"

Moderator edit : Please use code tags for pieces of code
 
Last edited by a moderator:

ErnieM

Joined Apr 24, 2011
8,377
excellent idea and perfect explanation , but why should it be couple of parallel , Don't 1 array of eight elements (for 8 floors) be enough?
Nope. When the car is going down you don't want to stop on floors that have only requested they want to go up.

Since there are two directions you need two arrays.
 

Art

Joined Sep 10, 2007
806
If they are byte arrays you could store a value for up, another value for down, and a third for an idiot who pushed both (and you might have to stop there again in the other direction), and a value for nobody waiting on that floor.
If it's a bit array you need a minimum of two arrays because if Up for floor G = 0 and down for floor G also = 0, you have a state for taking no action.
If Up for floor G = 1 and so does Down, you have a state for stopping there the next two passes.
 

djsfantasi

Joined Apr 11, 2010
9,163
If it's a bit array, one could index each floor by two and use odd entries for up and even entries for down. That us if f is the floor number, call up is indexed by i=(f-1)*2 and call down is indexed by i=(f-1)*2+1. This is a way of simulating a two dimensional array.
 

Thread Starter

mah

Joined Mar 15, 2010
393
i tried to use bit array inside sates but i failed i don't know how to use them. when some body call elevator to 4th floor it will go up before passing floor 1 somebody pressed button one it should stop then complete its way to 4th . i couldn't do that inside states . do you have any idea?, also what would happen if it passed floor 1 then button 1 pressed , the elevator should go up and store button 1 to return to it while going down . in which state will this be implemented ? or i should write it outside switch cases
 

djsfantasi

Joined Apr 11, 2010
9,163
What are your inputs? For example, I press the up button on floor 2. That's an input. What other inputs or state variables are there? I wouldn't start any code until I knew what all the inputs, outputs and states are! List them out, one per line.

And what do you mean by inside states?
 

Thread Starter

mah

Joined Mar 15, 2010
393
inputs and outputs for 5 floors

inputs:

the inside buttons are connected in parallel with the outside call button just one button for up and down so i show just the inside buttons

1- BUTTON_0 //push button for first floor inside the elevator
2-BUTTON_1
3-BUTTON_2
4-BUTTON_3
5-BUTTON_4 //push button for 5th floor

6- SENSOR_0 // proximity sensor in the first floor that will sense the elevator existence
7-SENSOR_1
8-SENSOR_2
9-SENSOR_3
10-SENSOR_4

outputs:
1-go up( for the motor control)
2- go down
3- fast (for the motor working with up or down) when it is far from requested floor
4-slow (for the motor working with up or down) when reached
5-seven segment display floor number
6 - up and down arrow
7- lock for the door


the algorithm and input and outputs ,every thing is here except the code

this is the basic requirements for the elevator then i will update it if needed.



here is the process
1-the elevator is at ground floor as a default then somebody calls it from nth floor during it's travel to up somebody in n-2 and n-3 called it so the elevator should go up to (n-3) floor then to (n-2) floor then to nth floor.

2- there will be seven segment and arrows to display its state and floor number ( i will hold this step now)

3- during going up somebody called the elevator to go down , but the elevator will complete it's first orders then stop and go down.

the same time that happens when going up will happen when going down if somebody in between top floor and bottom pressed a button it should stop and take him

4-if two buttons pressed at the same time go to the first one the elevator way (up or down)

5- when there are no requests the elevator is waiting at the last called floor and its door is opened .

6-when it is moving the doors should be closed and make sure it isn't opened . when the elevator stopped at the called floor don't move if the door is opened
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,163
What do you mean when you say the inside buttons are connected in parallel with the outside call buttons? They are different functions.

By the way, how much research have you done? There are at least seven pages listing "elevator" topics on this site alone?
 

ErnieM

Joined Apr 24, 2011
8,377
I still don't see the inputs and outputs this requires. Each floor needs an up and down call (with 2 obvious exceptions), inside you need a button for each floor, and possibly some other functions one day. Then there is the motor control for up and down, the door open/close, floor detect switches, door open switches, people in door switches....

Plus there are issues if you are simulating this or building something the goes up and down.

Now if you expect someone to write code to your spec... I suspect your wait will be much shortened if you cross post in the Flea market section and request consultants.

Ain't no one gonna write your code for free.
 

Thread Starter

mah

Joined Mar 15, 2010
393
What do you mean when you say the inside buttons are connected in parallel with the outside call buttons? They are different functions.

By the way, how much research have you done? There are at least seven pages listing "elevator" topics on this site alone?
i want to exploit inputs pins on mcu so i will connect the first floor button inside the elevator with the outside button to make OR gate (in parallel) if one of them pressed the elevator will go to the 1st floor no matter it is in the 4th or ground floor . it will be programmed to go up or down related to its current position and compared with the requested floor.

and please send link for those seven pages
 

Thread Starter

mah

Joined Mar 15, 2010
393
Go to the Search page, type "elevator", and click search.
do you think that i didn't do that and i am waiting for your your advice ! i did that and there is a little data to make use of. if this is your help why did say " I wouldn't start any code until I knew what all the inputs, outputs and states are! List them out, one per line."
 

djsfantasi

Joined Apr 11, 2010
9,163
First, your "parallel" buttons are an addition to the requirements in your original post (and IMHO I've never seen an elevator work this way). Can't help if the requirements change.

Second, I feel you never successfully listed the inputs, outputs and states. I didn't say anything, because ErnieM did so eloquently. I expected an updated post from you, but became side tracked trying to clarify your "parallel" buttons.

Third, if you could not find anything useful in the past posts on elevators, I'm not sure I can provide much more.

Fourth, As ErnieM stated no one is going to write code for you. We can help if you're willing to listen. But I didn't see you responding to the very complete advice you've received thus far.

Sorry if this is frustrating, but TANSTAAFL
 

ErnieM

Joined Apr 24, 2011
8,377
i want to exploit inputs pins on mcu so i will connect the first floor button inside the elevator with the outside button to make OR gate (in parallel) if one of them pressed the elevator will go to the 1st floor no matter it is in the 4th or ground floor .
That would not work. Those call buttons are quite different things, though they may set the same states in the same state vector.

A floor has two buttons, one for "wanna go up" and another for "wanna go down."

The car has a button for each floor. An arbitrary floor generally may be either above or below the car, so pressing a button for a floor may be either a "wanna go up" or "wanna go down" request.
 

Thread Starter

mah

Joined Mar 15, 2010
393
ok this is the modification
inputs and outputs for 5 floors

inputs:

inside the car

1- BUTTON_0 //push button for first floor inside the elevator
2-BUTTON_1
3-BUTTON_2
4-BUTTON_3
5-BUTTON_4 //push button for 5th floor

up and down calls outside the car :

1-down for 1st floor
2-up and down for 2-3-4 floors
3-up for 5th floor

distance sensors:

1- SENSOR_0 // proximity sensor in the first floor that will sense the elevator existence
2-SENSOR_1
3-SENSOR_2
4-SENSOR_3
5-SENSOR_4

outputs:

1-go up( for the motor control)
2- go down
3- fast (for the motor working with up or down) when it is far from requested floor
4-slow (for the motor working with up or down) when reached
5-seven segment display floor number
6 - up and down arrow
7- lock for the door


now every thing is clear so please give help in storing requests while moving
 

djsfantasi

Joined Apr 11, 2010
9,163
The data structure has been discussed in posts 2,3,5&7. Are you asking how or when to modify them?

You have listed your inputs. How do these inputs affect your state variables? For example I am on floor 2 and press the up button. Which element of which array (if any) will change? Or I'm in the elevator and press the 3rd floor button?

By the way, are there any other variables you might need? Like what the elevator is doing?
 
Top