For my project, I need an ESP32 that reads the data from a PS4 controller and then transmits this information to an Arduino Mega. Although I've already developed an initial approach, I'm lacking the necessary knowledge to program in this area. As a result, I attempted to seek assistance from AI, but it hasn't worked out as expected.
Mega2560 as I2C MASTER:
#include <Wire.h>
const byte SLAVEADDRESS = 8;
const byte BYTEZAHL = 2 * sizeof(int);
union {
byte bytes[BYTEZAHL];
struct {
uint16_t Wert_1;
uint16_t Wert_2;
};
} tx;
void loop()
{
uint32_t jetzt = millis();
static uint32_t vorhin = jetzt;
if (jetzt - vorhin >= 1000)
{
vorhin = jetzt;
tx.Wert_1 = random(150, 300);
tx.Wert_2 = random(0, 1000);
Wire.beginTransmission(SLAVEADDRESS);
Wire.write(tx.bytes, sizeof(tx));
Wire.endTransmission();
Serial.print(F("\ttx.Wert_1: ")); Serial.print(tx.Wert_1); Serial.print(F("\ttx.Wert_2: ")); Serial.println(tx.Wert_2);
}
}
void setup()
{
Wire.begin();
Serial.begin(115200);
//Wire.setClock(400000L); //setzt die I²C Taktfrequenz auf 400kHz
delay(1000);
Serial.println(F("\nStart"));
Serial.print(F("BYTEZAHL Byte für int: ")); Serial.println(sizeof(int));
}
ESP32 as I2C SLAVE:
#include <Wire.h>
const byte SLAVEADDRESS = 8;
const byte BYTEZAHL = 4;
union {
byte bytes[BYTEZAHL];
struct {
int16_t Wert_1;
int16_t Wert_2;
};
} rx;
volatile int32_t rxWert_1 = 0;
volatile int32_t rxWert_2 = 0;
bool neu = false;
void setup()
{
Wire.begin(SLAVEADDRESS);
Wire.onReceive(receiveEvent);
Serial.begin(115200);
Serial.println(F("\nStart"));
}
void receiveEvent(int numBytes)
{
if (BYTEZAHL == numBytes)
{
for (byte j = 0; j < numBytes; j++)
{
rx.bytes[j] = Wire.read();
}
rxWert_1 = rx.Wert_1;
rxWert_2 = rx.Wert_2;
neu = true;
}
}
void loop()
{
if (neu)
{
neu = false;
for (byte j = 0; j < BYTEZAHL; j++)
{
Serial.print(rx.bytes[j], HEX); Serial.print(' ');
}
Serial.print(F("\trxWert_1: ")); Serial.print(rxWert_1); Serial.print(F("\trxWert_2: ")); Serial.println(rxWert_2);
}
}
PS4 Code:
#include <PS4Controller.h>
unsigned long lastTimeStamp = 0;
void notify()
{
char messageString[200];
sprintf(messageString, "%4d,%4d,%4d,%4d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d,%3d",
PS4.LStickX(),
PS4.LStickY(),
PS4.RStickX(),
PS4.RStickY(),
PS4.Left(),
PS4.Down(),
PS4.Right(),
PS4.Up(),
PS4.Square(),
PS4.Cross(),
PS4.Circle(),
PS4.Triangle(),
PS4.L1(),
PS4.R1(),
PS4.L2(),
PS4.R2(),
PS4.Share(),
PS4.Options(),
PS4.PSButton(),
PS4.Touchpad(),
PS4.Charging(),
PS4.Audio(),
PS4.Mic(),
PS4.Battery());
//Only needed to print the message properly on serial monitor. Else we dont need it.
if (millis() - lastTimeStamp > 50)
{
Serial.println(messageString);
lastTimeStamp = millis();
}
}
void onConnect()
{
Serial.println("Connected!.");
}
void onDisConnect()
{
Serial.println("Disconnected!.");
}
void setup()
{
Serial.begin(115200);
PS4.attach(notify);
PS4.attachOnConnect(onConnect);
PS4.attachOnDisconnect(onDisConnect);
PS4.begin();
Serial.println("Ready.");
}
void loop()
{
}
I would be very happy if someone could help me.
Best regards, Viki.