Arduino Microcontrollers SREEJAA SUNDARARAJU AND R. SCOTT CARSON BME 462
Parts of a sketch void setup() Runs at power-on and reset Used to initialize pins and variables void loop() Continually runs while the microcontroller is powered This is where work is performed
Arduino IDE Verify Upload New, Open, Save Serial Monitor
Defining a sample frequency Max ECG frequency ~150Hz F sample >= 2*150 Hz F sample = 5*150 Hz = 750 Hz We want to sample every 1.33 ms
Implementation 1 – delay() delay(int t) or delayMicroseconds(int t) Performs a busy wait for t time float ecg_reading = 0.0; int ecg = A0; void setup(){ Serial.begin(9600); } void loop(){ ecg_reading = analogRead(ecg); Serial.println(ecg_reading); delayMicroseconds(1333); }
Implementation 2 – Timer interrupt ATMega328 microprocessor has 3 timers Functionality defined by configuration registers Can be complex if you are unfamiliar with embedded programming 88A-88PA-168A-168PA P_datasheet_Complete.pdf TimerOne library - A wrapper class around Timer1 to provide easy access to Timer functionality, including configuring interrupts Interrupts – Interrupts are used to tell the CPU a certain event has occurred and to execute a specific function known as an Interrupt Service Routine (ISR) Interrupt events Counter reaching a specified value Button press Timer expiring This is the interesting one for us.
Timer Interrupts con’t #include void timerISR(); // Used to store analog value float ecg_reading = 0.0; int ecg = A0; // ECG analog pin volatile boolean timer_expired = false; void setup(){ Timer1.initialize(1333); Timer1.attachInterrupt(timerISR); Serial.begin(9600); } void loop(){ if(timer_expired){ noInterrupts(); ecg_reading = analogRead(ecg); Serial.println(ecg_reading); timer_expired = false; interrupts(); } void timerISR(){ timer_expired = true; }