the pulse counter is reporting almost half the number it should report.
This commit is contained in:
parent
173fab6433
commit
d0543bbbab
|
|
@ -4,30 +4,69 @@ Servo myServo; // Create a Servo object
|
||||||
const int servoPin = 10; // D10 is OC1B on Pro Mini
|
const int servoPin = 10; // D10 is OC1B on Pro Mini
|
||||||
const int ledPin = 13;
|
const int ledPin = 13;
|
||||||
const int analogPin = A0;
|
const int analogPin = A0;
|
||||||
|
const int SensorPin = 2;
|
||||||
|
|
||||||
|
volatile uint16_t pulses = 0,ppm = 0; //pulse per minute
|
||||||
|
|
||||||
|
|
||||||
|
// Interrupt Service Routine (ISR)
|
||||||
|
void INT0_ISR() {
|
||||||
|
pulses++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_timer() {
|
||||||
|
|
||||||
|
// Stop Timer2
|
||||||
|
TCCR2A = 0;
|
||||||
|
TCCR2B = 0;
|
||||||
|
TCNT2 = 0;
|
||||||
|
// Set compare match value for 100ms
|
||||||
|
// Clock = 16 MHz, Prescaler = 1024
|
||||||
|
// Tick = 16,000,000 / 1024 = 15,625 ticks/sec
|
||||||
|
// 100ms = 0.1s → 0.1 * 15625 = 1562.5 ticks
|
||||||
|
// Since Timer2 is 8-bit, we can't reach 1562 in one cycle.
|
||||||
|
// So we will count multiple compare matches.
|
||||||
|
OCR2A = 50; // Compare match every ~16ms (250 counts at 1024 prescaler)
|
||||||
|
// CTC mode
|
||||||
|
TCCR2A |= (1 << WGM21);
|
||||||
|
// Prescaler = 1024
|
||||||
|
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
|
||||||
|
// Enable compare match interrupt
|
||||||
|
TIMSK2 |= (1 << OCIE2A);
|
||||||
|
// Enable global interrupts
|
||||||
|
sei();
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(ledPin, OUTPUT);
|
pinMode(ledPin, OUTPUT);
|
||||||
Serial.begin(115200); // Initialize Serial
|
Serial.begin(115200); // Initialize Serial
|
||||||
myServo.attach(servoPin); // Attach servo to pin 10
|
myServo.attach(servoPin); // Attach servo to pin 10
|
||||||
|
|
||||||
|
attachInterrupt(digitalPinToInterrupt(SensorPin), INT0_ISR, FALLING);
|
||||||
|
setup_timer();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
int analogValue = analogRead(analogPin); // Read from A0 (0–1023)
|
int analogValue = analogRead(analogPin); // Read from A0 (0–1023)
|
||||||
int angle = map(analogValue, 0, 1023, 0, 180); // Map to 0–180 degrees
|
int angle = map(analogValue, 0, 1023, 0, 180); // Map to 0–180 degrees
|
||||||
|
Serial.print("pulses: ");
|
||||||
|
Serial.println(pulses);
|
||||||
|
|
||||||
myServo.write(angle); // Set servo angle
|
myServo.write(angle); // Set servo angle
|
||||||
|
|
||||||
digitalWrite(ledPin, HIGH);
|
delay(1000);
|
||||||
// Serial.print("LED ON - Analog: ");
|
}
|
||||||
// Serial.print(analogValue);
|
|
||||||
// Serial.print(" | Angle: ");
|
|
||||||
// Serial.println(angle);
|
ISR(TIMER2_COMPA_vect) {
|
||||||
delay(10);
|
static uint16_t ticks = 0;
|
||||||
|
static bool ledState = false;
|
||||||
digitalWrite(ledPin, LOW);
|
ticks++;
|
||||||
// Serial.print("LED OFF - Analog: ");
|
if (ticks >= 150) { // 150 => 1s
|
||||||
// Serial.print(analogValue);
|
ticks = 0;
|
||||||
// Serial.print(" | Angle: ");
|
ledState = !ledState;
|
||||||
// Serial.println(angle);
|
ppm = pulses;
|
||||||
delay(10);
|
pulses = 0;
|
||||||
|
digitalWrite(ledPin, ledState);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue