uint8_t GPIO_Pin = D2;
uint8_t PWM_Pin = D6;
unsigned long duration;
uint8_t getData;
unsigned int rpm;
unsigned long timeold;
void setup() {
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(GPIO_Pin), IntCallback, RISING);
pinMode(GPIO_Pin, INPUT_PULLUP);
pinMode(PWM_Pin, OUTPUT);
analogWrite(PWM_Pin, 0);
duration = 0;
rpm = 0;
timeold = 0;
}
void loop() {
if (Serial.available()) { // If anything comes in Serial (USB),
getData = Serial.read(); // read it and send it out Serial1 (pins 0 & 1)
if( getData >='0' && getData <=':' ){
//Serial.println(getData);
analogWrite(PWM_Pin, map((getData-'0')*10, 0, 100, 0, 255) );
Serial.print("analogWrite ");
Serial.println((getData-'0')*10);
}
}
if (duration >= 2) {
rpm = 30 *1000 / (millis() - timeold) * duration;
//Serial.println(duration,DEC);
//Serial.println((millis() - timeold),DEC);
Serial.print("目前轉速:");
Serial.println(rpm,DEC);
timeold = millis();
duration = 0;
}
}
ICACHE_RAM_ATTR void IntCallback(){
duration++;
}
沒有留言:
張貼留言