Timer library in Arduino - timer

I'm doing a calculator with arduino 4x4 keypad and 16x2 LCD. I have to do timer-interupt for this project with timer library. I asked this Q before, but I did not know that I must use timer library. Here is my Q : If an user does not press one of the button on keypad for 30 secs, the calculator should close itself. How can I do that with timer library ?

Instead of using timer library, you could use the millis() function:
// setup
long int time;
void loop(){
// get input
if(input){
interpretInput(input); // For example
time = millis();
}
if(millis()>time+30000){ // if there is more than 30 seconds between now and the last input, then call your shutdown function
shutdown();
}
}

Related

How to wake Pic32 from sleep mode?

I am trying to keep PIC32 in sleep mode on boot up. And when the power button is pressed the Pic32 should exit the sleep mode and wake up.
I am able to put PIC32 in sleep mode but I am not sure how I can trigger it to wake up on button press.
Is it possible if I can trigger an interrupt whenever the user presses the power button and thus wake the PIC32?
I am using microchip harmony framework and am quite new to this can someone please suggest how I can achieve this?
To put PIC32 I am using the PowerEnterMode function of harmony. To wake up PIC32 I have tried using the watch dog timer following the sample project provided with microchip harmony but couldn't get it to work.
I also read that I can use external interrupt to set it up but I don't know how to set it up.
I have added my code below.
void APP_Initialize ( void )
{
DRV_ADC_Open();
DRV_ADC_Start();
PLIB_ADC_Enable(ADC_ID_1);
SPIHandle = DRV_SPI_Open(DRV_SPI_INDEX_0, DRV_IO_INTENT_READWRITE );
DelayMs(100);
SYS_DEVCON_PowerModeEnter(SYS_POWER_MODE_IDLE);
appData.state = APP_STATE_POWER_UP;
}
void APP_Tasks (void)
{
switch ( appData.state )
{
case APP_STATE_POWER_UP:
{
uint8_t pwrButton;
pwrButton = PWR_BTNStateGet() ;
if (npwrButton == 0) // If button is pressed
{
PwrButtonDebounce += 1; // Increment the pressed timer
DelayMs(10);
} else
{
PwrButtonDebounce = 0; // Clear the Debounce Timer
}
if (PwrButtonDebounce == MAX_DEBOUNCE) // Debounced....
{
// Here Wake up routine on button press
appData.state = APP_STATE_INIT;
}
}
}
I expect that whenever I press the power button the Pic32 should come in the APP_STATE_POWER_UP state and on debounce go in the initialization state but It never comes in this state.
Can someone suggest how I can set this up?
You should use Change Notification interrupt.
Enable the CN interrupt on the pin that you have your button and it will wake your device when pressed.

Implementing a delay using timers in STM32

Simply, I want to implement a delay function using stm32 timers, like the one in AVR microcontrollers "Normal mode". Can anybody help ? I just can't find that in the stm32 datasheet! It only supports PWM, input capture, output compare and one-pulse mode output!
N.B: I forgot to mention that I'm using stm32F401 microcontroller
You have very special timer for this purpose called SysTick. Set it to overflow every 1ms. In its handler
static volatile uint32_t counter;
void SysTick_Handler(void)
{
counter++;
}
inline uint32_t __attribute__((always_inline)) GetCounter(void)
{
return counter;
}
void Dealy(uint32_t ms)
{
uint32_t tickstart = GetCounter();
while((GetCounter() - tickstart) < ms);
}
If you want to be able to delay by periods shorter than milli-seconds
you can set up a timer to auto-reload and use the internal clock.
The internal clock frequency set to the timer is shown in the cube MX
clock tab. On my NUCLEO-F446RE most timers get 84MHz or 42MHz.
Timer 3 gets 42 MHz by default.
So if I set the pre scaler to 42 and the count period to maximum (0xFFFF
if 16 bit) I will have a cycling clock that changes every micro-second.
I can then, using a property of twos complement maths, simply subtract the
old time from the new to get the period.
void wait_us (int16_t delay) {
int16_t t1= htim3.Instance->CNT;
while (( htim3.Instance->CNT - t1 ) < delay) {
asm ("\t nop");
}
}
This is an old trick from PIC18 C coding

The program for LEDs 8x8 doesn't function in Arduino Uno. I'm not using the pinMode() method. I'm using the other method for specification

The program for LEDs 8x8 doesn't function in Arduino Uno. I'm not using the pinMode() method to specify pin OUTPUT for LEDs. I'm using the method "LedControl"
The specification pin:
"lc=LedControl(4,6,5,1);"
for setting the pin. Why ?
This program compiles and executes without problems.
I'm importing the library correctly.
I'm using this example:
#include "LedControl.h"
/*Now we need a LedControl to work with.
pin 4 is connected to the DataIn
pin 5 is connected to the CLK
pin 6 is connected to LOAD / CS
We only have a single MAX7219 */
LedControl lc=LedControl(4,6,5,1);
/* we always wait a bit between updates of the display */
unsigned long delaytime=500;
void setup() {
/* The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call */
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.clearDisplay(0);
}
void loop() {
lc.setIntensity(0,8);
single();
lc.clearDisplay(0);
}
/* This function will light up every Led on the matrix. The led will blink along with the row-number. row number 4 (index==3) will blink 4 times etc. */
void single() {
for(int row=0;row<8;row++) {
for(int col=0;col<8;col++) {
delay(50);
lc.setLed(0,row,col,true);
delay(50);
for(int i=0;i<col;i++) {
lc.setLed(0,row,col,false);
delay(50);
lc.setLed(0,row,col,true);
delay(50);
}
}
}
}
The LedControl library uses pinMode in the background to make the LEDs easier to control. When you give values to LedControl, it automatically uses pinMode, which operates as pinMode(pin, OUTPUT|INPUT).

Start a timer with a function in arduino

I have a project to control my projector via RS-232 commands, and the projector sends feedback to the arduino. Therefore I have a LCD screen with buttons. To make it nicer, I have an ultrasonic rangefinder that I want to use so that when you approach the device, the LCD backlight turns on for 30 seconds, then turns back off. I can't use a delay because I need to continue polling for buttons and serial information from the projector.
There are community libraries like Timer.h and SimpleTimer.h, but these only do oscillations, etc.
What I would like to do is this:
distance = measureUltrasonicDistance(ultrasonicPin); //returns in cm
if (distance <= 10) {
//digitalWrite(baclkightPin,HIGH);
//have this turn off 30 seconds later
}
This seems like a typical application for the event fuse library.
First thing to realize is that loop() is continuously invoked, and that you can use a "watchdog" pattern to keep the light on without using a timer.
unsigned long timeoff;
void setup() {
timeoff = millis();
}
void loop() {
distance = measureUltrasonicDistance(ultrasonicPin); //returns in cm
if (distance <= 10) {
digitalWrite(baclkightPin,HIGH);
// compute boundary of when light should be off
timeoff = millis() + 30L*1000L;
}
if (timeoff < millis()) {
digitalWrite(backlightPin, LOW);
}
}
Hope this helps, let me know if there are any problems.

How can I create interrupts in C for Arduino?

So I did this code for Arduino in C. It is for controlling a stepper motor. But every time I have to wait until the microcontroller begins a new loop so that it can take the value for the new variables, how can I create an interrupt so it will do it in any time of the program?
#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>
#include "IO/ioconfig.h"
#include "leebotones/leebotonesA.h"
#include "leebotones/leebotonesB.h"
#include "rutina/avanza.h"
#include "rutina/retrocede.h"
char variableA = 0;
char variableB = 0;
int main(void){
ioconfig();
while(1) {
if (leebotonesA()==1) {
variableA++;
} //Fin de if leebotonesA.
if (leebotonesB()==1) {
if (variableA==0) {
variableB=1;
}
else {
variableB=0;
}
}
if (variableA==2) {
variableA=0;
PORTD=0x00;
_delay_ms(10000);
} //Fin de if variableA.
if (variableA==1 && variableB==0) {
avanza();
} //Fin de if leebotonesA.
if (variableA==1 && variableB==1) {
retrocede();
}
_delay_ms(25);
}//End of while
}// End of main
A hardware interrupt on the Arduino occurs when one of the interrupt pins receives a change of state. The function to use, if you have access to the Arduino library, is attachInterrupt.
Example code to listen for an interrupt (derived from the documentation I linked to, I added comments to help explain):
// The Arduino has an LED configured at pin 13
int pin = 13;
// Holds the current state of the LED to handle toggling
volatile int state = LOW;
void setup()
{
pinMode(pin, OUTPUT);
// First Parameter:
// 0 references the interrupt number. On the Duemilanove, interrupt 0
// corresponds to digital pin 2 and interrupt 1 corresponds to digital pin
// 3. There are only two interrupt pins for the Duemilanove and I believe
// the Uno too.
// Second Parameter:
// blink is the name of the function to call when an interrupt is detected
// Third Parameter:
// CHANGE is the event that occurs on that pin. CHANGE implies the pin
// changed values. There is also LOW, RISING, and FALLING.
attachInterrupt(0, blink, CHANGE);
}
void loop()
{
// Turns the LED on or off depending on the state
digitalWrite(pin, state);
}
void blink()
{
// Toggles the state
state = !state;
}
There is also a concept of Pin Change Interrupts that is supported on every pin. See the bottom part of introduction to interrupts for more info.
However, sometimes a hardware interrupt can be avoided by refactoring your code. For example, keep your loop() running quickly --- mostly just reading inputs, limit the use of delay() --- and in the loop, call a function when the targeted inputted value is detected.
MSTimer2 is an Arduino library function to let you set timer interrupts.
Another alternative to delay, rather than interrupts, is to set a timer and check it each time through the loop. http://arduino.cc/en/Tutorial/BlinkWithoutDelay explains the concepts. The Metro library http://arduino.cc/playground/Code/Metro implements this and is easy to use. I've used it instead of delay() so that I can check for a button push while my robot is moving.

Resources