ESP32-C6x2 — Esp32 C6 simulation

An interactive Esp32 C6 circuit simulation you can run free in your browser on Velxio, by kshyhoo.

Components

Sketch code

const int NUM_BYTES = 5;
// 74HC165 - PRZYCISKI (BUTTONS)
const int BTN_SL = 11;
const int BTN_CK = 2;
const int BTN_DI = 3;
// 74HC595 - DIODY LED
const int LED_CLOCK = 4;
const int LED_LATCH = 5;
const int LED_DATA = 0;

const int BUZZER_PIN = 10;
const int pulseDelay = 5;

byte buttonData[NUM_BYTES];
byte lastButtonState[NUM_BYTES];
byte ledState[NUM_BYTES];  // Stan diod odzwierciedlający feedback z ESP2

void setup() {
  Serial.begin(115200);                     // Debug
  Serial1.begin(115200, SERIAL_8N1, 16, 17);
  pinMode(BTN_SL, OUTPUT);
  pinMode(BTN_CK, OUTPUT);
  pinMode(BTN_DI, INPUT);
  pinMode(LED_LATCH, OUTPUT);
  pinMode(LED_CLOCK, OUTPUT);
  pinMode(LED_DATA, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
  digitalWrite(BTN_SL, HIGH);
  Serial.println("ESP 1 - Panel Sterujący START");
}

void loop() {
  // 1. ODCZYT PRZYCISKÓW
  digitalWrite(BTN_SL, LOW);
  delayMicroseconds(pulseDelay);
  digitalWrite(BTN_SL, HIGH);
  delayMicroseconds(pulseDelay);
  for (int i = NUM_BYTES - 1; i >= 0; i--) {
    for (int j = 0; j < 8; j++) {
      int bit = digitalRead(BTN_DI);
      bitWrite(buttonData[i], j, bit);
      digitalWrite(BTN_CK, HIGH);
      delayMicroseconds(pulseDelay);
      digitalWrite(BTN_CK, LOW);
      delayMicroseconds(pulseDelay);
    }
  }
  // 2. DETEKCJA KLIKNIĘCIA I WYSŁANIE KOMUNIKATU DO ESP 2
  for (int i = 0; i < NUM_BYTES; i++) {
    for (int j = 0; j < 8; j++) {
      bool isPressed = bitRead(buttonData[i], j);
      bool wasPressed = bitRead(lastButtonState[i], j);
      if (isPressed && !wasPressed) {
        int buttonNumber = (i * 8) + j;
        // Wysłanie informacji przez UART do drugiego ESP (np. format: "B:nr\n")
        Serial1.print("B:");
        Serial1.println(buttonNumber);
        tone(BUZZER_PIN, 2500, 15);
        Serial.print("Wysłano kliknięcie kanału: ");
        Serial.println(buttonNumber);
      }
    }
    lastButtonState[i] = buttonData[i];
  }
  // 3. ODBIERANIE STANU DIOD / FEEDBACKU Z ESP 2
  if (Serial1.available() >= NUM_BYTES) {
    for (int i = 0; i < NUM_BYTES; i++) {
      ledState[i] = Serial1.read();
    }
  }
  // 4. WYSYŁANIE NA LEDY
  digitalWrite(LED_LATCH, LOW);
  for (int i = 0; i < NUM_BYTES; i++) {
    shiftOut(LED_DATA, LED_CLOCK, LSBFIRST, ledState[i]);
  }
  digitalWrite(LED_LATCH, HIGH);
  delay(10);
}

More projects by kshyhoo