Making a timer in Arduino whilst checking for input - arduino-uno

I need to wait for a period of time while checking whether a button is pressed (so whether an input is HIGH or LOW).
The delay function is annoying to use for this because it cannot check whether something is happening while being delayed, so it would have to wait for 1 ms, check, wait, check, wait, check etc...
Can you help me with the coding I would need to check and pause for a set amount of time, at the same time?

You can realize that with a second condition-controlled loop.
If you want to wait in each arduino main loop as an example for 20 seconds and execute in this time span further code you can do this as follows:
unsigned long startTime = millis(); // Number of milliseconds since the program started (unsigned long)
unsigned long intervalTime = 20000UL; // equals 20 seconds
int buttonPin = 3; // used button pin
void loop()
{
while(millis() - startTime < intervalTime){
if(digitalRead(buttonPin)==HIGH){
//...
}
else {
//...
}
}
//...
}

Related

lose RTC after restart (no battery)

in the ESP-IDF doc about RTC, it says:
This timer allows time keeping in various sleep modes, and can also persist time keeping across any resets
I added the below lines to my main function to test it, but every time after rebooting the ESP(not a hard reset), Each time the code inside the if block is executed.
struct timeval current_time;
// printf("seconds : %ld\nmicro seconds : %ld", current_time.tv_sec, current_time.tv_usec);
struct timeval d_time;
d_time.tv_sec = 1000;
d_time.tv_usec = 120;
struct timezone tz;
gettimeofday(&current_time, NULL);
if (current_time.tv_sec < 100) {
tz.tz_minuteswest = 210;
tz.tz_dsttime = DST_NONE;
settimeofday(&d_time, &tz);
}
so, what should I do to make it work, I couldn't the problem.
thank you.

TIM2 module not ticking at 1us in STM8S103F3 controller

I created a program on STM8S103F3 to generate a delay in rage of micro seconds using TIM2 module, but the timer is not ticking as expected and when I tried to call 5 sec delay using it, it is giving around 3 sec delay. I'm using 16MHz HSI oscillator and timer pre-scalar is set to 16. please see my code below. Please help me to figure out what is wrong with my code.
void clock_setup(void)
{
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
}
void delay_us(uint16_t us)
{
volatile uint16_t temp;
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_16, 2000); //Prescalar value 8,Timer clock 2MHz
TIM2_Cmd(ENABLE);
do{
temp = TIM2_GetCounter();
}while(temp < us);
TIM2_ClearFlag(TIM2_FLAG_UPDATE);
TIM2_Cmd(DISABLE);
}
void delay_ms(uint16_t ms)
{
while(ms--)
{
//delay_us(1000);
delay_us(1000);
}
}
It is better to use a 10us time base to round the delays. Well in order to achive a 10us timebase, if you use 16MHz master clock and you prescale TIM2 by 16, then you get a 1 us increment time, right? But we want TIM2 to overflow to generate an event named 16us event. Since we know that the timer will increment every 1us, if we use a reload value 65536 - 10 = 65526, this will give us a 10us overflow hence, 10us time base. If we are ok until here in delay code we'll just check the TIM2 update flag to know whther it has overflowed or not. See the example code snippet below.
// Set up it once since our time base is a fixed 10us
void setupTIM2(){
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_16, 65526); //Prescalar value 16,Timer clock 1MHz
}
void delay_us(uint16_t us)
{
volatile uint16_t temp;
TIM2_Cmd(ENABLE);
const uint16_t count = us / 10; //Get the required counts for 10us time base
// Loop until the temp reaches the required count value
do{
while(TIM2_GetFlagStatus(TIM2_FLAG_UPDATE) == RESET); //Wait for the TIM2 to overflow
TIM2_ClearFlag(TIM2_FLAG_UPDATE); // Clear the overflow flag
temp++;
} while(temp < count);
TIM2_Cmd(DISABLE);
}
void delay_ms(uint16_t ms)
{
while(ms--)
{
//delay_us(1000);
delay_us(1000);
}
}
void main(void){
...
setupTIM2();
...
delay_ms(5000);
}

Trigger Countdown with 433 MHz transmission

