int trigPin = 12; //Trig Pin 定義超音波感測器角位
int echoPin = 11;//Echo Pin //定義超音波感測器角位
int ledPin = 13; //定義燈號角位
volatile int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
int val1=0; //超音波感測器數值
int val2=0; //HALL感測器數值
long duration, cm, inches;
void setup() {
Serial.begin (9600); // Serial Port begin
pinMode(trigPin, OUTPUT); //Define inputs and outputs
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
attachInterrupt(0,rpm, FALLING);//interrupt cero (0) is on pin two(2).
}
void loop()
{
if (millis() - lastmillis == 500){
rpm = rpmcount * 60;
val2 = rpm;
Serial.print("RPM =\t"); //print the word "RPM" and tab.
Serial.print(rpm);
Serial.print("\t Hz=\t"); //print the word "Hz".
Serial.println(rpmcount); /*print revolutions per second or Hz. And print new line or enter.*/
rpmcount = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm, FALLING); //enable interrupt
}
if(val2>360){ //若時速超過20公里
Distance();
if(val1<=20) //距離小於20CM
{
digitalWrite(13,HIGH); //LED警示燈亮起
}
else
{
digitalWrite(13,LOW); //LED警示燈為暗
}
}
}