🛠 Les 3 boutons A, B, C et le haut-parleur interne

M5Stack Core (ESP32) — Basic / Gray family (K001 / K002) · debutant · Schéma : IMG-m5stack-core-esp32-debutant-2-m5stack-core-esp32-boutons-ab-c.svg — M5Stack Core de face avec les trois boutons avant surlignés et leurs broches GPIO39 (A), GPIO38 (B), GPIO37 (C), haut-parleur 1 W sur GPIO25 indiqué à l'arrière

Objectives

Hardware

Complete code

#include <M5Unified.h>

uint32_t nbA = 0, nbB = 0, nbC = 0;

void setup {
  auto cfg = M5.config;
  M5.begin(cfg);
  M5.Speaker.setVolume(180);
  M5.Display.setTextSize(2);
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
  M5.Display.println("Boutons A / B / C");
}

void loop {
  M5.update;                       // indispensable avant wasPressed
  if (M5.BtnA.wasPressed) {
    nbA++;
    M5.Speaker.tone(2200, 100);      // 2,2 kHz pendant 100 ms
  }
  if (M5.BtnB.wasPressed) {
    nbB++;
    M5.Speaker.beep;
  }
  if (M5.BtnC.wasPressed) {
    nbC++;
    M5.Speaker.mute;
  }
  M5.Display.fillRect(0, 50, 320, 90, TFT_BLACK);
  M5.Display.setCursor(10, 55);
  M5.Display.printf("A : %lu\n", nbA);
  M5.Display.printf("B : %lu\n", nbB);
  M5.Display.printf("C : %lu\n", nbC);
  vTaskDelay(5 / portTICK_PERIOD_MS);
}

Expected verification

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials