Skip to content

Instantly share code, notes, and snippets.

@ScienceElectronicsFun
Created September 10, 2022 22:38
Show Gist options
  • Save ScienceElectronicsFun/77b1dcb274d1e0d37b463c1d31820735 to your computer and use it in GitHub Desktop.
Save ScienceElectronicsFun/77b1dcb274d1e0d37b463c1d31820735 to your computer and use it in GitHub Desktop.
#include <avr/io.h>
#include <avr/interrupt.h>
const int upDown = 32;
volatile long counter = 0;
volatile long counter_buffer = 0;
volatile bool lockBuffer = false;
int ledPin = 13;
//#define HWSERIAL Serial1
// the setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(33), myInterrupt, FALLING);
pinMode(upDown, INPUT);
Serial.begin(9600);
//HWSERIAL.begin(9600);
}
// the loop() methor runs over and over again,
// as long as the board has power
void loop() {
digitalWrite(ledPin, HIGH); // set the LED on
delay(10); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(10); // wait for a second
//cli();
//count_copy = overflow_count;
//sei();
lockBuffer = true;
Serial.println(counter_buffer);
//HWSERIAL.print(counter_buffer);
lockBuffer = false;
delay(10);
}
void myInterrupt() {
int val = 0;
val = digitalRead(upDown);
if (val == 1) {
counter += 1;
}
else {
counter -= 1;
}
if (lockBuffer == false) {
counter_buffer = counter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment