見出し画像

8×8ドットマトリックス4連(5)・・急げ小緑人

はじめに

 前回、ドット絵のコマ表示後、delayを無くしても「だらだら歩く」ところまででした。信号機の表示切替のスピードに追い付いていなかったのです。今回は、もっと「急がせ」ます。

 その方法です。一枚のドット絵の表示の際、前回のように全256ドットすべてに書き込み命令を出すと無駄が出るので、「変化があるドット」だけを書き込みます。「変化がある」かどうかは、排他論理和 XOR(Arduinoでは ^ )を使えばいいですね。

改造結果・・・速くなったはず

改造後

改造前(前回比較)

今回の実装

#include <LedControl.h>

const int DIN_PIN = 7;
const int CS_PIN = 6;
const int CLK_PIN = 5;
const int DEVICES = 4;

const byte walkingimages[7][4][8]={
{{//0
B00000000,
B00000110,
B00001111,
B00000110,
B00000011,
B00000011,
B00000011,
B00000101
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B11100000,
B10010000,
B10001000
},{
B00001001,
B00000000,
B00000000,
B00000001,
B00000010,
B00001110,
B00001000,
B00000000
},{
B11000000,
B11000000,
B10100000,
B00010000,
B00000000,
B00000100,
B00000100,
B00000000  
}},

{{//1
B00000000,
B00000110,
B00001111,
B00000110,
B00000011,
B00000011,
B00000011,
B00000101
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B11100000,
B10010000,
B10001000
},{
B00001001,
B00000000,
B00000000,
B00000000,
B00000001,
B00000010,
B00001100,
B00000000
},{
B11000000,
B11000000,
B10100000,
B10011100,
B00000100,
B00000100,
B00000100,
B00001000  
}},

{{//2
B00000000,
B00000011,
B00000111,
B00000011,
B00000001,
B00000001,
B00000001,
B00000010
},{
B00000000,
B00000000,
B10000000,
B00000000,
B00000000,
B11000000,
B00100000,
B10010000
},{
B00000100,
B00000000,
B00000001,
B00000010,
B00000010,
B00000001,
B00000111,
B00000000
},{
B11010000,
B01000000,
B00100000,
B00111100,
B00000100,
B00001000,
B00000000,
B00000000
}},

{{//3
B00000000,
B00000011,
B00000111,
B00000011,
B00000001,
B00000001,
B00000001,
B00000000
},{
B00000000,
B00000000,
B10000000,
B00000000,
B10000000,
B11000000,
B10100000,
B11010000
},{
B00000000,
B00000001,
B00000000,
B00000001,
B00000010,
B00000001,
B00000000,
B00000011
},{
B11010000,
B01100000,
B11100000,
B00100000,
B00010000,
B10000100,
B10000100,
B10000000
}},

{{//4
B00000000,
B00000011,
B00000111,
B00000011,
B00000001,
B00000001,
B00000001,
B00000000
},{
B00000000,
B00000000,
B10000000,
B00000000,
B10000000,
B11000000,
B10100000,
B11010000
},{
B00000000,
B00000001,
B00000000,
B00000001,
B00000001,
B00000000,
B00000000,
B00000001
},{
B11010000,
B01100000,
B11100000,
B00100000,
B00010000,
B11001000,
B01011000,
B11010000
}},

{{//5
B00000000,
B00000011,
B00000111,
B00000011,
B00000001,
B00000001,
B00000001,
B00000000
},{
B00000000,
B00000000,
B10000000,
B00000000,
B10000000,
B11000000,
B11000000,
B11100000
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
},{
B11100000,
B01100000,
B01100000,
B10110000,
B01010000,
B01110000,
B00010000,
B01010000
}},

{{//6
B00000000,
B00000110,
B00001111,
B00000110,
B00000011,
B00000011,
B00000011,
B00000001
},{
B00000000,
B00000000,
B00000000,
B00000000,
B10000000,
B11000000,
B10100000,
B11010000
},{
B00000001,
B00000000,
B00000000,
B00000000,
B00000001,
B00000000,
B00000001,
B00000000
},{
B11010000,
B11100000,
B01100000,
B11000000,
B00100000,
B10011000,
B10000100,
B00011100
}}
};

LedControl display = LedControl(DIN_PIN, CLK_PIN, CS_PIN, DEVICES);

void setup() {
int devices = display.getDeviceCount();
for(int address=0;address<devices;address++) {
  display.shutdown(address,false);
  display.setIntensity(address,0);
  display.clearDisplay(address);
};
displayImageL(walkingimages[0]);
delay(2000);
}

void displayImage(int device,const byte *image) {
 for (int i = 0; i < 8; i++) {
   for (int j = 0; j < 8; j++) {
     display.setLed(device , i, j, bitRead(image[i], 7 - j));
   }
 }
}

void displaydiffImage(int device,const byte *image, const byte *preimage){
 for (int i = 0; i < 8; i++) {
   for (int j = 0; j < 8; j++) {
     if(preimage[i]^image[i]){//前回と違うときだけ実行
       display.setLed(device , i, j, bitRead(image[i], 7 - j));
     }
   }
 }
}

void displayImageL(const byte image[4][8]){
  displayImage(3, image[0]);
  displayImage(2, image[1]);
  displayImage(1, image[2]);
  displayImage(0, image[3]);
}

void displaydiffImageL(const byte image[4][8], const byte preimage[4][8]){
  displaydiffImage(3, image[0],preimage[0]);
  displaydiffImage(2, image[1],preimage[1]);
  displaydiffImage(1, image[2],preimage[2]);
  displaydiffImage(0, image[3],preimage[3]);
}

void loop() {
 for (byte num = 1; num<7; num++){
   displaydiffImageL(walkingimages[num],walkingimages[num-1]);
   //delay(50);
 }
 displaydiffImageL(walkingimages[0],walkingimages[6]);
 //delay(50);
}




この記事が気に入ったらサポートをしてみませんか?