|
//bool zero[7] = {true, true, true, true, true, true, false}; |
|
//bool one[7] = {false, true, true, false, false, false, false}; |
|
//bool two[7] = {true, true, false, true, true, false, true}; |
|
//bool three[7] = {true, true, true, true, false, false, true}; |
|
//bool four[7] = {false, true, true, false, false, true, true}; |
|
//bool five[7] = {true, false, true, true, false, true, true}; |
|
//bool six[7] = {true, false, true, true, true, true, true}; |
|
//bool seven[7] = {true, true, true, false, false, false, false}; |
|
//bool eight[7] = {true, true, true, true, true, true, true}; |
|
//bool nine[7] = {true, true, true, false, false, true, true}; |
|
|
|
bool numbers[10][7] = { |
|
{true, true, true, true, true, true, false}, |
|
{false, true, true, false, false, false, false}, |
|
{true, true, false, true, true, false, true}, |
|
{true, true, true, true, false, false, true}, |
|
{false, true, true, false, false, true, true}, |
|
{true, false, true, true, false, true, true}, |
|
{true, false, true, true, true, true, true}, |
|
{true, true, true, false, false, false, false}, |
|
{true, true, true, true, true, true, true}, |
|
{true, true, true, false, false, true, true}// |
|
}; |
|
|
|
int distanceSensor = A0; |
|
|
|
void setup() { |
|
Serial.begin(9600); |
|
pinMode(distanceSensor, INPUT); |
|
for (int pin = 2 ; pin < 9 ; pin++){ // setup pins 2 – 8 to be output |
|
pinMode(pin, OUTPUT); |
|
} |
|
} |
|
|
|
void loop() { |
|
int meters = cm(readUltrasonicDistance(distanceSensor)) / 100; |
|
Serial.println(meters); |
|
runNumber(numbers[meters]); |
|
delay(100); |
|
} |
|
|
|
long cm(long dist) { |
|
return 0.01723 * dist; |
|
} |
|
|
|
void runNumber(bool number[7]) { |
|
for (int n = 0; n <= 7; n++) { |
|
int pin = n + 2; |
|
if (number[n]) { |
|
digitalWrite(pin, LOW); |
|
} else { |
|
digitalWrite(pin, HIGH); |
|
} |
|
} |
|
} |
|
|
|
long readUltrasonicDistance(int pin) |
|
{ |
|
pinMode(pin, OUTPUT); // Clear the trigger |
|
digitalWrite(pin, LOW); |
|
delayMicroseconds(2); |
|
// Sets the pin on HIGH state for 10 micro seconds |
|
digitalWrite(pin, HIGH); |
|
delayMicroseconds(10); |
|
digitalWrite(pin, LOW); |
|
pinMode(pin, INPUT); |
|
// Reads the pin, and returns the sound wave travel time in microseconds |
|
return pulseIn(pin, HIGH); |
|
} |