jonahpearl
New member
Hi all -- I'm hoping to have an elapsedMillis variable be reset inside of an interrupt callback, like this:
When I try to declare the elapsedMillis as volatile, I get an error that seems to indicate that comparison operation isn't defined for this combination of variable types.
Would it be an easy fix to define the relevant operations, or is there something more deeply complicated about defining ops for volatile vars / is there something wrong about the way I've declared the vars?
(I suspect that declaring the elapsedMillis as volatile is overkill for my particular application, i.e. since I give it many msec of buffer anyways, it won't matter if it gets updated one loop iteration late. But I'm curious because I could imagine cases where it might matter to be able to quickly update a timer based on an incoming trigger!)
Code:
const int trigger_pin = 17; // listens for trigger
volatile elapsedMillis sinceTrigger;
volatile long int trigger_count = 0;
const int min_offset_from_trigger_msec = 10;
const int max_offset_from_trigger_msec = 30;
const bool various_conditions = true;
void setup() {
Serial.begin(9600);
pinMode(trigger_pin, INPUT);
digitalWrite(trigger_pin, HIGH);
attachInterrupt(digitalPinToInterrupt(trigger_pin), countTriggerOne, RISING);
}
void loop() {
// put your main code here, to run repeatedly:
if (
(various_conditions) and
(sinceTrigger >= min_offset_from_trigger_msec) and
(sinceTrigger <= max_offset_from_trigger_msec)
){
do_something();
}
}
void do_something(){
Serial.println("foo");
}
void countTriggerOne()
{
sinceTrigger = 0;
trigger_count++;
}
When I try to declare the elapsedMillis as volatile, I get an error that seems to indicate that comparison operation isn't defined for this combination of variable types.
Code:
volatile_elapsedMillis_example:24: error: no match for 'operator>=' (operand types are 'volatile elapsedMillis' and 'const int')
(sinceTrigger >= min_offset_from_trigger_msec) and
^
/Users/jonahpearl/Documents/Arduino/volatile_elapsedMillis_example/volatile_elapsedMillis_example.ino:24:19: note: candidate: operator>=(long unsigned int, int) <built-in>
/Users/jonahpearl/Documents/Arduino/volatile_elapsedMillis_example/volatile_elapsedMillis_example.ino:24:19: note: conversion of argument 1 would be ill-formed:
/Users/jonahpearl/Documents/Arduino/volatile_elapsedMillis_example/volatile_elapsedMillis_example.ino:24:22: warning: passing 'volatile elapsedMillis' as 'this' argument discards qualifiers [-fpermissive]
(sinceTrigger >= min_offset_from_trigger_msec) and
^
In file included from /private/var/folders/x7/04jny8k55mv6gr_jtz4kh05c0000gn/T/AppTranslocation/72E6703A-E80D-43F8-985A-CA1173953BBF/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/WProgram.h:71:0,
from /var/folders/x7/04jny8k55mv6gr_jtz4kh05c0000gn/T/arduino_build_371018/pch/Arduino.h:6:
/private/var/folders/x7/04jny8k55mv6gr_jtz4kh05c0000gn/T/AppTranslocation/72E6703A-E80D-43F8-985A-CA1173953BBF/d/Teensyduino.app/Contents/Java/hardware/teensy/avr/cores/teensy3/elapsedMillis.h:42:2: note: in call to 'elapsedMillis::operator long unsigned int() const'
operator unsigned long () const { return millis() - ms; }
Would it be an easy fix to define the relevant operations, or is there something more deeply complicated about defining ops for volatile vars / is there something wrong about the way I've declared the vars?
(I suspect that declaring the elapsedMillis as volatile is overkill for my particular application, i.e. since I give it many msec of buffer anyways, it won't matter if it gets updated one loop iteration late. But I'm curious because I could imagine cases where it might matter to be able to quickly update a timer based on an incoming trigger!)