Membuat Indikator Bergerak Dengan LED matrix 8X8

Pada proyek sebelumnya dijelaskan cara membuat icon statis menggunakan LED Matrix 8X8 seperti gambar hati, smiley, tanda-tanya, dll. Bagi yang belum mencobanya silakan baca tulisan ini

Setelah kita bisa membuat icon statis maka dengan sedikit modifikasi kita bisa membuat animasi sederhana. Prinsipnya adalah dengan cara membuat beberapa icon statis secara bergantian.

Kali ini kita akan coba membuat animasi seperti pada gambar di bawah ini

Baiklah, berikut code scetch nya

  // 2-dimensional array of row pin numbers:
  const int row[8] = {2, 7, 19, 5, 13, 18, 12, 16};

  // 2-dimensional array of column pin numbers:
  const int col[8] = {6, 11, 10, 3, 17, 4, 8, 9};

  // 2-dimensional array of pixels:
  int pixels[8][8];

  int speed;

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

  speed = 300;
 
  for (int thisPin = 0; thisPin < 8; thisPin++) {
    // initialize the output pins:
    pinMode(col[thisPin], OUTPUT);
    pinMode(row[thisPin], OUTPUT);
    // take the col pins (i.e. the cathodes) high to ensure that
    // the LEDS are off:
    digitalWrite(col[thisPin], HIGH);
  }

  // initialize the pixel matrix:
  for (int x = 0; x < 8; x++) {
    for (int y = 0; y < 8; y++) {
      pixels[x][y] = HIGH;
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:

  detakJantung();
  delay(100);

}

void refreshScreen() {
  // iterate over the rows (anodes):
  for (int thisRow = 0; thisRow < 8; thisRow++) {
    // take the row pin (anode) high:
    digitalWrite(row[thisRow], HIGH);
    // iterate over the cols (cathodes):
    for (int thisCol = 0; thisCol < 8; thisCol++) {
      // get the state of the current pixel;
      int thisPixel = pixels[thisRow][thisCol];
      // when the row is HIGH and the col is LOW,
      // the LED where they meet turns on:
      digitalWrite(col[thisCol], thisPixel);
      //delay(6);
      // turn the pixel off:
      if (thisPixel == LOW) {
        digitalWrite(col[thisCol], HIGH);
      }
    }
    // take the row pin low to turn off the whole row:
    digitalWrite(row[thisRow], LOW);
  }
}


void detakJantung(){

  getPict1(pixels);

  for (int i = 0; i <speed; i++) {
    refreshScreen();
  }

  clearPict1(pixels);
  getPict2(pixels);

  for (int i = 0; i <speed; i++) {
    refreshScreen();
  }

  clearPict2(pixels);
  getPict3(pixels);

  for (int i = 0; i <speed; i++) {
    refreshScreen();
  }

  clearPict3(pixels);
  getPict2(pixels);
 
  for (int i = 0; i <speed; i++) {
    refreshScreen();
  }

 
  clearPict2(pixels);


}

void getPict1(int pixdata[8][8]) {
  pixdata[2][3] = LOW;
  pixdata[2][4] = LOW;
  pixdata[3][2] = LOW;
  pixdata[3][4] = LOW;
  pixdata[4][3] = LOW;
  pixdata[4][4] = LOW;
}

void clearPict1(int pixdata[8][8]) {
  pixdata[2][3] = HIGH;
  pixdata[2][4] = HIGH;
  pixdata[3][2] = HIGH;
  pixdata[3][4] = HIGH;
  pixdata[4][3] = HIGH;
  pixdata[4][4] = HIGH;
}

void getPict2(int pixdata[8][8]) {
  pixels[1][5] = LOW;
  pixels[1][4] = LOW;
  pixels[1][3] = LOW;
  pixels[2][5] = LOW;
  pixels[2][2] = LOW;
  pixels[3][1] = LOW;
  pixels[3][4] = LOW;
  pixels[4][2] = LOW;
  pixels[4][5] = LOW;
  pixels[5][3] = LOW;
  pixels[5][4] = LOW;
  pixels[5][5] = LOW;
}

void clearPict2(int pixdata[8][8]) {
  pixels[1][5] = HIGH;
  pixels[1][4] = HIGH;
  pixels[1][3] = HIGH;
  pixels[2][5] = HIGH;
  pixels[2][2] = HIGH;
  pixels[3][1] = HIGH;
  pixels[3][4] = HIGH;
  pixels[4][2] = HIGH;
  pixels[4][5] = HIGH;
  pixels[5][3] = HIGH;
  pixels[5][4] = HIGH;
  pixels[5][5] = HIGH;
}

void getPict3(int pixdata[8][8]) {
  pixels[0][3] = LOW;
  pixels[0][4] = LOW;
  pixels[0][5] = LOW;
  pixels[1][6] = LOW;
  pixels[1][2] = LOW;
  pixels[2][6] = LOW;
  pixels[2][1] = LOW;
  pixels[3][5] = LOW;
  pixels[3][0] = LOW;
  pixels[4][6] = LOW;
  pixels[4][1] = LOW;
  pixels[5][6] = LOW;
  pixels[5][2] = LOW;
  pixels[6][5] = LOW;
  pixels[6][4] = LOW;
  pixels[6][3] = LOW;
}

void clearPict3(int pixdata[8][8]) {
  pixels[0][3] = HIGH;
  pixels[0][4] = HIGH;
  pixels[0][5] = HIGH;
  pixels[1][6] = HIGH;
  pixels[1][2] = HIGH;
  pixels[2][6] = HIGH;
  pixels[2][1] = HIGH;
  pixels[3][5] = HIGH;
  pixels[3][0] = HIGH;
  pixels[4][6] = HIGH;
  pixels[4][1] = HIGH;
  pixels[5][6] = HIGH;
  pixels[5][2] = HIGH;
  pixels[6][5] = HIGH;
  pixels[6][4] = HIGH;
  pixels[6][3] = HIGH;
}
Bahasa Indonesia