Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created April 11, 2010 07:53
Show Gist options
  • Save tatsuro-ueda/362580 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/362580 to your computer and use it in GitHub Desktop.
// 以下は【ステップ1】で使う
const int buttonPin = 5; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
int counter = 0; // 10ミリ秒ごとに1ずつ増えていくカウンタ
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);
}
void loop(){
// 【ステップ1】まず<ボタンの状態>※を読み取り・・・
buttonState = digitalRead(buttonPin);
// もし※がオンで・・・
if (buttonState == HIGH) {
// かつ直前に接触していなければ・・・
if (!has_touched) {
// ボタンが押された瞬間とみなして、カウンタから諸値を計算して表示し・・・
calc_and_display(counter);
// カウンタをリセットし、フラグを立てる。
counter = 0; has_touched = true;
}
// もし※がオフなら・・・
} else {
// フラグを下げる。
has_touched = false;
}
// 【ステップ2】カウンタを1増やし、10msのあいだ待つ。
// 整数は3万ぐらいが上限なので、10倍することを見越して1000でストップ
if (counter < 1000) { counter++; }
delay(10);
}
void calc_and_display(int counter) {
// カウンタの値からRPMを計算し・・・
bike_cadence = 1000.0 / (counter * 10.0) * 60.0;
sprintf(display_str1, "%s%d%s", "Cadence: ", (int)bike_cadence, " rpm");
// さらに速さを計算して・・・
bike_speed = 0.7 * 3.14 * 3.6 * 1000 / (counter * 10);
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