/week eleven: programming
    
# concept
    
# ---------------------------------
  

following last week's assignment, i've decided to change my final project to be a remote-controlled car.

for this week, i wanted to try and use ESP-NOW to implement a method of remotely-controlling the motion of a motor using another microcontroller.

# circuit
    
# ---------------------------------
  

for the circuit, i essentially just replicated the stepper motor circuit on one side of the breadboard, and the ESP-NOW setup from this tutorial on the other side of the breadboard

i also added a button and hooked it up to the analog sensor

# code
    
# ---------------------------------
  

i began with the example code on the RandomNerd tutorial. instead of creating a custom data structure, i had the microcontroller just send an integer called "forward" — whose value was determined by analogRead(A3): in other words, whether the button was pressed.

here was the code i used for the sending microcontroller:

    #include 
    #include 
    
    uint8_t broadcastAddress[] = {0x94, 0x3C, 0xC6, 0x13, 0x97, 0x14};
    
    esp_now_peer_info_t peerInfo;
    
    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() {
      Serial.begin(115200);
     
      WiFi.mode(WIFI_STA);
    
      if (esp_now_init() != ESP_OK) {
        Serial.println("Error initializing ESP-NOW");
        return;
      }
    
      esp_now_register_send_cb(OnDataSent);
      
      memcpy(peerInfo.peer_addr, broadcastAddress, 6);
      peerInfo.channel = 0;  
      peerInfo.encrypt = false;
      
      if (esp_now_add_peer(&peerInfo) != ESP_OK){
        Serial.println("Failed to add peer");
        return;
      }
    }
     
    void loop() {
    
      int forward = analogRead(A3);
    
      Serial.println(forward);
    
      esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &forward, sizeof(forward));
       
      if (result == ESP_OK) {
        Serial.println("Sent with success");
      }
      else {
        Serial.println("Error sending the data");
      }
    
      delay(500);
    
    }

for the receiving microcontroller, i also began with the RandomNerd tutorial. i greatly simplified the code so that in the void loop(), it would execute stepper.runSpeed() (i.e. make the stepper motor move) only if the value of the "forward" variable was greater than zero.

here was the code i used for the receiving microcontroller:

    #include 
    #include 
    #include 
    
    const int stepPin = 13;
    const int dirPin = 12;
    int forward;
    
    AccelStepper stepper (1, stepPin, dirPin);
    
    void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
      memcpy(&forward, incomingData, sizeof(forward));
      Serial.println(forward);
    
    }
     
    void setup() {
      Serial.begin(115200);
      
      WiFi.mode(WIFI_STA);
    
      if (esp_now_init() != ESP_OK) {
        Serial.println("Error initializing ESP-NOW");
        return;
      }
    
      esp_now_register_recv_cb(OnDataRecv);
    }
     
    void loop() {
     
       stepper.setMaxSpeed(500);     
       stepper.setSpeed(500);  
       
       if(forward > 0){
          stepper.runSpeed();
       }
    }
# final result
    
# ---------------------------------