#include <avr/io.h>
#include <avr/interrupt.h>
#include <LiquidCrystal_I2C.h>
volatile LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
int interruptPin = 4; // TENG 출력을 연결한 ATtiny85의 입력 핀
#define INTERRUPT_PIN PCINT1
#define INT_PIN PB1
#define PCINT_VECTOR PCINT0_vect
volatile unsigned long pulseTime = 0; // TENG 출력 감지 시간
volatile unsigned long lastPulseTime = 0; // 이전 TENG 출력 감지 시간
volatile unsigned long pulsePeriod = 0; // TENG 출력 주기
volatile float speedKmph = 0.0; // 속도 (Km/h)
void setup()
{
cli();
PCMSK |= (1 << INTERRUPT_PIN);
GIMSK |= (1 << PCIE);
pinMode(INT_PIN, INPUT_PULLUP);
sei();
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(3, 0);
lcd.print("ok");
}
void loop()
{
if (pulsePeriod > 0) {
// 속도 계산: 주기와 1m 기준으로 Km/h로 변환
speedKmph = 3.6 / ((pulsePeriod /1000.0)/ 1000.0) ;
// LCD 디스플레이에 속도 출력
lcd.setCursor(0, 0);
lcd.print(speedKmph, 1);
lcd.print(" km/h ");
pulsePeriod = 0; // 주기 초기화
}
}
ISR(PCINT_VECTOR)
{
pulseTime = micros(); // 현재 시간 기록
if (lastPulseTime > 0) {
pulsePeriod = pulseTime - lastPulseTime;
}
int a= 100;
lcd.setCursor(1, 1);
lcd.print(pulsePeriod, 1);
lastPulseTime = pulseTime;
}