Automatic Gate with FSM and ISR

The purpose of this project was to the control an automatic gate with Arduino Nano using finite state machine method and interrupts.

IntermediateShowcase (no instructions)4 hours3,678
Automatic Gate with FSM and ISR

Things used in this project

Story

Read more

Schematics

Automatic Gate Interrupt - Project

Architecture and FSM (finite state machine) flowcharts

Electric diagram of automatic gate

Electric diagram of automatic gate control with arduino using FSM and ISR

Breadboard scheme

Electric diagram

Code

Automatic Gate using FSM and ISR

Arduino
This code was developed to control an automatic gate under an university project, using finit state machines and interruption service routines.
/*
Universidade do Algarve - Instituto Superior de Engenharia * Microprocessors * Professor: António João Freitas Gomes da Silva
André Neves nº 49960
Nuno Martins nº 11854
Code to control an automatic gate using FSM (Finite State Machines) and ISR (Interrupt Service Routines)
 */

volatile int tpin = 2; // Sinal de comando (abre/fecha) do portão, pin 12   | Command Signal (Open/Close) the gate, pin 12
volatile int dpin = 3; // Sinal de fim de curso aberto/fechado, pin 13      | Open&close micro-switch signal open/close, pin 13  
volatile int t; // Variável de impulso de comando                           | Command Impulse Variable
volatile int ti; // Variável de armazenamento do sinal de comando           | Variable to memorize the command signal
volatile int d; // Variável para armazenar o sinal de fim de curso          | Variable to memorize Open&close micro-switch signal
volatile int A = 6; // led_A: Abertura de portão, pin 6                     | Open gate,pin 6
volatile int F = 7; // led_F: Fecho do portão, pin 7                        | Close gate, pin 7
volatile int I = 5; // led_I: Luz intermitente, pin 5                       | Blinking light, pin 5
volatile int Abre; // Variável para armazenar o sinal de abertura           | Variable to memorize the open signal
volatile int Fecha; // Variável para armazenar o sinal de fecho             | Variable to memorize the close signal
volatile int Interm; // Variável para armazenar o sinal da luz intermitente | Variable to memorize the Blinking light signal
volatile int state_1 = 0; // Variável dos estados de abertura/fecho/paragem | Variable of States the open/close/stop
volatile int state_2 = 0; // Variável dos estados da luz intermitente       | Variable of Blinking light state
volatile int state_s = 0; // Variável dos estados do sinal de comando       | Variable of Command signal state
volatile unsigned long currentmillis; // tempo actual                       | Current Time
volatile unsigned long previousmillis;// tempo anterior                     | Previous Time

void setup() {
  // put your setup code here, to run once:

pinMode (A, OUTPUT);      // Definição do pin associado a A como saída       | Configures the pin associated to A as Output
pinMode (F, OUTPUT);      // Definição do pin associado a F como saída       | Configures the pin associated to A as Output
pinMode (I, OUTPUT);      // Definição do pin associado a I como saída       | Configures the pin associated to A as Output
pinMode (tpin, INPUT);    // Definição do pin associado a tpin como entrada  | Configures the pin associated to tpin as Input
pinMode (dpin, INPUT);    // Definição do pin associado a dpin como entrada  | Configures the pin associated to dpin as Input
 // Attach an interrupt to the ISR vector
attachInterrupt(0, pin_ISR, CHANGE);        //Interrupção por acionamento do botão de comando   |Interruption due to command button
attachInterrupt(1, pin_ISR, CHANGE);        //Interrupção por acionamento do fim de curso       |Interruptdue to close/open micro-switch
}

void loop() {
  // put your main code here, to run repeatedly:
  state_2_next();
  state_2_out ();
  digitalWrite(I, Interm);             
}

void pin_ISR() {              //Rotina de Interrupção                      | Interrupt Service Routine.
  t = digitalRead(tpin);      // Leitura da Variavel T                     | Reading of Variable T.
  d = digitalRead(dpin);      // Leitura da Variavel D                     | Reading of Variable D.
  state_1_next();             // Chamada da função FSM1 - estado seguinte  | Calls function FSM1 - next state
  state_1_out();              // Chamada da função FSM2 - saída do estado  | Calls function FSM2 - output state
  digitalWrite (A, Abre);     // Leitura da Variavel A.                    | Reading of Variable A
  digitalWrite (F, Fecha);    // Leitura da Variavel F.                    | Reading of Variable F
 
}
 // Na função abaixo está descrito o funcionamento da FSM1 referente aos estados do motor do portão, em função das entradas T e D.
//The function state_1_next includes the code of FSM1 - gate motor state, in order of inputs T and D.
void state_1_next(){
   switch (state_1){
    case 0:
     if (t==HIGH){
     state_1=1;
     }
     break;
    case 1:
     if (d==HIGH){
     state_1=2;
     }
     else if (t==HIGH){
     state_1=4;
     }
     break;
    case 2:
     if (t==HIGH){
     state_1=3;
     }
     break;
    case 3:
     if (d==HIGH){
     state_1=0;
     }
     else if (t==HIGH){
     state_1=5;
     }
     break;
    case 4:
     if (t==HIGH){
     state_1=3;
     }
     break;
    case 5:
     if (t==HIGH){
     state_1=1;
     }
     break;}
 }
  //A função state_1_out diz respeito aos Outputs dos estados da função state_1_next.
 // The function state_1_out define the state_1 output.
void state_1_out(){
   switch (state_1){
    case 0:
     Abre=0;
     Fecha=0;
     break;
    case 1:
     Abre=1;
     Fecha=0;
     break;
    case 2:
     Abre=0;
     Fecha=0;
     break;
    case 3:
     Abre=0;
     Fecha=1;
     break;
    case 4:
     Abre=0;
     Fecha=0;
     break;
    case 5:
     Abre=0;
     Fecha=0;
     break;}
}
//Na funçao state_2_next está o codigo relativo à FSM3 - estado da luz intermintente durante a abertura/fecho do portão
//The function state_2_next includes the code of FSM3 - related to the blinking light state during opening/closing gate
void state_2_next(){
  currentmillis = millis();
  switch (state_2){
    case 0:
     if ((state_1==1 || state_1==3) && (currentmillis-previousmillis)>=50){
      state_2=1;
      previousmillis = currentmillis;}
     break;
    case 1:
     if (state_1==0 || state_1==2 || state_1==4 || state_1==5){
     state_2=0;}
     else if ((state_1==1 || state_1==3) && (currentmillis-previousmillis)>=50){
      state_2=0;
      previousmillis = currentmillis;}
     break;}
}
  //A função state_2_out diz respeito aos Outputs dos estados da função state_2_next.
 // The function state_2_out define the state_2 output.
void state_2_out(){
  switch (state_2){
    case 0:
    Interm=0;
    break;
    case 1:
    Interm=1;
    break;}
}

Credits

Nuno Miguel Guerreiro Carvalho Martins

Nuno Miguel Guerreiro Carvalho Martins

0 projects • 0 followers
Engineering student of electronics and energy systems. I also work on solar energy (thermal and photo-voltaic).
AndreNeves

AndreNeves

0 projects • 0 followers
Student of Electrical and Electronical Engineering

Comments