I would like to use an arduino to read 433 MHz transmission from multiple Soil Moisture Sensors. Since I can never be sure all transmissions reach the receiver I'd like to set a countdown from the moment the first transmission is received. If another transmission is received, the countdown starts again.
After a defined amount of time (e.g. 10 Minutes) without any more signals or if all signals have been received (e.g 4 Sensors) the receiving unit should stop and come to decision based on the data it got to the point.
For transmitting and receiving I am using the <RCSwitch.h>library.
The loop of the receiving unit and one Sensor looks like this:
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void Setup(){
Serial.begin(9600);
mySwitch.enableReceive(4);
}
void loop() {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
lcd.clear();
Serial.print("Unknown encoding");
}
else {
Serial.print(mySwitch.getReceivedValue());
Serial.print("%");
}
The full code includes some differentiation mechanism for all sensors but I figured that might not be relevant for my question.
Question:
What's the best way to do this without a real time clock module. As far as I know I can't wait by using delay(...)since then I won't receive any data while the processor waiting.
You can use millis() as a clock. It returns the number of milliseconds since the arduino started.
#define MINUTES(x) ((x) * 60000UL)
unsigned long countStart = 0;
void loop()
{
if (/*read from module ok*/)
{
countStart = millis();
// sanity check, since millis() eventually rolls over
if (countStart == 0)
countStart = 1;
}
if (countStart && ((millis() - countStart) > MINUTES(10)))
{
countStart = 0;
// trigger event
}
}
Arduino's internal timers can also be used in this situation. If a long time period is needed, it's better to use 16bit counter (usually timer1) at 1024 prescaler (largest available). If the largest time interval of timer is greater than time required, then a counter have to be added in order to keep track of 1 minute interval.
For example, for 1-minute interval, initialize registers as:
TCCR1A = 0; //Initially setting every register as 0x0000
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 468750; // compare match register 16MHz/1024/2/frequency(hz)
TCCR1B |= (1 << WGM12); // Timer compare mode
TCCR1B |= (1 << CS10) | (1 << CS10); // 1024 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
These configuration of timer will give interrupt time of 1 minute. And upon timer completion ISR TIMER1_COMPA_vect will be run. You can play around with value of OCR1A for different interrupt periods.
Main advantage of using interrupts is that they don't block any task and can will be executed instantaneously (if interrupts are not disabled explicitly).

AlarmRepeat function in Arduino

