#include <LedControl.h>
#include <Ultrasonic.h>
#define trigPin 8
#define echoPin 9
const int DATA_IN_PIN = 11; //DIN
const int CLK_PIN = 12; //CLK
const int LOAD_PIN = 10; //CS
const int NUM_DEVICES = 8;
int displayState = 0;
bool istrue = true;
LedControl lc = LedControl(DATA_IN_PIN, CLK_PIN, LOAD_PIN, NUM_DEVICES);
//大吉大利
byte A[][16] = {
{0x83, 0x42, 0x44, 0x24, 0x28, 0x28, 0x18, 0x18, 0x10, 0x10, 0xff, 0x10, 0x10, 0x10, 0x10, 0x10},
{0x44, 0x7c, 0x44, 0x44, 0x44, 0x7c, 0x00, 0x00, 0x7e, 0x10, 0x10, 0x10, 0xff, 0x10, 0x10, 0x10},
{0x83, 0x42, 0x44, 0x24, 0x28, 0x28, 0x18, 0x18, 0x10, 0x10, 0xff, 0x10, 0x10, 0x10, 0x10, 0x10},
{0x22, 0x26, 0xa2, 0xa2, 0x66, 0x6e, 0x76, 0x76, 0x26, 0xfe, 0x26, 0x26, 0x26, 0xe2, 0x3a, 0x12}
};
//平平安安
byte B[][16] = {
{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xff, 0x10, 0x34, 0x34, 0x54, 0x10, 0x10, 0xfe, 0x00},
{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xff, 0x10, 0x34, 0x34, 0x54, 0x10, 0x10, 0xfe, 0x00},
{0xc2, 0x24, 0x1c, 0x18, 0x38, 0x68, 0x24, 0x24, 0xff, 0x10, 0x10, 0x92, 0x42, 0x7e, 0x10, 0x10},
{0xc2, 0x24, 0x1c, 0x18, 0x38, 0x68, 0x24, 0x24, 0xff, 0x10, 0x10, 0x92, 0x42, 0x7e, 0x10, 0x10}
};
//步步高升
byte C[][16] = {
{0xc0, 0x60, 0x10, 0x88, 0x54, 0x54, 0x52, 0x10, 0x10, 0xff, 0x50, 0x50, 0x50, 0x5e, 0x10, 0x10},
{0xc0, 0x60, 0x10, 0x88, 0x54, 0x54, 0x52, 0x10, 0x10, 0xff, 0x50, 0x50, 0x50, 0x5e, 0x10, 0x10},
{0x82, 0xbe, 0xa6, 0xa6, 0xbe, 0x82, 0xfe, 0x00, 0x3c, 0x24, 0x24, 0x3c, 0x00, 0xff, 0x10, 0x10},
{0x84, 0x44, 0x44, 0x24, 0x24, 0x24, 0x24, 0x24, 0xff, 0x24, 0x24, 0x24, 0x24, 0x64, 0x3c, 0x14}
};
//太貪心
byte D[][16] = {
{0x83, 0x52, 0x54, 0x34, 0x28, 0x28, 0x18, 0x18, 0x10, 0x10, 0xff, 0x10, 0x10, 0x10, 0x10, 0x10},
{0x44, 0x24, 0x7c, 0x44, 0x7c, 0x44, 0x7c, 0x08, 0x08, 0xbf, 0x56, 0x2c, 0x18, 0x10},
{0x00, 0x1c, 0x24, 0xa4, 0xa5, 0xa1, 0x61, 0x62, 0x62, 0x22, 0x20, 0x08, 0x08, 0x10, 0x10, 0x00},
{0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00},
};
//派對時刻
byte E[][16] = {
{0x50, 0x79, 0x7a, 0x72, 0x74, 0xf4, 0x76, 0x76, 0x75, 0xb4, 0xbc, 0xa6, 0x60, 0x78, 0x46, 0x02},
{0x44, 0xe6, 0x32, 0x22, 0x72, 0x22, 0xf6, 0x66, 0x5a, 0x02, 0xf2, 0x6f, 0x72, 0xf2, 0x62, 0x62},
{0x04, 0x06, 0xa2, 0xea, 0xaa, 0xa2, 0xbf, 0xa2, 0xe2, 0xbf, 0xa4, 0xa4, 0xbe, 0xe4, 0x04, 0x04},
{0x8a, 0x56, 0x52, 0xa2, 0x66, 0x56, 0x36, 0x26, 0xf6, 0x56, 0x56, 0x26, 0xfe, 0x02, 0x22, 0x42}
};
//救命有蛇
byte F[][16] = {
{0x51, 0xd2, 0x4a, 0xdc, 0xe4, 0x6c, 0x4c, 0xea, 0xda, 0x4a, 0x52, 0xff, 0x48, 0x58, 0x68, 0x48},
{0x08, 0x08, 0x5c, 0x7e, 0x5a, 0x5a, 0x5a, 0x7e, 0x00, 0x83, 0x7e, 0x24, 0x28, 0x18, 0x10, 0x10},
{0x24, 0x2c, 0x24, 0x24, 0x3c, 0xa4, 0xa4, 0x7c, 0x64, 0x24, 0x3c, 0x20, 0x20, 0xff, 0x10, 0x10},
{0x00, 0x8f, 0xd9, 0x79, 0x69, 0x48, 0xcc, 0xec, 0xea, 0xe8, 0xf2, 0xf1, 0xff, 0x44, 0x44, 0x44}
};
//碳吉郎
byte G[][16] = {
{0x09, 0x2a, 0xa6, 0xfe, 0xbe, 0xbd, 0xb4, 0xac, 0xbf, 0xe8, 0x88, 0x5e, 0x56, 0xf6, 0x16, 0x04},
{0x44, 0x7c, 0x44, 0x44, 0x44, 0x7c, 0x00, 0x00, 0x7e, 0x10, 0x10, 0x10, 0xff, 0x10, 0x10, 0x10},
{0x44, 0x44, 0x74, 0x76, 0x56, 0x65, 0x45, 0x75, 0x56, 0x56, 0x76, 0x56, 0x56, 0x75, 0x27, 0x20},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
};
//!!!!
byte H[][16] = {
{0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00},
{0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00},
{0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00},
{0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x00}
};
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
for (int address = 0; address < NUM_DEVICES; address++) {
lc.shutdown(address, false);
lc.setIntensity(address, 8);
lc.clearDisplay(address);
}
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1; // Convert duration to distance
if (distance <= 10) { // If object is within 20 cm
Serial.println("1");
displayCurrentState();
if(istrue)
{
displayState++;
if(displayState>6)
{
displayState=0;
}
}
}
else {
Serial.println("0");
clearLED();
}
delay(500);
}
void displayCurrentState() {
switch (displayState) {
case 0:
displayChineseCharacters(A);
break;
case 1:
displayChineseCharacters(B);
break;
case 2:
displayChineseCharacters(C);
break;
case 3:
displayChineseCharacters(D);
break;
case 4:
displayChineseCharacters(E);
break;
case 5:
displayChineseCharacters(F);
break;
case 6:
displayChineseCharacters(G);
break;
case 7:
displayChineseCharacters(H);
break;
}
}
void displayChineseCharacters(byte characters[][16]) {
for (int charIndex = 0; charIndex < 4; charIndex++) {
for (int row = 0; row < 16; row++) {
int deviceIndex = row / 8;
int actualRow = row % 8;
lc.setRow(deviceIndex * 4 + charIndex, actualRow, characters[charIndex][row]);
}
}
}
void clearLED() {
for (int address = 0; address < NUM_DEVICES; address++) {
lc.clearDisplay(address);
}
}