M5Cardputer ESP-NOW Sender — ESP32-S3 simulation

ESP-NOW sender on M5Cardputer with keyboard input, displays send status on screen.

An interactive ESP32-S3 circuit simulation you can run free in your browser on Velxio, by jolantapietnik.

Components

Sketch code

#include <Arduino.h>
#include "WiFi.h"
#include <motor.h>
// #include <esp_now.h>

#define OFF_TIMEOUT 0xFA
#define ARM_CONTROLLER 0x05

struct struct_message
{
    char input;
    uint32_t timestamp;
};

volatile struct_message recivedMessage;
volatile unsigned long recivedTimestamp;

int moveMode = 0;

DIRECTIONS driveDirection(const char c)
{
    switch (c)
    {
    case 'w':
    case ';': // Forward
        return DIRECTIONS::FORWARD;
    case 's':
    case '.': // Back
        return DIRECTIONS::BACK;
    case 'a':
    case ',': // Left
        return DIRECTIONS::LEFT;
    case 'd':
    case '/': // Right
        return DIRECTIONS::RIGHT;
    case 't':
        moveMode = moveMode ? 0 : 1;
        break;
    default: // Stop
        return DIRECTIONS::HALT;
    }
}

void setup()
{
    Serial.begin(115200);
    Serial.println("Checkpoint: Serial0 Ok!");
    Serial2.begin(9600, SERIAL_8N1, 18, 17);
    Serial.println("Checkpoint: Serial2 Ok!");

    delay(1000);
    Serial.println("[ESP-NOW]: ROLE <Reciver>");

    pinMode(11, INPUT_PULLUP);
    pinMode(12, INPUT_PULLUP);
    pinMode(13, INPUT_PULLUP);
    pinMode(14, INPUT_PULLUP);

}

void MapButtons() {
    if (digitalRead(11) == LOW) {
        recivedMessage.input = 'w';
        recivedMessage.timestamp = millis();
    }
    else if (digitalRead(12) == LOW) {
        recivedMessage.input = 's';
        recivedMessage.timestamp = millis();
    }
    else if (digitalRead(13) == LOW) {
        recivedMessage.input = 'a';
        recivedMessage.timestamp = millis();
    }
    else if (digitalRead(14) == LOW) {
        recivedMessage.input = 'd';
        recivedMessage.timestamp = millis();
    }
    else {
        recivedMessage.input = 'x';
        recivedMessage.timestamp = millis();
    }
}


void loop()
{
    MapButtons();
    unsigned long now = millis();
    bool timeout = (now - recivedTimestamp) > OFF_TIMEOUT;
    DIRECTIONS driveDirectionVal = driveDirection(recivedMessage.input);
    Serial.println(driveDirectionVal);

    // TODO: write method to change mode (Arm/Move)
    Serial2.printf("Direction=%d;MoveMode=%d", driveDirectionVal, moveMode);
    delay(1000);
}