Created
January 8, 2020 10:37
-
-
Save deviceplususer/bf93414755ab19157911ef5fe6e1a429 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ライブラリ | |
#include <Wire.h>//I2Cライブラリ | |
#include <VL6180X.h>//TOF距離センサーのライブラリ | |
#include <SparkFun_TB6612.h>//モータードライバのライブラリ | |
//センサー、サウンド関連 | |
#define SPEAKER 8//スピーカーを接続したピン | |
#define VOLUME_SWITCH 13//音量調整つまみのスイッチに接続する端子 | |
VL6180X sensor;//TOF距離センサーのオブジェクト | |
const int minFreq = 262;//最低の周波数 ド(C4) | |
const int maxFreq = 494;//最高の周波数 シ(B4) | |
const int heightRange = 50;//高さの範囲 50mm | |
//モーター関連 | |
#define AIN1 2//モータードライバのAIN1に接続するピン | |
#define AIN2 4//モータードライバのAIN2に接続するピン | |
#define PWMA 5//モータードライバのPWMAに接続するピン | |
#define STBY 9//モータードライバのSTBYに接続するピン | |
#define MOTOR_SPEED A0//モーターの速度調整つまみに接続するピン | |
const int offsetA = 1;//回転方向の指定(1 or -1) | |
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);//モータードライバのオブジェクト | |
//原点設定関連 | |
#define ORIGIN_BUTTON 11//原点設定ボタンに接続するピン | |
bool prevButtonState = true;//原点設定ボタンの前の状態 | |
int origin = 0;//原点の高さ | |
void setup() | |
{ | |
Serial.begin(9600);//シリアル通信を開始 | |
pinMode(VOLUME_SWITCH, INPUT_PULLUP);//音量つまみスイッチのピンをプルアップの入力モードにする | |
pinMode(ORIGIN_BUTTON, INPUT_PULLUP);//原点設定ボタンのピンをプルアップの入力モードにする | |
Wire.begin();//I2Cを開始 | |
sensor.init();//TOF距離センサーを初期化 | |
sensor.configureDefault();//TOF距離センサーを標準的な値に設定 | |
sensor.setTimeout(500);//タイムアウトを500msに設定 | |
} | |
void loop() | |
{ | |
//------------------------------------------------------------ | |
// TOF距離センサーで距離を測り、周波数に変換する | |
//------------------------------------------------------------ | |
float distance = sensor.readRangeSingleMillimeters();//TOF距離センサーの値を取得(単位:mm) | |
bool offsetButtonState = digitalRead(ORIGIN_BUTTON); //原点設定ボタンの状態を取得 | |
if ( offsetButtonState == 0 && prevButtonState == 1 )//原点設定ボタンが押されたら、 | |
{ | |
origin = distance;//originを現在のdistanceに変更 | |
} | |
prevButtonState = offsetButtonState;//prevButtonStateを更新 | |
float frequency = map(distance, origin, origin-heightRange, minFreq, maxFreq);//距離を周波数に変換 | |
Serial.print("Distance:"); | |
Serial.print(distance); | |
Serial.print(" Frequency:"); | |
Serial.print(frequency); | |
Serial.println(); | |
//---------------------------------------------------------------------- | |
// 指定した周波数で音を鳴らす | |
//---------------------------------------------------------------------- | |
bool volumeSwitchState = digitalRead(VOLUME_SWITCH);//音量つまみスイッチの値を取得 | |
if ( volumeSwitchState == 0 ) {//音量つまみスイッチがオンなら | |
tone(SPEAKER, frequency );//スピーカーから距離に応じた周波数の音を鳴らす | |
} else { | |
noTone(SPEAKER);//スイッチがオフなら音を止める | |
} | |
//---------------------------------------------------------------------- | |
// 速度調整つまみの値に応じてモーターをコントロールする | |
//---------------------------------------------------------------------- | |
int motorSpeed = analogRead(MOTOR_SPEED);//速度調整つまみの値を取得 | |
if ( motorSpeed > 10 )//つまみの値が一定以上なら | |
{ | |
motorSpeed = map( motorSpeed, 10, 1023, 40, 255 );//つまみの値をスピードに変換 | |
motor1.drive( motorSpeed );//指定したスピードでモーターを回す | |
} else { | |
motor1. brake();//つまみの値が一定以下ならモーターを止める | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment