elevator programming with atmega16

Discussion in 'Embedded Systems and Microcontrollers' started by mah, Nov 12, 2014.

  1. mah

    Thread Starter Active Member

    Mar 15, 2010
    276
    2
    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 (Text):
    1. #include <mega16.h>
    2. #include <delay.h>
    3. #define BUTTON_0  PINC.0
    4. #define BUTTON_1  PINC.1
    5. #define BUTTON_2  PINC.2
    6. #define BUTTON_3  PINC.3
    7. #define BUTTON_4  PINC.4
    8.  
    9. #define SENSOR_0  PIND.0
    10. #define SENSOR_1  PIND.1
    11. #define SENSOR_2  PIND.2
    12. #define SENSOR_3  PIND.3
    13. #define SENSOR_4  PIND.4
    14. //////////////////////////
    15. #define MOVE_UP    PORTB.1
    16. #define MOVE_DOWN  PORTB.2
    17. /////////////////////////////////
    18. enum elevator_state {
    19.   PARKED_0,PARKED_1,PARKED_2 ,PARKED_3 , PARKED_4  ,
    20.   UP_TO_1   , UP_TO_2  , UP_TO_3   , UP_TO_4  ,DOWN_TO_3, DOWN_TO_2,
    21.    DOWN_TO_1, DOWN_TO_0
    22.   };
    23. ////////////////////////
    24. enum motor_direction {
    25.   STOP      = 0,   // send 0 to portb to stop the elevator
    26. };
    27.  
    28. ////////////////////////////
    29. unsigned char state = PARKED_0;
    30. unsigned char  motor=STOP;
    31. ////////////////////////////
    32. void error(void)
    33. {
    34.   motor = STOP;
    35.   for(;;) {
    36.       PORTB.5 = 1;
    37.   }  
    38. }
    39. ////////////////////////////////
    40.  
    41. void main(void)
    42. {
    43. PORTB=0x00;DDRB=0xFF;
    44. PORTC=0x00;DDRC=0x00;
    45. PORTD=0x00;DDRD=0x00;
    46. for( ;; ) {
    47.      
    48.       switch( state ) {
    49.         case PARKED_0:
    50.           if (BUTTON_1 | BUTTON_2 | BUTTON_3 | BUTTON_4) {
    51.             state = UP_TO_1;
    52.    PORTB.1=1;
    53.           }
    54.          else{
    55.         PORTB.1=0;      //clear up
    56.          }
    57.           break;
    58.          
    59.       case PARKED_1:    
    60.         if (BUTTON_2 | BUTTON_3 | BUTTON_4) {
    61.           state = UP_TO_2;
    62.           PORTB.1=1;  // motor up
    63.         }
    64.        
    65.         else if (BUTTON_0) {
    66.           state = DOWN_TO_0;
    67.            PORTB.2=1;     //  /down
    68.         }
    69.         else{
    70.                    PORTB.2=0;      //stop the motor
    71.             }
    72.      
    73.                      
    74.           break;
    75.  
    76.       case PARKED_2:    
    77.         if (BUTTON_3 | BUTTON_4) {
    78.           state = UP_TO_3;
    79.            PORTB.1=1;
    80.         }
    81.        
    82.          else if (BUTTON_0 | BUTTON_1) {
    83.           state = DOWN_TO_1;
    84.          PORTB.2=1;     //  /down
    85.           }          
    86.           break;
    87. ////////////////////////////////////////
    88.       case PARKED_3:    
    89.         if (BUTTON_4) {
    90.           state = UP_TO_4;
    91.          PORTB.1=1;
    92.         }
    93.         else if (BUTTON_0 | BUTTON_1 | BUTTON_2 ) {
    94.           state = DOWN_TO_2;
    95.            PORTB.2=1;     //  /down
    96.         }
    97.                    
    98.           break;
    99.                
    100.       case PARKED_4:    
    101.         if (BUTTON_0 | BUTTON_1 | BUTTON_2 | BUTTON_3) {
    102.          PORTB.2=1;      // down
    103.         }
    104.                      
    105.           break;
    106.          
    107.         case UP_TO_1:
    108.           if (SENSOR_2 | SENSOR_3 | SENSOR_4) {
    109.             error();
    110.           }
    111.          
    112.           else if (SENSOR_1) {
    113.             state = PARKED_1;
    114.         PORTB.1=0;
    115.           }
    116.           else {
    117.              PORTB.1=1;
    118.           }
    119.           break;
    120.  
    121.         case UP_TO_2:
    122.        
    123.            if (SENSOR_2) {
    124.             state = PARKED_2;
    125.           PORTB.1=0;
    126.           }
    127.           else {
    128.             motor = MOVE_UP;
    129.           }
    130.           break;
    131.  
    132.         case UP_TO_3:
    133.         if (SENSOR_3) {
    134.             state = PARKED_3;
    135.        PORTB.1=0;      //clear up
    136.           }
    137.           else {
    138.        PORTB.1=1;
    139.           }
    140.           break;
    141.  
    142.         case UP_TO_4:
    143.           if (SENSOR_4) {
    144.             state = PARKED_4;
    145. PORTB.1=0;
    146.           }
    147.           else {
    148.             motor = MOVE_UP;
    149.           }
    150.           break;
    151.          
    152.      case DOWN_TO_0:      
    153.  
    154.           if (SENSOR_0) {
    155.             state = PARKED_0;
    156.              PORTB.2=0;      //stop the motor
    157.           }
    158.           else {
    159.              PORTB.2=1;     //  /down
    160.             }
    161.            
    162.       case DOWN_TO_1:      
    163.  
    164.           if (SENSOR_1) {
    165.             state = PARKED_1;
    166.              PORTB.2=0;      //stop the motor
    167.           }
    168.           else {
    169.              PORTB.2=1;     //  /down
    170.             }
    171.      case DOWN_TO_2:      
    172.  
    173.           if (SENSOR_2) {
    174.             state = PARKED_2;
    175.              PORTB.2=0;      //stop the motor
    176.           }
    177.           else {
    178.              PORTB.2=1;     //  /down
    179.              PORTB.1=0;     //  clear up
    180.  
    181.             }
    182.            
    183.      case DOWN_TO_3:      
    184.  
    185.           if (SENSOR_3) {
    186.             state = PARKED_3;
    187.              PORTB.2=0;      //stop the motor
    188.           }
    189.           else {
    190.              PORTB.2=1;     //  /down
    191.              PORTB.1=0;     //  clear up
    192.  
    193.             }
    194.             break;
    195.          
    196.       default:
    197.         error();
    198.       }
    199.     }
    200.     }
    201.      
    202.  
     
  2. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    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.
     
    mah likes this.
  3. mah

    Thread Starter Active Member

    Mar 15, 2010
    276
    2
    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?
     
  4. mah

    Thread Starter Active Member

    Mar 15, 2010
    276
    2
    i tried this code in code vision

    Code (Text):
    1.  
    2. #include <mega16.h>
    3.  
    4.  
    5. /* define bit buffer array type*/
    6. typedef struct {
    7. unsigned char b0:1;
    8. unsigned char b1:1;
    9. unsigned char b2:1;
    10. unsigned char b3:1;
    11. unsigned char b4:1;
    12. unsigned char b5:1;
    13. unsigned char b6:1;
    14. unsigned char b7:1;
    15. } bits_t;
    16.  
    17. /* Set Bit Function */
    18. void SetBit(bits_t* array, unsigned char loc, bit val)
    19. {
    20. switch(loc) /* loc has values in between 0 and 7 */
    21. {
    22. case 0: array->b0 = val; break;
    23. case 1: array->b1 = val; break;
    24. case 2: array->b2 = val; break;
    25. case 3: array->b3 = val; break;
    26. case 4: array->b4 = val; break;
    27. case 5: array->b5 = val; break;
    28. case 6: array->b6 = val; break;
    29. case 7: array->b7 = val; break;
    30. }
    31. }
    32.  
    33. /* Get Bit Function */
    34. bit GetBit(bits_t* array, unsigned char loc)
    35. {
    36. bit result;
    37.  
    38. switch(loc) /* loc has values in between 0 and 7 */
    39. {
    40. case 0: result = array->b0; break;
    41. case 1: result = array->b1; break;
    42. case 2: result = array->b2; break;
    43. case 3: result = array->b3; break;
    44. case 4: result = array->b4; break;
    45. case 5: result = array->b5; break;
    46. case 6: result = array->b6; break;
    47. case 7: result = array->b7; break;
    48. }
    49.  
    50. return result;
    51. }
    52.  
    53. /* Main */
    54. void main(void)
    55. {
    56. unsigned char i;
    57.  
    58. bits_t bit_array[10]; // Declare 10 bytes long bit array
    59.  
    60. SetBit(&bit_array[1], 3, 1); // Set bit3 of bit_array[1]
    61.  
    62. if(GetBit(&bit_array[1], 3)) // Check bit3 of bit_array[1]
    63. SetBit(&bit_array[1], 0, 1); // Set bit0 of bit_array[1]
    64.  
    65. for(i=0;i<8;i++) // Use for loop to access the bits
    66. SetBit(&bit_array[0], i, 1);
    67.  
    68. while(1)
    69. {}
    70.  
    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: Nov 15, 2014
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  6. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    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.
     
  7. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,795
    831
    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.
     
  8. mah

    Thread Starter Active Member

    Mar 15, 2010
    276
    2
    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
     
  9. mah

    Thread Starter Active Member

    Mar 15, 2010
    276
    2
    any help people with my code?
     
  10. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,795
    831
    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?
     
  11. mah

    Thread Starter Active Member

    Mar 15, 2010
    276
    2
    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: Nov 15, 2014
  12. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,795
    831
    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?
     
  13. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  14. mah

    Thread Starter Active Member

    Mar 15, 2010
    276
    2
    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
     
  15. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,795
    831
    Go to the Search page, type "elevator", and click search.
     
  16. mah

    Thread Starter Active Member

    Mar 15, 2010
    276
    2
    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."
     
  17. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,795
    831
    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
     
  18. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  19. mah

    Thread Starter Active Member

    Mar 15, 2010
    276
    2
    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
     
  20. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,795
    831
    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?
     
Loading...