…there is my sketch for ESP-NOW reseiver as ‘‘ a tank’’ … and I try to add it as code into a code field :
//ESP8266_now_receiver_AD174_po_17_tank_en.ino
// some as a tank controlled with two joysticks
// used GPIO for ESP8266 NodeMCU V1.0
// ESP Board MAC Address: 50:02:91:EF:E6:06 NodeMCU V1.0 po
/*
Thanks to Rui Santos & Sara Santos and their Random Nerd Tutorials
Complete ESP-NOW details at https://RandomNerdTutorials.com/esp-now-esp32-arduino-ide/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
*/
// reprogrammed for my AD174 with ESP8266 NodeMCU V1.0 by PavelOu
#include <ESP8266WiFi.h>
#include <espnow.h>
//bool serialOn = false;
String sketchName = "ESP8266_now_receiver_AD174_po_17_tank_en.ino";
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message
{
int dataR;
int dataL;
int dataSW;
} struct_message;
// Create a struct_message called myData
struct_message myData;
long int timeLast = 0;
int timeOut = 1000;
bool dataOn = false;
//byte = 0;
bool joystickCalibration = false;
bool carOn = false;
bool blinkOn = false;
//byte minLed = 1;
//byte carDirectFB = 0;
//definition from AD174 are changed for NodeMcu V1.0
int gpLF = 13; // Left forward D7 // Two wheels left forward
int gpLB = 14; // Left back D5 // Two wheels left backward
int gpRF = 15; // Right forward D8 // Two wheels right forward
int gpRB = 12; // Right back D6 // Two wheels right backward
int motoryR = 4; // Enable Right D2 // PWM from 0 to 255
int motoryL = 5; // Enable Left D1 // PWM from 0 to 255
int carPowerOnLed = 2; // D4 data off LED_BUILT_IN reversed level
int carDataOnLed = 2; // D4 - " -
int carOnLed = 16; // D0 indikace car on
//NodeMCU free outputs for Leds
//GPIO00 = D3 = Flash Output
//GPIO16 = D0 = Wake
//GPIO02 = D4 = Output + LED_BUILTIN, too
const byte carStop = 0;
byte speedMax = 235;
int carDirectLFB = 0;
int carDirectRFB = 0;
int carSW = 0; // = 0 = carOff , = 1 carOn
int osaR = 0; // axe R = speed Right
int osaRzero = 2200;
int osaRtolerance = 80;
int osaL = 0; // axe L = speed Left
int osaLzero = 2200;
int osaLtolerance = 80;
int osaLFB = 0; // recounted on position value speedL
int osaRFB = 0; // - " -
// rychlosti pro motory
byte speedL = 0;
byte speedR = 0;
void InitMotors() //changed from AD174
{
pinMode(gpLF, OUTPUT); //Left Wheels Forward
pinMode(gpLB, OUTPUT); //Left Wheels Backward
pinMode(gpRB, OUTPUT); //Right Wheels Forward
pinMode(gpRF, OUTPUT); //Right WheelsBackward
digitalWrite(gpLF, LOW);
digitalWrite(gpLB, LOW);
digitalWrite(gpRF, LOW);
digitalWrite(gpRB, LOW);
pinMode(motoryL, OUTPUT);
pinMode(motoryR, OUTPUT);
digitalWrite(motoryL, 0);
digitalWrite(motoryR, 0);
}
void CarSim()
{
Serial.println("Car test stop");
digitalWrite(gpLF,LOW); //set gpLF high level
digitalWrite(gpLB,LOW); //set gpLB Low level
digitalWrite(gpRB,LOW); //set gpRB Low level
digitalWrite(gpRF,LOW); //set gpRF high level
analogWrite(motoryR,0);
analogWrite(motoryL,0);
delay(1000);
Serial.println("Car test acceleration");
for(int i = 0; i <= speedMax; i++)
{
analogWrite(motoryL,i);
analogWrite(motoryR,i);
delay(10);
}
//delay(1000);
Serial.println("Car reduce speed");
for(int i = speedMax; i >= 0; i--)
{
analogWrite(motoryL,i);
analogWrite(motoryR,i);
delay(10);
}
Serial.println("Car is stopped");
digitalWrite(gpLF,LOW); //set gpLF high level
digitalWrite(gpLB,LOW); //set gpLB Low level
digitalWrite(gpRF,LOW); //set gpRF high level
digitalWrite(gpRB,LOW); //set gpRB Low level
analogWrite(motoryL,0);
analogWrite(motoryR,0);
delay(1000);
}
void CarTest()
{
// put your main code here, to run repeatedly
Serial.println("Car will goes forward");
digitalWrite(gpLB,LOW); //set gpLB Low level
digitalWrite(gpRB,LOW); //set gpRB Low level
digitalWrite(gpLF,HIGH); //set gpLF high level
digitalWrite(gpRF,HIGH); //set gpRF high level
//accelerate
for(int i = 0; i <= speedMax; i++)
{
analogWrite(motoryL,i);
analogWrite(motoryR,i);
delay(10);
}
//delay(1000);
//Reduce speed
for(int i = speedMax; i >= 0; i--)
{
analogWrite(motoryL,i);
analogWrite(motoryR,i);
delay(10);
}
Serial.println("Car will stop");
digitalWrite(gpLF,LOW); //set gpLF high level
digitalWrite(gpLB,LOW); //set gpLB Low level
digitalWrite(gpRF,LOW); //set gpRF high level
digitalWrite(gpRB,LOW); //set gpRB Low level
analogWrite(motoryL,0);
analogWrite(motoryR,0);
delay(1000);
Serial.println("Car will goes backward");
digitalWrite(gpLF,LOW); //set gpLF high level
digitalWrite(gpRF,LOW); //set gpRF high level
digitalWrite(gpLB,HIGH); //set gpLB Low level
digitalWrite(gpRB,HIGH); //set gpRB Low level
//accelerate
for(int i = 0; i <= speedMax; i++)
{
analogWrite(motoryL,i);
analogWrite(motoryR,i);
delay(10);
}
//delay(2000);
//Reduce speed
for(int i = speedMax; i >= 0; i--)
{
analogWrite(motoryL,i);
analogWrite(motoryR,i);
delay(10);
}
// car will stop
Serial.println("Car will stop");
digitalWrite(gpLF,LOW); //set gpLF high level
digitalWrite(gpLB,LOW); //set gpLB Low level
digitalWrite(gpRF,LOW); //set gpRF high level
digitalWrite(gpRB,LOW); //set gpRB Low level
analogWrite(motoryL,0);
analogWrite(motoryR,0);
delay(1000);
}
void MoveCar(byte newDirect) //for variant of receiver one and two
{
if (newDirect == 0) //stop
{
//Serial.println("Car will stop");
digitalWrite(gpLB,LOW); //set gpLB Low level
digitalWrite(gpRB,LOW); //set gpRB Low level
digitalWrite(gpLF,LOW); //set gpLF high level
digitalWrite(gpRF,LOW); //set gpRF high level
analogWrite(motoryR,0);
analogWrite(motoryL,0);
}
if (newDirect == 1) //forward
{
//Serial.println("Car will goes forward");
digitalWrite(gpLB,LOW); //set gpLB Low level
digitalWrite(gpRB,LOW); //set gpRB Low level
digitalWrite(gpLF,HIGH); //set gpLF high level
digitalWrite(gpRF,HIGH); //set gpRF high level
//digitalWrite(carBackLed,LOW);
}
if (newDirect == 2) //bacward
{
//Serial.println("Car will goes backward");
digitalWrite(gpLB,HIGH); //set gpLB Higy level
digitalWrite(gpRB,HIGH); //set gpRB High level
digitalWrite(gpLF,LOW); //set gpLF Low level
digitalWrite(gpRF,LOW); //set gpRF LOW level
//digitalWrite(carBackLed,HIGH);
}
if (newDirect == 3) //turn left
{
Serial.println("Car will turn left");
digitalWrite(gpLB,HIGH); //set gpLB Higy level
digitalWrite(gpRB,LOW); //set gpRB High level
digitalWrite(gpLF,LOW); //set gpLF Low level
digitalWrite(gpRF,HIGH); //set gpRF LOW level
}
if (newDirect == 4) //turn right
{
Serial.println("Car will turn right");
digitalWrite(gpLB,LOW); //set gpLB Higy level
digitalWrite(gpRB,HIGH); //set gpRB High level
digitalWrite(gpLF,HIGH); //set gpLF Low level
digitalWrite(gpRF,LOW); //set gpRF LOW level
}
}
void MoveLeftWheels(int directLW) //for third variant of receiver
{
if (directLW == 1)
{
//Serial.println("Car will goes forward");
digitalWrite(gpLF,HIGH); //set gpLF high level
digitalWrite(gpLB,LOW); //set gpLB low level
}
if (directLW == 0)
{
//Serial.println("Car will goes forward");
digitalWrite(gpLF,LOW); //set gpLF Low level
digitalWrite(gpLB,LOW); //set gpLB Low level
}
if (directLW == -1)
{
digitalWrite(gpLF,LOW); //set gpLF low level
digitalWrite(gpLB,HIGH); //set gpLB high level
}
}
void MoveRightWheels(int directRW) //for third variant of receiver
{
if (directRW == 1)
{
//Serial.println("Car will goes forward");
digitalWrite(gpRF,HIGH); //set gpRF high level
digitalWrite(gpRB,LOW); //set gpRB high level
}
if (directRW == 0)
{
//Serial.println("Car will goes forward");
digitalWrite(gpRF,LOW); //set gpRF Low level
digitalWrite(gpRB,LOW); //set gpRB Low level
}
if (directRW == -1)
{
digitalWrite(gpRF,LOW); //set gpRF low level
digitalWrite(gpRB,HIGH); //set gpRB high level
}
}
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len)
{
memcpy(&myData, incomingData, sizeof(myData));
osaR = myData.dataR; // speedR = speed right wheels
osaL = myData.dataL; // speedL = speed left wheels
carSW = myData.dataSW;
dataOn = true;
digitalWrite(carDataOnLed,HIGH);
if (carSW == 0)
{
carOn = false;
joystickCalibration = false;
digitalWrite(carOnLed,LOW);
}
if (carSW == 1)
{
if (!carOn)
{
if (!joystickCalibration)
{
osaLzero = osaL;
osaRzero = osaR;
joystickCalibration = true;
}
carOn = true;
digitalWrite(carOnLed,HIGH);
}
}
analogWriteResolution(8);
carDirectLFB = 0;
osaLFB = osaL - osaLzero;
if (osaLFB<0) // Y < 0 = left wheels Backward
{
osaLFB = osaLzero - osaL;
if (osaLFB > osaLtolerance) // axe Y < 0 = carBackward
{
carDirectLFB = -1;
}
}
else
{
if (osaLFB > osaLtolerance) // carForward
{
carDirectLFB = 1;
}
}
MoveLeftWheels(carDirectLFB);
int speedintL = map(osaLFB,0,osaLzero,0,speedMax);
speedL = constrain(speedintL,0,speedMax);
carDirectRFB = 0;
osaRFB = osaR - osaRzero;
if (osaRFB<0) // X < 0 = right wheels go backward
{
osaRFB = osaRzero - osaR;
if (osaRFB > osaRtolerance) // speed RFB > tolerance
{
carDirectRFB = -1;
}
}
else
{
if (osaRFB > osaRtolerance) // car wheels right go forward
{
carDirectRFB = 1;
}
}
MoveRightWheels(carDirectRFB);
int speedintR = map(osaRFB,0,osaRzero,0,speedMax);
speedR = constrain(speedintR,0,speedMax);
if (carOn)
{
analogWrite(motoryL,speedL);
analogWrite(motoryR,speedR);
}
else
{
//MoveCar(carDirectFB);
carDirectLFB = carStop;
MoveLeftWheels(carDirectLFB);
carDirectRFB = carStop;
MoveRightWheels(carDirectRFB);
analogWrite(motoryL,0);
analogWrite(motoryR,0);
}
timeLast = millis();
}
void setup()
{
// Initialize Serial Monitor
Serial.begin(9600);
delay(1000);
//before using io pin, pin mode must be set first
Serial.println(sketchName);
InitMotors();
//pinMode(espnowLED, OUTPUT);
pinMode(carOnLed, OUTPUT);
pinMode(carDataOnLed, OUTPUT);
pinMode(carPowerOnLed, OUTPUT);
//delay(1000);
Serial.println("ESP8266_now_receiver_AD174_po_10.ino");
Serial.println("Test of outputs - indication:");
digitalWrite(carPowerOnLed,LOW); // reversed levels built in Led
delay(1000);
digitalWrite(carPowerOnLed,HIGH); // - " -
delay(1000);
digitalWrite(carDataOnLed,HIGH);
delay(1000);
digitalWrite(carDataOnLed,LOW);
delay(1000);
digitalWrite(carOnLed,HIGH);
delay(1000);
digitalWrite(carOnLed,LOW);
delay(1000);
Serial.println("Test motoru");
//CarTest();
analogWriteResolution(8);
CarSim();
Serial.println("Preliminary calibration of joystick ");
//osaR = analogRead(motoryL);
Serial.print("osaLzero: ");
Serial.println(osaLzero); // speed left zero
Serial.print("osaRzero: ");
Serial.println(osaRzero); // speed right zero
//speedY = 1;
//speedX = 1;
speedL = 1;
speedR = 1;
//speedLR = 1;
carOn = false;
joystickCalibration = false;
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != 0)
{
Serial.println("Error initializing ESP-NOW");
//digitalWrite(espnowLED,HIGH);
return;
}
else
{
digitalWrite(carPowerOnLed,LOW); // reversed level
Serial.println("ESP-NOW is initialized");
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
//timeLast = millis();
digitalWrite(carDataOnLed,LOW);
digitalWrite(carOnLed,LOW);
Serial.println("There is end of serial communication for work in better real time");
delay(5000);
}
void loop()
{
long int timeActual = millis() - timeLast;
if (timeActual > timeOut)
{
// watch dog
if (!dataOn)
{
digitalWrite(carDataOnLed,LOW);
}
else
{
dataOn = false;
}
timeLast = millis();
}
}
… and I see it seems to be well …