#include <Arduino.h>
#include <LedControl.h>
//#include "pitch.h"
const int LED_DIN = 11;
const int LED_CLK = 13;
const int LED_CS = 10;
const int TRIG = 9;
const int ECHO = 8;
LedControl lc = LedControl(LED_DIN, LED_CLK, LED_CS, 1);
int duration = 0;
int distance = 0;
//const int STEP = 30;
//const int THRESHOLD = 30;
byte levels[8][8] = {
{B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000},
{B00000000, B00000000, B00000000, B00011000, B00011000, B00000000, B00000000, B00000000},
{B00000000, B00000000, B00011000, B00111100, B00111100, B00011000, B00000000, B00000000},
{B00000000, B00011000, B00111100, B01111110, B01111110, B00111100, B00011000, B00000000},
{B00011000, B00111100, B01111110, B11111111, B11111111, B01111110, B00111100, B00011000},
{B00111100, B01111110, B11111111, B11111111, B11111111, B11111111, B01111110, B00111100},
{B01111110, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B01111110},
{B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111}
};
void setup() {
Serial.begin(9600);
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
// power on animation
for (int i=0; i<8; i++) {
for (int j=0; j<8; j++) {
lc.setRow(0, j, levels[i][j]);
}
delay(100);
}
}
void display(int scale) {
// 距離に応じたレベル表示
int level = min(scale , 7);
for (int i=0; i<8; i++) {
lc.setRow(0, i, levels[level][i]);
}
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(TRIG, HIGH);
delayMicroseconds(10); //Creates a ten microsecond delay
digitalWrite(TRIG, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(ECHO, HIGH);// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
Serial.println(distance);
Serial.print(" cm");
//if (distance > 200)
int scale = map(distance, 0 , 400, 0, 8);
display(scale);
delay(50);
}