-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharduinoTrinketCode.txt
44 lines (35 loc) · 1.17 KB
/
arduinoTrinketCode.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
volatile unsigned long lastRisingEdge = 0;
volatile unsigned long period = 0;
// Desired on and off times in milliseconds
unsigned long desiredOnTime = 10;
unsigned long desiredOffTime = 10;
// Pin assignments
const int inputPin = 2; // Pin to read the incoming frequency
const int outputPin = 13; // Pin to output the on/off signal
void setup() {
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
// Attach an interrupt to the input pin on the RISING edge
attachInterrupt(digitalPinToInterrupt(inputPin), onRisingEdge, RISING);
}
void loop() {
unsigned long currentPeriod;
// Safely read the shared 'period' variable
noInterrupts();
currentPeriod = period;
interrupts();
// Check if a period has been measured
if (currentPeriod > 0) {
// Turn the output pin on
digitalWrite(outputPin, HIGH);
delay(140); // 140ms (needs to be >70)
// Turn the output pin off
digitalWrite(outputPin, LOW);
delay(800); // 800ms (needs to be >400)
}
}
void onRisingEdge() {
unsigned long currentRisingEdge = micros();
period = currentRisingEdge - lastRisingEdge;
lastRisingEdge = currentRisingEdge;
}