@ecabanas, TimerOne is being used as a simple 500ms (500,000us) timer. You can replace the TimerOne library and code with a Software Timer set to a 500ms interval with the callback being isr_timer(). Very straight forward.
sorry, but If I try to compile the arduino code I receive an answer like this.
I’m triying to find TimerOne in Particle’s Library but i did not find it
Correct, you only need to state the pin, but you need to check whether your used pin as interrupt source.
On the Particle devices it’s also prefered to use the pin names as printed on the device D2 instead of just 2. https://docs.particle.io/reference/firmware/electron/#attachinterrupt-
Unfortunately there is no explicit list of “available” pins for the Electron, so D2 might pose a problem.
It shares its EXTI line with A0, A3 and maybe MODE (I couldn’t find any info in the docs - have to look in the sources) and since MODE already uses an interrupt you can’t reuse the same line.
So if you experience problems with the interrupt try a different pin for the interrupt.
Looks fine to me. Does anything not work that way?
Just about your map() function.
Direction = map(VaneValue, 0, 1023, 0, 360);
The Particle devices sport a 12bit ADC so you’d map the raw readings of 0…4095 to your actual value.
And the allowed voltage range is 0…3.3V not 5V - take care.
When using #include "Arduino.h"sei() and cli() should be available but both ways should work alike.
Just one thing to be careful about. While an Arduino doesn’t do a lot else than running your code, Particle devices do other stuff while not occupied with your code (e.g. cloud house keeping) which might get affected by disabling interrupts for prolonged periods.
If you are not using SYSTEM_THREAD(ENABLED)delay() does (try to) attend to the cloud every second (unless you prevent it by disabling interrupts) and with multi threading disabling interrupts might have even more impact.
When writing code purely for Particle devices, I’d stick with the “native” syntax.
But if you want to write portable code you can use the Arduino way of writing it. On Particle devices (with more recent system versions) digitalPinToInterrupt is defined like this (or similar)
#define digitalPinToInterrupt(_pin) _pin
So it just uses the _pin parameter as is, while on Arduinos the pins need to be mapped to some other number, due to a more limited numer of available interrupts.
As for SYSTEM_THREAD(ENABLED) this only exitst for Particle devices, so that won’t port to Arduinos, but for the Electron I’d use it.
If I use sei(); and cli(); instead of interrupts(); and noInterrupts(); my Electron behaves strangely… It connects to to the cloud (breathing blue) but since I upload the code I’m unable to flash a new code (over OTA). So I can only flash the code over USB. Also the code isn’t running. Any ideas?