#define BUTTON_PIN 21 // GIOP21 pin connected to button
#define LED_PIN 22
#define BUZZER_PIN 19
#define PRESSED 0
#define NOT_PRESSED 1

int freq = 5000;
int resolution = 8;
int dutyCycle = 0;
int tone_freq = 440;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }
  Serial.println("Starting...");
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  ledcAttach(LED_PIN, freq, resolution);

  ledcWrite(LED_PIN, dutyCycle);
}

void loop() {
  ledcWrite(LED_PIN, dutyCycle);
  blockPress();
  dutyCycle += 50;
  Serial.println(dutyCycle);
  // print out the button's state
  if (dutyCycle >= 255) { dutyCycle = 0; }
  delay(0);
}


void blockPress() {
  int state = NOT_PRESSED;
  
  while (state != PRESSED) {
    delay(10);
    state = digitalRead(BUTTON_PIN);
  }
  tone(BUZZER_PIN, tone_freq);

  while (state == PRESSED) {
    delay(10);
    state = digitalRead(BUTTON_PIN);
  }
  noTone(BUZZER_PIN);
}
$abcdeabcde151015202530fghijfghij
Loading
esp32-devkit-c-v4