Created
April 25, 2010 11:46
-
-
Save tatsuro-ueda/378350 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
// 以下は loop() で使う | |
const int buttonPin = 5; // the number of the pushbutton pin | |
boolean buttonState = 0; // variable for reading the pushbutton status | |
//int counter = 0; // 5ミリ秒ごとに1ずつ増えていくカウンタ | |
long cycle_time = 0; // 車輪が1周するのにかかった時間(ミリ秒) | |
long old_millis = 0; // 前回ボタンが押された時刻 | |
boolean has_touched = false; // 接触直後か(チャタリング対策) | |
// 以下は calc_and_display() で使う | |
#include <LiquidCrystal.h> | |
LiquidCrystal clcd(6, 7, 8, 9, 10, 11); // Arduino-0017以降 | |
#include <stdio.h> // sprintfを使うために必要 | |
float bike_cadence = 0.0; // ホイールの分当たり回転数 | |
float bike_speed = 0.0; // 自転車のスピード | |
char display_str1[16]; // 液晶に表示するための文字列 | |
char display_str2[16]; // floatはLCDにprintできない | |
void setup(){ | |
clcd.begin(16, 2) ; // Arduino-0017以降は追加 | |
pinMode(buttonPin, INPUT); | |
clcd.print("Ready !"); | |
} | |
void loop(){ | |
// まず<ボタンの状態>※を読み取り・・・ | |
buttonState = digitalRead(buttonPin); | |
// もし※がオンで・・・ | |
if (buttonState == HIGH) { | |
// かつ直前に接触していなければ・・・ | |
if (!has_touched) { | |
// ボタンが押された瞬間とみなして、 | |
// 周期を計算し、時刻を記録し、フラグを立てて、 | |
cycle_time = millis() - old_millis; | |
old_millis = old_millis + cycle_time; | |
has_touched = true; | |
// 周期から諸値を計算して表示する | |
calc_and_display(cycle_time); | |
} | |
// もし※がオフなら・・・ | |
} else { | |
// フラグを下げる。 | |
has_touched = false; | |
} | |
delay(5); | |
} | |
void calc_and_display(long cycle_time) { | |
// カウンタの値からRPMを計算し・・・ | |
//bike_cadence = 1000.0 / cycle_time * 60.0; | |
//sprintf(display_str1, "%s%d%s", "Cadence: ", (int)bike_cadence, " rpm"); | |
// ケイデンス・センサを付けるまでは周期を表示する | |
sprintf(display_str1, "%s%d%s", "Time: ", (int)cycle_time, " ms"); | |
// さらに速さを計算して・・・ | |
bike_speed = 0.7 * 3.14 * 3.6 * 1000 / cycle_time; | |
sprintf(display_str2, "%s%d%s", "Speed: ", (int)bike_speed, " km/h"); | |
// 液晶パネルに表示する。 | |
clcd.clear() ; | |
clcd.print(display_str1); | |
clcd.setCursor(0,1); | |
clcd.print(display_str2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment