超音波距離センサー その2 名札

f:id:tadpole-endoh:20170811102409j:plain

 電光掲示板式の名札の問題は、いつも流れているとうっとうしい。そこで超音波距離センサーを付けた。左下にあるのはreal time clock で、この情報も受け取ります。

 人が近づくと私の名前、所属、今の時刻を表示します。使ってみると時刻は余計だった。写真は大きいmatrix だが目立ちすぎてうっとうしい、名札として使うならmini matrix の赤以外の色を勧めます。

 写真で:右上はSparkFun の電源。左上は超音波距離センサー、右下はadafruit 8×8のmatrix 。左真ん中はArduino Pro Mini 5V。左下はRTC。Lipo battery は基板の裏に回っています。

【使い方】レセプションで。

「これは美人が近づくと動き出して私の名前をアピールします。」(女性は気持ち悪そうにオヤジを見つめ返す)

 「ああっ!動き出した。流石にお奇麗なかたですねえ。」(理系の女性ほどはまる)  

 実際にはビール瓶でも何でも近づくと動く。あなたの話術の技量次第。

       読者の健闘を祈る!

 ここから下はarduino IDE に入れてupload して下さい。RTC, matrix, 超音波距離センサーのsample code を混ぜて書き直しただけです。duration の値を変えると検知する距離を変えられます。RTCを外すならその部分をcomment out しながら動作確認をすれば不必要な部分が判るはずです。

 

//Before running this program, you must set the time for RTC by using //realTimeClockTimerReset program.
#include <Wire.h>
#include "RTClib.h"
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <avr/power.h>
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
  RTC_DS1307 RTC;      // Establish clock object
  DateTime Clock;      // Holds current clock time
    int hourval=0;
  int minuteval=0;
  int secondval=0;
const int Trig = 11;
const int Echo = 10;
unsigned int Duration = 0;
void setup() {
    power_spi_disable();
    power_adc_disable();
    power_usart0_disable();
    pinMode(Trig,OUTPUT);
    pinMode(Echo,INPUT);
    Wire.begin(0x68);
    RTC.begin();              // Initialize clock
    matrix.begin(0x70);  // pass in the address
  }
   
void loop() {
    //  DateTime Clock;                    // variable to hold our time
    char* colon = ":";                 // static characters save a bit
    char* slash = "/";                 //   of memory
 
  Clock = RTC.now();                 // get the RTC time
 
  hourval = Clock.hour();  //+offset  calculate hour to display
  if(hourval > 23) hourval-=24;      // adjust for over 23 hour
      else if(hourval < 0) hourval+=24;  //   or under 0 hours
 
  minuteval = Clock.minute();        // This block prints the time
  secondval = Clock.second();        //  to the LCD Shield
 
    digitalWrite(Trig,LOW);
    delayMicroseconds(1);
    digitalWrite(Trig,HIGH);
    delayMicroseconds(1);
    digitalWrite(Trig,LOW);
    Duration = pulseIn(Echo,HIGH);
 
  if (Duration<3750) {
    // triger for 50cm=2500 duration
        matrix.setTextSize(1);
        matrix.setTextWrap(false);  // we dont want text to wrap so it scrolls nicely
        matrix.setTextColor(LED_ON);
        matrix.setRotation(0);
    for (int8_t x=8; x>=-70; x--) {
      matrix.clear();
      matrix.setCursor(x,0);
      matrix.print("T.Endoh");
      matrix.writeDisplay();
    delay(75);
  }
 
    matrix.setRotation(3);
    for (int8_t x=8; x>=-76; x--) {
      matrix.clear();
      matrix.setCursor(x,0);
      matrix.print("Chinke Company");
      matrix.writeDisplay();
    delay(65);
  }
 
     matrix.setTextSize(1);
        matrix.setTextWrap(false);  
        matrix.setTextColor(LED_ON);
        matrix.setRotation(3);
    for (int8_t x=8; x>=-30; x--) {
        matrix.clear();
        matrix.setCursor(x,0);
      if(hourval < 10) printzero();    
        matrix.print(hourval);
        matrix.print(colon);
      if(minuteval < 10) printzero();
        matrix.print(minuteval);
   
          matrix.writeDisplay();
      delay(80);
      matrix.setRotation(0);
      }
     
      matrix.setRotation(3);
    for (int8_t x=8; x>=-60; x--) {
      matrix.clear();
      matrix.setCursor(x,0);
      matrix.print("O`Clock");
      matrix.writeDisplay();
    delay(45);
  }
   delay(3000);
  }
  else;
  delay(1000);
}
void printzero() {
    matrix.print("0");
  }