My goal is to attach two sensors (about 30cm apart) to two separate pins, each attached to an interrupt, so that I can calculate the speed an object travels between the two sensors.
Since I'm relatively new to the ESP (and C++ programming), I'm first trying a proof of concept using a Wemos board, connected to a single 1-Button shield and a NeoPixel-shield.
For this proof of concept, I'm using the 1-button shield positions (up and down) to simulate the two sensors; in the end result this will be replaced by two sensors and two interrupts in code.
This is the code I use:
extern "C"{
#include "user_interface.h"
}
#include <Adafruit_NeoPixel.h>
#define LED_PIN 4 // D2
#define SWITCH_PIN 0 // D3
boolean triggered;
char line[40];
volatile uint32 sw_up;
volatile uint32 sw_down;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, LED_PIN, NEO_GRB + NEO_KHZ800);
void switching() {
if (digitalRead(SWITCH_PIN) == LOW) {
sw_up = 0;
sw_down = system_get_time(); // millis();
} else {
sw_up = system_get_time(); // millis();
}
}
void setup() {
sw_up = 0;
sw_down = 0;
triggered = false;
Serial.begin(115200);
Serial.setDebugOutput(false);
Serial.println("Initializing switch");
pinMode(SWITCH_PIN, INPUT_PULLUP);
Serial.println("Initializing interrupts");
attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), switching, CHANGE);
Serial.println("Initializing NeoPixel library...");
pixels.begin(); // This initializes the NeoPixel library.
pixels.setPixelColor(0, pixels.Color(0, 0, 255));
pixels.show();
}
void loop() {
if (sw_down > 0) {
if (sw_up > 0) {
Serial.println("Switch is up");
Serial.print("Trigger time is: ");
int time_taken = sw_up - sw_down;
Serial.println(time_taken);
pixels.setPixelColor(0, pixels.Color(0, 0, 255));
pixels.show();
sw_up = 0;
sw_down = 0;
triggered = false;
} else if (!triggered) {
Serial.println("Switch is down");
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
pixels.show();
triggered = true;
}
}
}
If I press the button the NeoPixel turns RED and when I release the button the NeoPixel turns GREEN.
Also the state of the button is printed correctly onto the console and when the button is depressed, the time taken between press and depress *should* be shown (time_now - time_then = time_taken).
I say *should* because all I get is some, seamingly, random numbers as a result:
Initializing switch
Initializing interrupts
Initializing NeoPixel library...
Switch is down
Switch is up
Trigger time is: 656218
Switch is down
Switch is up
Trigger time is: 179
Switch is down
Switch is up
Trigger time is: 39923
Switch is down
Switch is up
Trigger time is: 39900
Switch is down
Switch is up
Trigger time is: 533833
Switch is down
Switch is up
Trigger time is: 58324
Switch is down
Switch is up
Trigger time is: -22804555
I've tried using millis(), micros() and other time related functions, but they all seem to return unlogical bogus times.
I remember having read once that certain time functions can't be used inside an interrupt, but I can't seem to find that information anymore.
Great chance I'm just making a simple programming error, but I don't see it at the moment.
Can anyone point me in the right direction?
Greetz,
Peter