I am using the Time.h and TimeAlarms.h libraries in Arduino. I try to call a function on specific times of the day (every day). The function is called on the first day, but then on the next day it seems like the alarm ceases to work, although it should repeat every day. Any ideas what's wrong with my sketch?
#include <Time.h>
#include <TimeAlarms.h>
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by Unix time_t as ten ASCII digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
// The constant variables used in the code:
const unsigned long shock_delay = 10; // shock stimulus duration (s)
// Edit below only if you make changes to the hardware configuration:
const int tonePin = 13; // the number of the LED pin
const int shockPin = 12; // the number of the shocker pin
const int buzzerOut = 8; // the number of the tone producer
const int trialButton=2; // the number of the trial button
const int controlButton=3; // the number of the control button
// Define the variables that will change in the code
unsigned int Interval1; // the interval between the beginning of the hour and the first footshock
unsigned int Interval2; // the interval between the first and the second footshock
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
setTime(23,59,50,10,4,15); // The current time (HH,MM,SS,DD,MM,YY)
// Define the position of the different digital pins
pinMode(tonePin, OUTPUT); // The tone LED (red)
pinMode(shockPin, OUTPUT); // The shock output, which will coincide with green LED
pinMode(trialButton, INPUT); // The trial input button
pinMode(controlButton, INPUT); // The control input button
Alarm.alarmRepeat(20,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(21,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(22,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(23,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(0,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(1,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(2,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(3,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(4,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(5,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(6,00,0,RandomShock); // Initiate the user defined RandomShock function
Alarm.alarmRepeat(7,00,0,RandomShock); // Initiate the user defined RandomShock function
}
void loop(){
// Create a message that gives the current time and date on the monitor
if(Serial.available() )
{
processSyncMessage();
}
if(timeStatus() == timeNotSet)
Serial.println("waiting for sync message");
else
digitalClockDisplay(); // The function that calls on the time display
Alarm.delay(1000); // Delay of 1 minute between time display
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void processSyncMessage() {
// if time sync available from serial port, update time and return true
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of header & 10 ASCII digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
setTime(pctime); // Sync Arduino clock to the time received on the serial port
}
}
}
void RandomShock () {
Interval1 = random(0,60); // Random value between 0 and 59 [min]
digitalWrite(tonePin,HIGH); // Indicate the shock program is ON by the red LED
Alarm.delay(Interval1*60000); // Wait for the duration of the first interval
digitalWrite(tonePin,LOW); // Turn the red LED OFF
digitalWrite(shockPin, HIGH); // Apply the first shock (green LED will turn ON)
Serial.println("Applying an electeric shock at:"); // Write a message indicating a shock is applied
digitalClockDisplay(); // Display the time during which the shock was applied
Alarm.delay(shock_delay*1000); // The duration of the shock [10 seconds]
digitalWrite(shockPin, LOW); // Terminate the first shock (green LED will turn OFF)
Interval2 = random(0,(61-Interval1)); // Randomly asign a value to the second interval [min]
digitalWrite(tonePin,HIGH); // Indicate the shock program is ON by the red LED
Alarm.delay(Interval2*60000-20000); // Wait for the duration of the second interval
digitalWrite(tonePin,LOW); // Turn the red LED OFF
digitalWrite(shockPin,HIGH); // Apply the second shock (green LED will turn ON)
Serial.println("Applying an electeric shock at:"); // Write a message indicating a shock is applied
digitalClockDisplay(); // Display the time during which the shock was applied
Alarm.delay(shock_delay*1000); // The duration of the shock [10 seconds]
digitalWrite(shockPin,LOW); // Terminate the second shock
digitalWrite(tonePin,HIGH); // Indicate the shock program is ON by the red LED
Alarm.delay((60-Interval1-Interval2)*60000); // Wait until the hour is completed
digitalWrite(tonePin,LOW); // Turn the red LED OFF
}
There are two problems with the sketch that I wrote:
Alarm.alarmRepeat function does not seem to be able to call my
function exactly at midnight (00:00:00), however, if I schedule it
to any other time (e.g., 00:00:01) it works just fine.
There is a limit to the number of alarms you can schedule: the
maximum number of alarms is 6. The solution is either to change this threshold, or to reduce the number of alarms.
After correcting for these two issues the sketch work smoothly.
Seems that TimeAlarm library is unable to set an alarm to midnight
https://github.com/PaulStoffregen/TimeAlarms/issues/3
Try this pull request https://github.com/PaulStoffregen/TimeAlarms/pull/4
If you need more than 6 alarms just look for that at TimeAlarms.h on the library:
#if defined(__AVR__)
#define dtNBR_ALARMS 6 // max is 255
#else
#define dtNBR_ALARMS 12 // assume non-AVR has more memory
#endif
and change it to your need (example:24)
#if defined(__AVR__)
#define dtNBR_ALARMS 24 // max is 255
#else
#define dtNBR_ALARMS 12 // assume non-AVR has more memory
#endif

Android java development time of acceleration to

How can I calculate the time of acceleration to 100kmh?
Well, I registered a location listener when the !location.hasSpeed() is true store the time of location into a variable. When the speed is reach of the given speed in this case 100km/h (27.77 m/s) I substract from the spped of location and the result I divide by 1000.
Here is the "pseudo code"
#Override
public void onLocationChanged(Location currentLoc) {
// when stop reseted, when start reset again
if (isAccelerationLoggingStarted) {
if (currentLoc.hasSpeed() && currentLoc.getSpeed() > 0.0) {
// dismiss the time between reset to start to move
startAccelerationToTime = (double) currentLoc.getTime();
}
}
if (!currentLoc.hasSpeed()) {
isAccelerationLoggingStarted = true;
startAccelerationToTime = (double) currentLoc.getTime();
acceleration100 = 0.0;
}
if (isAccelerationLoggingStarted) {
if (currentLoc.getSpeed() >= 27.77) {
acceleration100 = (currentLoc.getTime() - startAccelerationToTime) / 1000;
isAccelerationLoggingStarted = false;
}
}
}
The main problem i see here, is that whenever the device is moving, startAccelerationToTime is reset. (The first if only checks whether there's movement; it doesn't check whether there's already a start time recorded.
I don't see where isAccelerationLoggingStarted is needed at all -- the speed, and the variables themselves, can be cleaned up a bit to make it clear what the next step should be.
Your pseudocode probably ought to look something like:
if speed is 0
clear start time
else if no start time yet
start time = current time
clear acceleration time
else if no acceleration time yet, and if speed >= 100 mph
acceleration time = current time - start time
In Java, that'd look like...
long startTime = 0;
double accelerationTime = 0.0;
#Override
public void onLocationChanged(Location currentLoc) {
// when stopped (or so slow we might as well be), reset start time
if (!currentLoc.hasSpeed() || currentLoc.getSpeed() < 0.005) {
startTime = 0;
}
// We're moving, but is there a start time yet?
// if not, set it and clear the acceleration time
else if (startTime == 0) {
startTime = currentLoc.getTime();
accelerationTime = 0.0;
}
// There's a start time, but are we going over 100 km/h?
// if so, and we don't have an acceleration time yet, set it
else if (accelerationTime == 0.0 && currentLoc.getSpeed() >= 27.77) {
accelerationTime = (double)(currentLoc.getTime() - startTime) / 1000.0;
}
}
Now, i'm not sure exactly how location listeners work, or how often they notify you when you're moving. So this may only semi work. In particular, onLocationChanged might not get called when you're not moving; you may need to request an update (perhaps via a "reset" button or something) or set certain params in order to trigger the stuff that happens when speed == 0.

Resources