#include <Arduino.h>
#include <esp32-hal-timer.h>
// Pin definitions
const int buttonPin = 34; // Pin connected to the button
const int clockPin = 18; // Pin connected to the clock output
// Timer variables
hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
volatile bool clockRunning = false;
// Interrupt service routine for the timer
void IRAM_ATTR onTimer() {
digitalWrite(clockPin, !digitalRead(clockPin)); // Toggle clock pin state
}
// Interrupt service routine for the button
void IRAM_ATTR buttonISR() {
// Debouncing delay
delay(50);
// Check button state again to ensure it's still pressed
if (digitalRead(buttonPin) == LOW) {
// Toggle clock running state
clockRunning = !clockRunning;
if (clockRunning) {
// Start the timer
timerAlarmEnable(timer);
Serial.println("Clock started.");
} else {
// Stop the timer
timerAlarmDisable(timer);
digitalWrite(clockPin, LOW); // Set clock pin low
Serial.println("Clock stopped.");
}
}
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Configure button pin
pinMode(buttonPin, INPUT_PULLUP);
// Configure clock pin
pinMode(clockPin, OUTPUT);
digitalWrite(clockPin, LOW); // Start with clock pin low
// Create a timer
timer = timerBegin(0, 80, true); // Timer 0, divider 80 (1MHz), count up
timerAttachInterrupt(timer, &onTimer, true); // Attach timer interrupt
timerAlarmWrite(timer, 100, true); // 100 microsecond period (10 MHz frequency)
// Attach interrupt to the button pin
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, FALLING);
}
void loop() {
// Nothing to do here because the action is handled by interrupts
}
there was no error in compiling
But i am not able to generate the clock pulse.