Day4 02-encoder

2024. 2. 4. 15:45arduino

  • 인터럽트를 사용하여 인코더의 펄스를 세는 코드
int encoderPin = 2;

unsigned long oldTime = 0;
unsigned long newTime = 0;

volatile int count = 0;


void ISREncoder() {
  count++;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(encoderPin, INPUT_PULLUP);

  attachInterrupt(INT0, ISREncoder, FALLING);
}

void loop() {
  // put your main code here, to run repeatedly:
  newTime = millis();
  if (newTime - oldTime > 1000) {
    oldTime = newTime;
    noInterrupts();
    Serial.println(count);
    count = 0;
    interrupts();
  }
}

'arduino' 카테고리의 다른 글

Day4 04-DCmotor, button  (0) 2024.02.04
Day4 03-DCmotor  (0) 2024.02.04
Day4 01-debounce  (0) 2024.02.04
Day3 01-dht11  (0) 2024.02.04
Day2 06_Debounce  (0) 2024.01.18