Difference between revisions of "EMG - PhantomX Reactor Robot"
Line 32: | Line 32: | ||
<ul> | <ul> | ||
− | <li> <b> void initialize(unsigned long period) </b>: Initializes and set the timer to <i>period</i> in microseconds </li> | + | <li> <b> void initialize(unsigned long period) </b>: Initializes and set the timer to <i>period</i> in microseconds. </li> |
− | <li> <b> void setPeriod(unsigned long period) </b>: Set the times to <i>period</i> in microseconds </li> | + | <li> <b> void setPeriod(unsigned long period) </b>: Set the times to <i>period</i> in microseconds. </li> |
− | <li> <b> void attachInterrupt(void (*isr)()) </b>: Attachs a function to the timer that is called every <i> period </i>. | + | <li> <b> void attachInterrupt(void (*isr)()) </b>: Attachs a function to the timer that is called every <i> period </i>.</li> |
− | <li> <b> void start(void)</b>: Starts the timer</li> | + | <li> <b> void start(void)</b>: Starts the timer.</li> |
− | <li> <b> void stop(void)</b>: Stops the timer </li> | + | <li> <b> void stop(void)</b>: Stops the timer. </li> |
Revision as of 14:37, 24 February 2025
Timer
Note: Most of the information of this section is obtained from: https://www.prometec.net/timers/ Please refer to the previuos link and the library if you need more information.
A timer in electronics is a device or circuit used to measure and control time intervals. It generates precise delays or oscillations, making it essential in various applications like blinking LEDs, controlling motors or executing periodic events, between others.
It operates independently from the main program, allowing the microcontroller to perform other tasks simultaneously. Timers typically work by counting clock pulses from the microcontroller’s internal clock or an external source. They can be configured in various modes, such as delay generation, pulse width modulation (PWM), event counting, or frequency measurement.
Timers are configured by setting up several registers, to adjust the sampling time, durating, mode, starting, stoping,.... However, in our case, for the PhantomX React Robot, based on a AVR microcontroller, we will work with the TimerOne library.
TimerOne library
Important: If you do not have a libraries folder inside your <ARDUINO_MAIN_DIRECTORY>, create it and move the content inside it.
Main Functions of TimerOne library
This Section provides important information for the Timer handling. It is based on the explanation of different methods, but it does not explain the complete set of methods. To get and exahustive description of all methods the TimerOne.h must be studied.
Main methods are as follows:
- void initialize(unsigned long period) : Initializes and set the timer to period in microseconds.
- void setPeriod(unsigned long period) : Set the times to period in microseconds.
- void attachInterrupt(void (*isr)()) : Attachs a function to the timer that is called every period .
- void start(void): Starts the timer.
- void stop(void): Stops the timer.
For example, if you want to initialize a function that operates every second and blinks a led you need to
#include <TimerOne.h> const int led = 13; // the pin with a LED int ledState = LOW; // El LED empieza apagado void setup(void) { pinMode(led, OUTPUT); Timer1.initialize(1000000); // 1 second period Timer1.attachInterrupt(ISR_Blink); // Activates the interrupt and links it to ISR_Blink Serial.begin(9600); } void loop(void) { } void ISR_Blink() { ledState = !ledState; }