Site icon Elektrologi

Lampu Geser 2 Arah Asinkron Dengan Mikroprosesor

Spesifikasi

Perilaku yang diharapkan

Perancangan

Pada artikel ini, solusi yang dibuat memiliki sifat berikut

Perilaku sistem yang diharapkan dapat dimodelkan dengan menggunakan Timing Diagram (diagram waktu).. Untuk sederhananya dimulai dulu dengan kasus lampu hanya digeser ke 1 arah saja.

Diagram waktu lampu geser kanan 1 arah

Model Flowchart (Diagram Alir)

Model State Chart

State chart lampu geser asinkron

Video demo

 

Hasil

Lampiran

Implementasi perangkat lunak

 /*
  lampu geser dengan input event-triggerred dari interupsi eksternal
  FSM dengan model asinkron 
*/

#define OUT1 12
#define OUT2 11
#define OUT3 10
#define OUT4 9

#define STATE1 101
#define STATE2 102
#define STATE3 103
#define STATE4 104

#define EVENT_A 201
#define EVENT_B 202

int state = STATE1;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(OUT1, OUTPUT);
  pinMode(OUT2, OUTPUT);
  pinMode(OUT3, OUTPUT);
  pinMode(OUT4, OUTPUT);

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(2), Pin2_isr, FALLING );
  attachInterrupt(digitalPinToInterrupt(3), Pin3_isr, FALLING );

  // initial state
  state = STATE1;
  digitalWrite(OUT1, HIGH);
}

void Pin2_isr() {
  fsm(EVENT_B);
}

void Pin3_isr() {
  fsm(EVENT_A);
}

int fsm(int event) {
  switch (state) {
    case STATE1: {
        if (event == EVENT_A) {
          state = STATE2;
          digitalWrite(OUT1, LOW);
          digitalWrite(OUT2, HIGH);
          digitalWrite(OUT3, LOW);
          digitalWrite(OUT4, LOW);
        } else if (event == EVENT_B)
        {
          state = STATE4;
          digitalWrite(OUT1, LOW);
          digitalWrite(OUT2, LOW);
          digitalWrite(OUT3, LOW);
          digitalWrite(OUT4, HIGH);
        }
        break;
      }
    case STATE2: {
        if (event == EVENT_A) {
          state = STATE3;
          digitalWrite(OUT1, LOW);
          digitalWrite(OUT2, LOW );
          digitalWrite(OUT3, HIGH);
          digitalWrite(OUT4, LOW);
        } else if (event == EVENT_B)
        {
          state = STATE1;
          digitalWrite(OUT1, HIGH);
          digitalWrite(OUT2, LOW);
          digitalWrite(OUT3, LOW);
          digitalWrite(OUT4, LOW);
        }
        break;
      }
    case STATE3: {
        if (event == EVENT_A) {
          state = STATE4;
          digitalWrite(OUT1, LOW);
          digitalWrite(OUT2, LOW );
          digitalWrite(OUT3, LOW);
          digitalWrite(OUT4, HIGH);
        } else if (event == EVENT_B)
        {
          state = STATE2;
          digitalWrite(OUT1, LOW);
          digitalWrite(OUT2, HIGH);
          digitalWrite(OUT3, LOW);
          digitalWrite(OUT4, LOW);
        }
        break;
      }
    case STATE4: {
        if (event == EVENT_A) {
          state = STATE1;
          digitalWrite(OUT1, HIGH);
          digitalWrite(OUT2, LOW);
          digitalWrite(OUT3, LOW);
          digitalWrite(OUT4, LOW);
        } else if (event == EVENT_B)
        {
          state = STATE3;
          digitalWrite(OUT1, LOW);
          digitalWrite(OUT2, LOW);
          digitalWrite(OUT3, HIGH);
          digitalWrite(OUT4, LOW);
        }
        break;
      }
    default: {
        break;
      }
  }
}

void loop() {
}

Contoh-contoh lain pemodelan dengan FSM dapat dilihat di artikel “Contoh Implementasi FSM dengan mikrokontroler

Exit mobile version