How do I run a state machine once in an Arduino loop - arduino

I am attempting to photograph water drop collisions using two solenoids. I have successfully done this using one solenoid. This used setting delays in the code to time the camera shutter, water drops and flash. I would like to trigger two solenoids at the same time with identical delays but not sure how to do this.
The order of operation should be
Open the camera shutter (camera is in bulb mode)
Drop two water drops from two solenoids at the same time.
Fire the flash
Close the camera shutter
My platform is an Atmega 2560. Here is the code I have so far,
int relayCam=A0; //pin to relay to trigger camera
int relayFlash=A7 ; //pin to relay to fire flash
int button1Read;
//button read delays (settling time)
int button1DT=250;
int button1Pin=8; //pin for 2 drop button read
int DelayCamOpen=100; //wait time to trigger camera
int DelayCamClose=1000; //wait time to close camera shutter
//Drop start
int DropStart=500; //time to start first drop
//Flash timing
int DelayFlashFire1=350;
int DelayFlashOff=50; //turn flash off
bool runOnce = false;
class Solenoid
{
int SolenoidPin;
long OnTime;
long OffTime;
int SolenoidState;
unsigned long previousMillis;
public:
Solenoid(int pin, long on, long off)
{
SolenoidPin = pin;
pinMode(SolenoidPin, OUTPUT);
OnTime = on;
OffTime = off;
SolenoidState = HIGH;
previousMillis = 0;
}
void update()
{
unsigned long currentMillis = millis();
if((SolenoidState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
SolenoidState = HIGH;
previousMillis = currentMillis;
digitalWrite(SolenoidPin, SolenoidState);
}
else if ((SolenoidState == LOW) && (currentMillis - previousMillis >= OffTime))
{
SolenoidState = LOW;
previousMillis = currentMillis;
digitalWrite(SolenoidPin, SolenoidState);
}
}
};
Solenoid drop1(A1, 55, 55);
Solenoid drop2(A2, 55, 55);
void setup()
{
pinMode(relayCam, OUTPUT);
pinMode(relayFlash, OUTPUT);
pinMode(button1Pin, INPUT);
}
void loop()
{
//This part works
button1Read=digitalRead(button1Pin);
delay (button1DT);
if(button1Read==0) {
delay (DelayCamOpen); //wait time after button is pushed to start sequence
digitalWrite (relayCam,LOW); //opens the camera shutter
delay (DropStart); //wait time after shutter opens ro start first drop
//This part does not execute
if (runOnce == false)
{runOnce = true;
drop1.update();
drop2.update();
}
//This part works
delay (DelayFlashFire1); //wait time to fire flash
digitalWrite (relayFlash,LOW); //fire flash
delay (DelayFlashOff); //time to keep flash open (very short time)
digitalWrite (relayFlash,HIGH); //close flash relay
delay (DelayCamClose); //wait time to close camera shutter
digitalWrite (relayCam,HIGH); //close camera shutter
}
else if(button1Read==1)
{
digitalWrite (drop1,HIGH); //no signal to waterdrop
digitalWrite (drop2,HIGH); //no signal to waterdrop
digitalWrite (relayCam,HIGH); //no signal to camera
digitalWrite (relayFlash,HIGH); //no signal to flash
}
}
The camera and flash portion is working but the solenoids do not. I'm not sure if I'm even going about this correctly. I do know that having delays to run two solenoids is not giving me the desired results, hence trying to use the state machine method. Any help/suggestions is greatly appreciated. I am new to writing code and all things Arduino.
This is the code that works with one solenoid.
//Pin assignments
int relayCam=A0; //pin to relay to trigger camera
int relayFlash=A7 ; //pin to relay to fire flash
int relayWD1=A1; //pin to relay to trigger water drop 1
int button1Read;
//button read delays (settling time)
int button1DT=250;
int button1Pin=8; //pin for 2 drop button read
//drop sizes
int dropSize1=55; //time to hold relay for 1st drop
int dropSize2=45; //time to hold relay for 2nd drop
//delays betwween drops
int Delay1=45; //time between 1st and 2nd drops
//camera shutter timings
int DelayCamOpen=100; //wait time to trigger camera
int DelayCamClose=1000; //wait time to close camera shutter
//Drop start
int DropStart=500; //time to start first drop
//Flash timing
int DelayFlashFire1=350;
int DelayFlashOff=50; //turn flash off
void setup() {
pinMode(relayCam, OUTPUT);
pinMode(relayWD1, OUTPUT);
pinMode(relayFlash, OUTPUT);
pinMode(button1Pin, INPUT);
}
void loop() {
button1Read=digitalRead(button1Pin);
delay (button1DT);
if(button1Read==0) {
delay (DelayCamOpen); //wait time after button is pushed to start sequence
digitalWrite (relayCam,LOW); //opens the camera shutter
delay (DropStart); //wait time after shutter opens ro start first drop
digitalWrite (relayWD1,LOW); //starts first drop
delay (dropSize1); //duration of water flow
digitalWrite (relayWD1,HIGH); //stops first drop
delay (Delay1); //wait time for second drop to start
digitalWrite (relayWD1,LOW); //start of second drop
delay (dropSize2); //duration of waterflow
digitalWrite (relayWD1,HIGH); //stops second drop
delay (DelayFlashFire1); //wait time to fire flash
digitalWrite (relayFlash,LOW); //fire flash
delay (DelayFlashOff); //time to keep flash open (very short time)
digitalWrite (relayFlash,HIGH); //close flash relay
delay (DelayCamClose); //wait time to close camera shutter
digitalWrite (relayCam,HIGH); //close camera shutter
}
else if(button1Read==1)
{digitalWrite (relayWD1,HIGH); //no signal to waterdrop
digitalWrite (relayCam,HIGH); //no signal to camera
digitalWrite (relayFlash,HIGH); //no signal to flash
}
}

The abstraction represented by your update() method is tortured and confusing to me. My suggestion is to replace update() with two methods startDrop() and stopDrop(). startDrop() records a startTimestamp (like you've done with currentMillis but a class member variable) and opens the solenoid. stopDrop() compares the elapsed time and returns false if not enough time has passed and the solenoid remains open, or returns true if enough time has passed and the solenoid is closed.
class Solenoid
{
...
private unsigned long startTimestamp;
void startDrop()
{
startTimestamp = millis();
digitalWrite(SolenoidPin, LOW);
}
bool stopDrop()
{
if ((millis() - startTimestamp) >= OffTime)
{
digitalWrite(SolenoidPin, HIGH);
return true;
}
return false;
}
...
}
void loop()
{
...
drop1.startDrop();
drop2.startDrop();
while (drop1.stopDrop() == false);
while (drop2.stopDrop() == false);
...
}
That's just a rough draft. You might want to add some error checking to ensure that, for example, the solenoid was closed before startDrop() or that startDrop() was called before stopDrop().

Related

Changing blinking duration of LED by using 2 pushbuttons and arduino

I'm trying to make an arduino UNO circuit that allows me to set the blinking duration of an LED with two pushbuttons, but I'm having trouble with the program. First of all, the default blinking duration is 0,5 s. And I want to program the first pushbutton to be able to extend the blinking duration by 0,1 seconds, whereas the second one is for speeding up the duration by 0,1 seconds.
So in my current code, I use if statements to check whether the two buttons are pressed or not. If the inc button is pressed, the program should increase the duration by 100 ms, whereas when dec button is pressed, the program should decrease the duration by 100 ms.
However when I run it on the arduino circuit, the duration is stuck in 600 and 500. So in every loop, the program adds 100 ms to the duration time and then decreases it again by 100, even when I do nothing to the buttons.
Here's my code so far:
const int led = 7;
const int buttonUp = 6;
const int buttonDown = 5;
int duration = 500;
void setup(){
pinMode(led, OUTPUT);
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
Serial.begin(9600);
}
void loop(){
int inc = digitalRead(buttonUp);
int dec = digitalRead(buttonDown);
if(inc == HIGH){
duration += 100;
Serial.println(duration);
}
if(dec == HIGH){
duration -= 100;
if(duration < 0){
duration = 100;
}
Serial.println(duration);
}
digitalWrite(led, HIGH);
delay(duration);
digitalWrite(led, LOW);
delay(duration);
}
the code and circuit
serial monitor
Will be extremely grateful if anyone can point out any mistakes!! Thank you!
duration is 500ms. So you basically poll your button states once every second. The time window where you can detect a button click is very short compared to the time you cannot detect it. So chances that you register a click are very little. You need to push the button for at least a second to capture the signal every time.
If you push both buttons you add and subtract 100. That's a total change of 0. What do you expect?
This can be avoided by checking your button state more frequently or by using interrupts.
Find out how to use non-blocking delays and interrupts. The internet is full of tutorials.
bool blink(unsigned int duration) {
// demo code only
// (usable only once per sketch, due to static variables)
static unsigned long last;
static bool state;
if (millis() - last >= duration) {
last = millis();
state = ! state;
}
return state;
}
This is the BlinkWithoutDelay pattern. Usage:
digitalWrite(led,blink(duration));
Button handling is trickier than a beginner thinks: often you want to detect a state change between pressed and released, and you want to ignore the bouncing of a mechanical switch happening during a state change. Or you want to repeat some action, when the button is pressed for a long time.
Easiest you do a little delay(10); after detecting a state change to pass the bouncing time. Or look for libraries doing all that button handling.
Or do such a lazy delay anyway, to slow down your fast microcontroller below button bouncing speed.
const int led = 7;
const int buttonUp = 6;
const int buttonDown = 5;
void setup(){
pinMode(led, OUTPUT);
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
Serial.begin(9600);
}
unsigned int duration = 500;
int lastbutton = 0; // 0 / -100 / +100 to detect button changes
void loop(){
bool inc = digitalRead(buttonUp);
bool dec = digitalRead(buttonDown);
delay(5); // debounce
if( (inc || dec) && lastbutton == 0){
lastbutton = (inc - dec) * 100;
duration += lastbutton;
if (duration == 0) duration = 100; // Minimum
Serial.println(duration);
}
if (lastbutton != 0 && !inc && !dec) lastbutton = 0;
digitalWrite(led, blink(duration) );
}

Reading Speed with 2 Photoelectric sensors with Arduino and show speed after reading

I have 2 photo electric sensors with NPN output: Looking at the data sheet it might actually be LOW (page 14 NPN Output): https://mouser.com/datasheet/2/307/e3fb_photoelectric-1189416.pdf page 14.
The 2 sensors are placed 152.4 mm apart. Basically I want to start a timer when the first gate is triggered and finish when the second gate is triggered and then divide the set distance (152.4 mm) by the time to travel between each gate and output the speed in meters second. The sensors are powered by a 24 VDC power supply, the NPN output voltage can be as high as 3V DC from the collector. I first tested out the sensor to trigger the onboard LED to blink when an object is present in front of the sensor and was able to trigger the LED:
const int sensorPin = 3; // photoelectric sensor
const int ledPin = 13;
boolean state1 = LOW; // state at startup
void setup() {
pinMode(sensorPin, INPUT_PULLUP); // sensor input with internal pullup
resistor enabled
pinMode(ledPin, OUTPUT); // initialize digital pin 13 as an output.
}
void loop() {
state1 = digitalRead(sensorPin);
if (state1 == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
So it looks like the the voltage is in the right range to be read as HIGH or LOW.
So next I try to read the speed.
The code is below:
const int PE_01 = 2;
const int PE_02 = 3;
unsigned long start, finish, speed;
float elapsed;
boolean state1 = LOW; //state at startup
boolean state2 = LOW; //state at startup
void setup() {
// Initialize the serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(PE_01, INPUT_PULLUP); //sensor input with internal pullup
resistor enabled
pinMode(PE_02, INPUT_PULLUP); //sensor input with internal pullup
resistor enabled
}
void loop() {
state1 = digitalRead(PE_01);
state2 = digitalRead(PE_02);
// time = millis ();
if (state1 == HIGH){
start = millis();
}
if (state2 == HIGH) {
finish = millis();
}
elapsed = finish - start;
speed = 76200/elapsed; // unit distance (micro meters) and time (milli
seconds)
Serial.print("initial time (ms) = ");
Serial.println(start);
Serial.print("final time (ms) = ");
Serial.println(finish);
Serial.print("Speed of object (M/s) = ");
Serial.println(speed);
delay(500);
}
what i see in the serial monitor however is:
10:31:07.632 -> initial time (ms) = 15502
10:31:07.666 -> final time (ms) = 15502
10:31:07.666 -> Speed of object (M/s) = 4294967295
10:31:08.109 -> initial time (ms) = 16002
10:31:08.144 -> final time (ms) = 15502
10:31:08.178 -> Speed of object (M/s) = 0
The 4294967295 shows up when there is no object present and when there is it seems to output 0. I've adjusted the equation a few times but I don't seem to get anything that makes sense. Any help is greatly appreciated. Thank you!
The logic of code doesn't seem right.
From your explanation if you sure that trigged state is HIGH my code will be like this.
void loop() {
if (digitalRead(PE_01)== HIGH){// Check if first sensor trigged
unsigned long start = millis();
while(digitalRead(PE_02)== LOW);// Wait until second sensor trigged
unsigned long finish = millis();
float speed = 76200.0f/float(finish - start );
Serial.print("initial time (ms) = ");
Serial.println(start);
Serial.print("final time (ms) = ");
Serial.println(finish);
Serial.print("Speed of object (M/s) = ");
Serial.println(speed);
delay(500);
}
Edit
If your sensor are active low reverse all logic.
void loop() {
if (digitalRead(PE_01)== LOW){// Check if first sensor trigged
unsigned long start = millis();
while(digitalRead(PE_02)== HIGH);// Wait until second sensor trigged
unsigned long finish = millis();
float speed = 76200.0f/float(finish - start );
Serial.print("initial time (ms) = ");
Serial.println(start);
Serial.print("final time (ms) = ");
Serial.println(finish);
Serial.print("Speed of object (M/s) = ");
Serial.println(speed);
delay(500);
}

Arduino LED control sleep

I want to implement a simple LED controller with an Arduino Uno, that goes to sleep and has different buttons.
Functions of buttons are:
Digital 2: Button for ON OFF
Digital 3: Button for Wake up
Everything works ok, but when it goes to sleep, the LEDs also turn off. I want that after 30 seconds, when Arduino goes to sleep, lights stays on.
Here is my code:
#include <avr/sleep.h>
#define REDPIN 10
#define GREENPIN 11
#define BLUEPIN 9
#define delayTime 20 //za fading cas
unsigned long interval= 30000;
unsigned long previousMillis = 0;
const int ledPin = 12; // the pin that the LED is attached to
const int buttonPin1 = 2; //on off
bool vklop = false;
int bela = 10;
int barva;
int prejsnja_barva = 0;
int buttonPushCounter1 = 0; // counter for the number of button presses
int buttonState1 = 0; // current state of the button
int lastButtonState1 = 0; // previous state of the button
/////////////////////////////////////*SETUP*/////////////////////////////////////////
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
pinMode(3,INPUT); //because of interrupts PIN digital 3
digitalWrite(3,HIGH);
}
/////////////////////////////////////*LOOP*/////////////////////////////////////////
void loop()
{
unsigned long currentMillis = millis();
if ((currentMillis-previousMillis) > interval) //15s timer
{
previousMillis = currentMillis;
Serial.println("SLEEP!"); // kaj delaj po preteku 5s
delay(50);
sleepSetup(); //sleep mode
}
else
{
buttonState1 = digitalRead(buttonPin1);
/////////////////////////////////////ON/OFF/////////////////////////////////////////
/////////////////////////////////////ON/OFF/////////////////////////////////////////
if (buttonState1 != lastButtonState1) // compare the buttonState to its previous state
{
if (buttonState1 == HIGH) // if the state has changed, increment the counter
{
buttonPushCounter1++; // if the current state is HIGH then the button went from off to on:
Serial.println("on");
Serial.print("number of BUTTON1 pushes: ");
Serial.println(buttonPushCounter1);
digitalWrite(ledPin, HIGH);
if(buttonPushCounter1 % 2 == 0)
{
setColor(bela, bela, bela);
vklop = true;
barva = 13;
}
else
{
setColor(0, 0, 0);
vklop = false;
}
}
else // if the current state is LOW then the button went from on to off:
{
Serial.println("off");
digitalWrite(ledPin, LOW);
}
delay(50); // Delay a little bit to avoid bouncing
}
lastButtonState1 = buttonState1; // save the current state as the last state, for next time through the loop
}
}
/////////////////////////////////functions/////////////////////////////////////////////
/////////////////////////////////functions/////////////////////////////////////////////
/////////////////////////////////functions/////////////////////////////////////////////
void setColor(int red, int green, int blue)
{
analogWrite(REDPIN, red);
analogWrite(GREENPIN, green);
analogWrite(BLUEPIN, blue);
}
void sleepSetup(void)
{
sleep_enable(); // Set sleep enable (SE) bit:
attachInterrupt(1, pinInterrupt, LOW); // Set pin 2 as interrupt and attach handler:
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // define our preferred sleep mode:
digitalWrite(13,LOW);
sleep_cpu();
Serial.println("Just woke up!"); //OD TU SE NADALJUJE PO PRITISKU TIPKE
digitalWrite(13,HIGH);
}
void pinInterrupt() //ISR
{
sleep_disable();
detachInterrupt(0);
}
You're using AVR's Power Down sleep mode. In this mode all timers are turned off to save power.
No timers -> no PWM -> no analogue output -> no PWM driven LEDs
To keep the LED on use another sleep mode.
See
http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf for details.
But to be honest I am not quite sure if this makes any sense. If you're driving an LED through 3 outputs the power you can save by putting the MCU into sleep is maybe a few percent.
And as sleep stops the CPU and hence your program you won't be able to have the LEDs turn off after 30s.
Why not just wait 30s befor going to sleep? The alternative would be some external timing circuitry that would also consume power. So I guess having a few milliamps more for 30 seconds is still a better alternative.

Arduino Button with LED

I have put together an Arduino circuit that turns the led's off when the button is pressed. How do I code it so when I press it once it comes on and stays on and will only turn off once its pressed again? Any help would be appreciated
My Current code is:
int ledred = 12;
int ledgreen = 8;
int BUTTON = 4;
int speakerPin = 1;
void setup() {
// initialize the digital pin as an output.
Serial.begin(9600);
pinMode(ledgreen, OUTPUT);
pinMode(ledred, OUTPUT);
pinMode(BUTTON,INPUT);
}
void loop() {
if(digitalRead(BUTTON) == HIGH){
digitalWrite(ledred,HIGH);
digitalWrite(ledgreen,HIGH);
}else
{
digitalWrite(ledred,LOW);
digitalWrite(ledgreen,LOW);
}
}
If all you want is do this, you can use one of the interrupt pins and watch for the RISING (or FALLING) event.
Something similar to this example:
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
Mind that you may still need some debouncing strategy.
Also, you don't need to use an interrupt for that, but then you'd need some edge-detection algorithm. These are quite well explained in the debouncing article above. I personally prefer these, since interrupt pins in the UNO board are precious enough not to be used with humble button pressings... :o)
/*
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
The circuit:
- LED attached from pin 13 to ground
- pushbutton attached from pin 2 to +5V
- 10 kilohm resistor attached from pin 2 to ground
- Note: On most Arduino boards, there is already an LED on the board connected
to pin 13, so you don't need any extra components for this example.
created 21 Nov 2006
by David A. Mellis
modified 30 Aug 2011
by Limor Fried
modified 28 Dec 2012
by Mike Walters
modified 30 Aug 2016
by Arturo Guadalupi
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Debounce
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}

Arduino External Interrupt Not Fast Enough

I've been trying to measure the time that the line is high on the Arduino. It goes high, then stays high for a couple of milliseconds. Then it gets pulled low for 1us and then floats back to high. My code doesn't seem to recognise the line getting pulled low. Is 1us too fast for the interrupt? How can I slow it down?
Thank you
EDIT
My thoughts are to use an RC filter in conjunction with a diode to slow the rise time enough for the Arduino to recognise the change, but only when the change occurs from the receiving line. Is this viable? Or can I use a pulse extender chip with a diode in the same way?
#define ESC 2 //the digital pin the esc signal line is attached to
int throttlePos = 0;
volatile unsigned long timer_start;
volatile int last_interrupt_time;
volatile int pulse_time;
void setup() {
// put your setup code here, to run once:
pinMode(ESC, OUTPUT); //originally set the ESC's line to output to keep line high ready for throttle armature
digitalWrite(ESC, HIGH); //keep the pulse high due to inverted throttle pulse
Serial.begin(115200); //opens the serial port for use when testing
timer_start = 0;
attachInterrupt(digitalPinToInterrupt(ESC), calcSignal, CHANGE);
for(throttlePos = 0; throttlePos <= 1000; throttlePos += 1) //these for loops arm the ESC by emulating an inverted PWM pulse, process takes two seconds
{
digitalWrite(ESC, LOW);
delayMicroseconds(1500);
digitalWrite(ESC, HIGH);
delayMicroseconds(100);
}
for(throttlePos = 1000; throttlePos <= 2000; throttlePos += 1)
{
digitalWrite(ESC, LOW);
delayMicroseconds(1000);
digitalWrite(ESC, HIGH);
delayMicroseconds(100);
}
}
void loop() {
digitalWrite(ESC, LOW);
delayMicroseconds(1200);
digitalWrite(ESC, HIGH);
delayMicroseconds(100);
delay(19);
Serial.println(pulse_time);
}
void calcSignal()
{
//record the interrupt time so that we can tell if the receiver has a signal from the transmitter
last_interrupt_time = micros();
//if the pin has gone HIGH, record the microseconds since the Arduino started up
if(digitalRead(ESC) == HIGH)
{
timer_start = micros();
}
//otherwise, the pin has gone LOW
else
{
//only worry about this if the timer has actually started
if(timer_start != 0)
{
//record the pulse time
pulse_time = ((volatile int)micros() - timer_start);
//restart the timer
timer_start = 0;
}
}
}
1/1us is 1MHz. Given that your Arduino executes instructions at 16MHz (at best, some instructions take longer), your interrupt handler could easily be missing things. However, the interrupt should still execute, even if the interrupt condition is cleared before the interrupt finishes.
Are you using the Arduino libraries for interrupts or configuring pin change interrupts directly at the register level?
Have you verified that the pulse into the Arduino is electrically sound? Without knowing more about your circuit, it's difficult to suggest a fix.

Resources