Interrupts and cloud connection

Hi guys :slight_smile:

I want to use a Davis 6410 wind sensor. There is a sample code for arduino:

I have adjusted the code that I can use it with my Photon:

#include <math.h>

#define WindSensorPin (2) // The pin location of the anemometer sensor

volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine

float WindSpeed; // speed miles per hour

void setup() {
Serial.begin(9600);

pinMode(WindSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);

Serial.println("Davis Wind Speed Test");
Serial.println("Rotations\tMPH");
}

void loop() {

Rotations = 0; // Set Rotations count to 0 ready for calculations

attachInterrupt(WindSensorPin, isr_rotation, FALLING);
delay(3000);
detachInterrupt(WindSensorPin);
// convert to mp/h using the formula V=P(2.25/T)
// V = P(2.25/3) = P * 0.75

WindSpeed = Rotations * 0.75;

Serial.print(Rotations); Serial.print("\t\t");
Serial.println(WindSpeed);

}

// This is the function that the interrupt calls to increment the rotation count
void isr_rotation () {

if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact.
Rotations++;
ContactBounceTime = millis();
}

} 

But when I use this code I have problems uploading a new code OTA. I think the problem are is the interrupt function. How I can adjust my code that I can upload my new code OTA still using the interrupt function?

Best regards

@electronweather, you may want to try this code instead of delay(3000). It allows the DeviceOS to process Cloud stuff during the delay.

for(uint32_t ms = millis(); millis() - ms < 3000L; Particle.process()); // Different version of delay(3000)

Also, use the correct Particle definition for the pin you want to use, in this case D2 I assume. The first attachInterrupt() function is not needed since, instead of using interrutps() and noInterrupts() you use attachInterrupt() and detachInterrupt(), which is a wise substitute. With these changes, you code would look like this:

#include <math.h>

#define WindSensorPin (D2)	// The pin location of the anemometer sensor

volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine

float WindSpeed; // speed miles per hour

void setup() {
	Serial.begin(9600);

	pinMode(WindSensorPin, INPUT);

	Serial.println("Davis Wind Speed Test");
	Serial.println("Rotations\tMPH");
}

void loop() {

	Rotations = 0; // Set Rotations count to 0 ready for calculations

	attachInterrupt(WindSensorPin, isr_rotation, FALLING);
	for(uint32_t ms = millis(); millis() - ms < 3000L; Particle.process()); // Different version of delay(3000)
	detachInterrupt(WindSensorPin);
	// convert to mp/h using the formula V=P(2.25/T)
	// V = P(2.25/3) = P * 0.75

	WindSpeed = Rotations * 0.75;

	Serial.print(Rotations); Serial.print("\t\t");
	Serial.println(WindSpeed);

}

// This is the function that the interrupt calls to increment the rotation count
void isr_rotation () {

	if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact.
		Rotations++;
		ContactBounceTime = millis();
	}

} 

Thank you! :slight_smile:

But isnโ€™t it necessary to reset โ€œmsโ€ each loop?

@electronweather read the for loop! It gets reset there.

Ah now I can see it :slight_smile:

1 Like

Is it necessary to call Particle.process()); if SYSTEM_THREAD(ENABLED); ?

Nope, you wonโ€™t need to do that anymore - not even in SYSTEM_MODE(MANUAL)

So I dont have to use Particle.process());?