Create a new Arduino sketch. Define two global variables called count and prev_c
ID: 3762260 • Letter: C
Question
Create a new Arduino sketch.
Define two global variables called count and prev_count.
Make sure the variables are declared as a volatile unsigned long.
Initialize both variables to zero.
Setup()
Enable interrupts.
Use the attachInterrupt() function to assign INT0 to your Interrupt Service Routine (ISR).
ISR()
Write your ISR function below the loop function.
Each time the ISR is called, it should increment the variable count.
Loop()
Check to see if count > prev_count. If it is, print count to the Serial Monitor and assign prev_count to the current value of count.
Explanation / Answer
//declaring and assigning variables
volatile unsigned long count = 0, prev_count = 0;
void setup() {
//enabling interrupts
interrupts();
//attaching ISR function to the interrupt
//specifying cause interrupt wher theres a change
attachInterrupt(INT0,isrFunction,CHANGE);
}
void loop()
{
//comparing count to prev_count
if(count > prev_count)
{
//if true
//write to serial
Serial.print(count);
prev_count = count;
}
}
//ISR function
//called whenever there is a change in INT0
void isrFunction()
{
//incrementing count
count = count + 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.