Created
April 3, 2010 10:20
-
-
Save tatsuro-ueda/354350 to your computer and use it in GitHub Desktop.
Arduinoをスピードセンサーにつなげてみた
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 <LiquidCrystal.h> | |
LiquidCrystal clcd(6, 7, 8, 9, 10, 11); // Arduino-0017以降 | |
const int buttonPin = 5; // the number of the pushbutton pin | |
int buttonState = 0; // variable for reading the pushbutton status | |
// ホイールが回転した回数 | |
int times = 0; | |
// 10ミリ秒ごとに1ずつ増えていくカウンタ | |
int counter = 0; | |
// 自転車のスピード | |
float bike_speed = 0.0; | |
// 自転車のスピードを整数にしたもの | |
// floatはLCDにprintできない | |
int bike_speed_int = 0; | |
// 接触直後か(チャタリング対策) | |
boolean has_touched = false; | |
void setup(){ | |
clcd.begin(16, 2) ; // Arduino-0017以降は追加 | |
pinMode(buttonPin, INPUT); | |
} | |
void loop(){ | |
// まずボタンの状態を読み取り・・・ | |
buttonState = digitalRead(buttonPin); | |
// もしオンで・・・ | |
if (buttonState == HIGH) { | |
// かつ直前に接触していなければ・・・ | |
if (!has_touched) { | |
// ホイールが回転した回数を1増やし、フラグを立てる | |
times++; has_touched = true; | |
} | |
// 最初に読み取ったボタンの状態がオフなら・・・ | |
} else { | |
// フラグを下げる | |
has_touched = false; | |
} | |
// 1秒経過するごとに・・・ | |
if (counter % 100 == 0){ | |
// スピードを計算して表示を更新する | |
bike_speed = 0.7 * 3.14 * times * 3.6; | |
bike_speed_int = (float)bike_speed; | |
clcd.clear() ; | |
clcd.println(bike_speed_int); | |
times = 0; | |
counter = 0; | |
} | |
counter++; | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment