Using rotary encoder with 2 Esp 32s' to control servo motor

Hello, a complete newbie here. So i was trying to send data using rotary encoder and esp 32 to control a servo motor. I want the sender to send 1 when the rotary encoder turns clockwise, -1 when it turns counter clock wise and 0 when the button the rotary encoder is pressed. Use code tags to format code for the forum.

THESE ARE MY CODES FOR THE SENDER.

#include <esp_now.h>
#include <WiFi.h>

#define outputA 32  
#define outputB 34
#define outputSw 25

int counter = 0;
int aState;
int aLastState;

int nowState;

uint8_t macAll[] = { 0xC0, 0x49, 0xEF, 0x69, 0xB0, 0xA4 };  // Receiver MAC address

typedef struct struct_message {
  int angle;
} struct_message;

struct_message myData;

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void setup() {
  pinMode(outputA, INPUT_PULLUP);
  pinMode(outputB, INPUT_PULLUP);
  pinMode(outputSw, INPUT_PULLUP);

  Serial.begin(115200);

  aLastState = digitalRead(outputA);

  WiFi.mode(WIFI_STA);
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  esp_now_register_send_cb(OnDataSent);

  esp_now_peer_info_t peerInfo;
  peerInfo.channel = 0;
  peerInfo.encrypt = false;
  memcpy(peerInfo.peer_addr, macAll, 6);

  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }

  attachInterrupt(outputA, encoder, CHANGE);
  attachInterrupt(outputB, encoder, CHANGE);
  attachInterrupt(outputSw, encoderReset, FALLING);
}

void encoder() {
  aState = digitalRead(outputA);
  
  if (aState != aLastState) {
    if (digitalRead(outputB) != aState) {
      myData.angle = 1;
      nowState = 1;
    } else {
      myData.angle = -1;
      nowState = -1;
    }
    esp_err_t result = esp_now_send(macAll, (uint8_t *)&myData, sizeof(myData));
    
    //Serial.println(nowState);
  }
  aLastState = aState;
}

void encoderReset() {
  myData.angle = 0;
  nowState = 0;
  esp_err_t result = esp_now_send(macAll, (uint8_t *)&myData, sizeof(myData));
  
  //Serial.println(nowState);
}

void loop() {
  // Nothing to do here since interrupts handle everything
  Serial.println(nowState);
}

THESE ARE MY CODES FOR THE RECEIVER.

#include <WiFi.h>
#include <esp_now.h>
#include <ESP32Servo.h>  //motor

int mapAngle;

Servo myservo;  // servo motor name

/////////////////////////ESP-NOW//////////////////////////////

typedef struct struct_message {
  int angle;
} struct_message;

struct_message myData;

int Counter = 0; // Moved declaration outside loop()

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  //Serial.print("\r\nLast Packet Send Status:\t");
}

void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
  //Serial.println("recv_something");
  memcpy(&myData, incomingData, sizeof(myData));
  int rawCounter = myData.angle;

  if (rawCounter == 1) {
    Counter++; 
    if (Counter > 360) {
      Counter = 360; //LIMIT MAX COUNTER
    }
  }

  if (rawCounter == -1) {
    Counter--; 
    if (Counter <= 0) {
      Counter = 0; //LIMIT MINIMUM COUNTER
    }
  }

  if (rawCounter == 0) {
    Counter = 0;
  }

mapAngle = map(Counter, 0. 360, 0, 180);

  Serial.print("The New Motor angle is   ");
  Serial.println(mapAngle);

  myservo.write(mapAngle);

  //mapAngleValueP = mapAngleValueN;
}

void setup() {
  Serial.begin(115200);
  // myservo.attach(27);  // motor pinOut
  // while (! Serial) {
  //   delay(1);
  // }
  WiFi.mode(WIFI_STA);
  Serial.println(WiFi.macAddress());

  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  esp_now_register_recv_cb(OnDataRecv);
  esp_now_register_send_cb(OnDataSent);
}

void loop() {
  // Nothing to do here since ESP-NOW callbacks handle everything
}

The sender keeps sending '0' repeatedly. NEED URGENT HELP! T_T. Thank you in Advance!!

Welcome to the forum

What should be sent when the rotary encode is stationary and the button is not pressed ?

nothing is supposed to be sent. I only want the esp 32 to send data for one time when it registers a change in the Rotary encoder. after which i will program the receiver address to add and subtract (++ && --) to keep track of a counter for 0 (minimum) - 360 (maximum clamp), which will be remapped to 0 -180 for the 180 degree Servo motor.

have a look at esp32encoder

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.