Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created April 3, 2010 10:20
Show Gist options
  • Save tatsuro-ueda/354350 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/354350 to your computer and use it in GitHub Desktop.
Arduinoをスピードセンサーにつなげてみた
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 5; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
#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