I'm sending data to the receiver side upon change, now I want to send the y2 joystick axis mapped value over espnow to the receiver side and write it as the position of the servo motor. I can't send the joystick value for servo upon change as it may cause fluctuations. How can I modify the code to acheive this.
//--------------------------------------------
//ESP32 remote via ESP-NOW Protocol
//Transmit joy vals- fsr val
//--------------------------------------------
#include <esp_now.h>
#include <WiFi.h>
#include <ESP32MX1508.h>
//------------------------------------------------------------
#define open_pin 12
//FEEDBACK MOTOR
#define ROTA 4
#define ROTB 15
#define CH1 0
#define CH2 1
#define RES 12
MX1508 motorA(ROTA, ROTB, CH1, CH2, RES); //object for yrot motor
int motorSpeedMin = 0;
int motorSpeedMax = 4095;
// Define FSR threshold and motor speed limits
const int fsrThreshold = 500; // Adjust thresho
int fsrVal = 0;
//// Yrotation button pins
#define CW_BUTTON_PIN 25
#define ACW_BUTTON_PIN 26
//joystick pins
#define x 32
#define y 33
#define z 34
//#define y2 35
//STATES
#define FWD 2
#define REV 1
#define STOP 0
#define OPEN 0
#define CLOSE 1
#define CW 1
#define ACW 2
byte xState = 0;
byte yState = 0;
byte zState = 0;
byte jawState = 0;
byte rotState = 0;
byte lastxState = 0;
byte lastyState = 0;
byte lastzState = 0;
byte lastjawState = 0;
byte lastrotState = 0;
bool move = false;
#define dead_pos 700
#define dead_neg -700
int map_x = 0;
int map_y = 0;
int map_z = 0;
int map_y2 = 0;
int val_jaw = 0;
int val_cw = 0;
int val_acw = 0;
//------------------------------------------------------------
uint8_t RxMACaddress[] = {0xC0, 0x49, 0xEF, 0xD0, 0xDB, 0xB8};
//------------------------------------------------------------
typedef struct TxStruct
{
byte xData = STOP;
byte yData = STOP;
byte zData = STOP;
bool jaw = CLOSE;
byte rot = STOP;
} TxStruct;
TxStruct sentData;
//------------------------------------------------------------
typedef struct RxStruct
{
int fsr;
} RxStruct;
RxStruct receivedData;
esp_now_peer_info_t peerInfo;
//------------------------------------------------------------
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len)
{
memcpy(&receivedData, incomingData, sizeof(receivedData));
haptics();
}
//===================functions===================================================================
//state change for x link
void change_x() {
if (map_x < dead_neg || map_x > dead_pos) {
if (map_x > dead_pos) {
xState = FWD;
} else if (map_x < dead_neg) {
xState = REV;
}
}
else {
xState = STOP;
}
return;
}
//state change for y link
void change_y() {
if (map_y < dead_neg || map_y > dead_pos) {
if (map_y > dead_pos) {
yState = FWD;
} else if (map_y < dead_neg) {
yState = REV;
}
}
else {
yState = STOP;
}
return;
}
//state change for z link
void change_z() {
if (map_z < dead_neg || map_z > dead_pos) {
if (map_z > dead_pos) {
zState = FWD;
} else if (map_z < dead_neg) {
zState = REV;
}
}
else {
zState = STOP;
}
return;
}
//state change for jaw
void change_jaw() {
if (val_jaw == LOW ) {
jawState = OPEN;
}
else {
jawState = CLOSE;
}
//return;
}
//state change for ROTATION
void change_rot() {
if (val_cw == LOW and val_acw == HIGH ) {
rotState = CW;
}
else if (val_cw == HIGH and val_acw == LOW ) {
rotState = ACW;
}
else {
rotState = STOP;
}
//return;
}
//state for haptics
void haptics() {
fsrVal = receivedData.fsr;
// Map FSR value to motor speed
//int motorSpeed = map(fsrVal, fsrThreshold, 4095, motorSpeedMin, motorSpeedMax);
// Limit motor speed within the defined range
//motorSpeed = constrain(motorSpeed, motorSpeedMin, motorSpeedMax);
// Set motor speed
motorA.motorGo(fsrVal);
}
//======================================================================================
// preparing and sending data
void cast() {
if (xState != lastxState || yState != lastyState || zState != lastzState || jawState != lastjawState || rotState != lastrotState) { //This makes the async xTX data flow
sentData.xData = xState;
sentData.yData = yState;
sentData.zData = zState;
sentData.jaw = jawState;
sentData.rot = rotState;//Cast the changed state to the xTX callback
//sending
esp_err_t result = esp_now_send(RxMACaddress, (uint8_t *) &sentData, sizeof(sentData));
// getting feedback message
//feedback();
if (result == ESP_OK) {
char x_buf[20];
char y_buf[20];
char z_buf[20];
char j_buf[20];
char r_buf[20];
String sx, sy, sz, sj, sr = "";
if (xState == FWD) {
sx = "X_REV";
} else if (xState == REV) {
sx = "X_FWD";
} else if (xState == STOP) {
sx = "X_STOP";
}
if (yState == FWD) {
sy = "Y_REV";
} else if (yState == REV) {
sy = "Y_FWD";
} else if (yState == STOP) {
sy = "Y_STOP";
}
if (zState == FWD) {
sz = "Z_REV";
} else if (zState == REV) {
sz = "Z_FWD";
} else if (zState == STOP) {
sz = "Z_STOP";
}
if (jawState == OPEN) {
sj = "OPEN";
} else if (jawState == CLOSE) {
sj = "CLOSED";
}
if (rotState == CW) {
sr = "CW";
} else if (rotState == ACW) {
sr = "ACW";
} else if (rotState == STOP) {
sr = "NO ROT";
}
sprintf(x_buf, "Sending %s", sx);
sprintf(y_buf, "Sending %s", sy);
sprintf(z_buf, "Sending %s", sz);
sprintf(j_buf, "Sending %s", sj);
sprintf(r_buf, "Sending %s", sr);
Serial.println(x_buf);
Serial.println(y_buf);
Serial.println(z_buf);
Serial.println(j_buf);
Serial.println(r_buf);//Only prints when packet is sent
} else {
Serial.println("Failed to send data over ESP-NOW");
}
}
}
//======================================================================================
void setup()
{
Serial.begin(115200);
pinMode(open_pin, INPUT_PULLUP);
pinMode(CW_BUTTON_PIN, INPUT_PULLUP);
pinMode(ACW_BUTTON_PIN, INPUT_PULLUP);
motorA.motorBrake();
//----------------------------------------------------------
WiFi.mode(WIFI_STA);
//----------------------------------------------------------
if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}
//----------------------------------------------------------
// esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, RxMACaddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
//----------------------------------------------------------
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
//----------------------------------------------------------
esp_now_register_recv_cb(OnDataRecv);
}
//======================================================================================
void loop()
{
//reading values
val_jaw = digitalRead(open_pin);
val_cw = digitalRead(CW_BUTTON_PIN);
val_acw = digitalRead(ACW_BUTTON_PIN);
int val_x = analogRead(x);
int val_y = analogRead(y);
int val_z = analogRead(z);
//int val_y2 = analogRead(y2);
//mapping
map_x = map(val_x, 0, 4095, -1000, 1000);
map_y = map(val_y, 0, 4095, -1000, 1000);
map_z = map(val_z, 0, 4095, -1000, 1000);
//map_y2 = map(val_y2, 0, 4095, -1000, 1000);
//----------------------------------------------------------
// observing state change
change_x();
change_y();
change_z();
change_jaw();
change_rot();
//casting, sending and getting feedback
cast();
//----------------------------------------------------------
lastxState = xState;
lastyState = yState;
lastzState = zState;
lastjawState = jawState;
lastrotState = rotState;
//----------------------------------------------------------
//delay(1000);
}