This is part of an project I'm working on, I have an ESP32 with an rotary encoder sending the data to an Uno, But on the Uno side the part of the data is correct.
When I rotate the rotary endcoder on the EPS32 and this displays the correct values on the display and serial port monitor.
On first power up on the ESP32 the encoder value is 0 and the signal_good is 10 and these values are sent to the uno, On the uno through the serial monitor the encoder value is 0 (which is correct) but the Signal_good is 0, Which I thought should be 10 the same at the ESP32 is sending.
When I rotate the encoder in clockwise direction the uno displays the same value as the ESP32 and again in the anti-clockwise direction on the RotateCounter value .
But when the RotateCounter value goes below 0 the siganl_good show's a -1 and when the RotateCounter goes above 0 the signal_good show's 0 again and not 10.
Not sure whats going wrong am I missing something ?
This is the ESP32 code
//SLAVE mode
#include <Arduino.h>
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include "TFT_eSPI.h"
#include "U8g2_for_TFT_eSPI.h"
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
U8g2_for_TFT_eSPI u8f; // U8g2 font instance
#include <TimedAction.h>
#include <Rotary.h>
#define ECHO_TO_SERIAL 1 //change to zero = no serial output
#define stepPin3 32 // Set 'Step' rotary encoder pins
#define stepPin4 35
#define MoveSW 34 //SW pin on the rotary encoder (Button function)
RF24 radio(12, 14); // nRF24L01 (CE,CSN)TEST
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 00; // Address of our node in Octal format ( 04,031, etc)node00
const uint16_t Stepper_node = 03; // Address of our node in Octal format ( 04,031, etc)node00
Rotary Volts_Rotary = Rotary(stepPin3, stepPin4);
int stepperRotarySteps; // This keeps track of the steps done with the rotary encoder
//###################
const byte Dac_multipliers[] = { 1, 2, 4, 6, 8 };
const byte stepCount = sizeof(Dac_multipliers) / sizeof(Dac_multipliers[0]);
volatile byte currentStep = 0;
//##################################
typedef struct Motion_struct {
int RotateCounter;
int Siganl_good ;
//boolean StepperData.Siganl_good;
} Motion_struct;
//Create a struct_message called StepperData
Motion_struct StepperData;
//Print data to serail port
void printRecord(Print* pr, char sep = ',') {
pr->print(StepperData.RotateCounter); // Print btye 7 Platform_Level
pr->print(sep);
pr->print(StepperData.Siganl_good);
pr->println();
}
void TimerService02();
TimedAction Timedact02 = TimedAction(40, TimerService02); // mS
void setup() {
tft.init();
u8f.begin(tft);
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
SPI.begin();
Serial.begin(9600);
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
pinMode(MoveSW, INPUT_PULLUP); //added 10K resisotr
pinMode(stepPin3, INPUT_PULLUP); //added 10K resisotr
pinMode(stepPin4, INPUT_PULLUP);//added 10K resisotr
attachInterrupt(digitalPinToInterrupt(stepPin3), Volts_encoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(stepPin4), Volts_encoder, CHANGE);
StepperData.RotateCounter = 0;
// StepperData.Siganl_good = true;
}
void loop() {
Buttons() ;
network.update();
//===== Sending =====//
StepperData.Siganl_good = 10;
// StepperData.Siganl_good = true;
RF24NetworkHeader header(Stepper_node); // (Address where the data is going)
bool ok = network.write(header, &StepperData, sizeof(StepperData) ); // Send the data
Timedact02.check();
// Timedact01.check();
}
void TimerService02() {
//##############################
u8f.setFontMode(0);
u8f.setForegroundColor(TFT_WHITE); // apply color// start writing at this position
u8f.setFont(u8g2_font_inb27_mf);
u8f.setCursor(10, 60);
u8f.print(StepperData.RotateCounter);
u8f.print(" ");
u8f.setCursor(150, 60);
u8f.print(Dac_multipliers[currentStep]);
#if ECHO_TO_SERIAL
printRecord(&Serial);
#endif //ECHO_TO_SERIAL
}
void Volts_encoder() {
unsigned int result = Volts_Rotary.process();
///Rotary encoder 1 for setting the volts
if (result) {
if (result == DIR_CW) {
StepperData.RotateCounter += Dac_multipliers[currentStep] ;
} else {
StepperData.RotateCounter -= Dac_multipliers[currentStep] ;
}
}
}
void Buttons() {
#define buttonPressed LOW // When the button is pressed the input will be low, this is to remove the confusion this migth cause.
uint32_t currentMillis = millis(); // Millis times uses to debounce the button
static uint32_t lastMillis; // Start of the debounce timeout
const uint32_t BOUNCETIMEOUT = 20; // Debounce time in milliseconds
bool currentButtonState = digitalRead(MoveSW); // Reads the current state of the button and saves the result in a bool
static bool lastButtonState; // Holds the previous debounced state of the button
if (lastButtonState != currentButtonState) { // Checks to see if the button has been pressed or released, at this point the button has not been debounced
if (currentMillis - lastMillis >= BOUNCETIMEOUT) { // Checks to see if the state of the button has been stable for at least bounceTimeout duration
lastButtonState = currentButtonState; // At this point the button has been debounced, so save the last state
if (currentButtonState == buttonPressed) {
currentStep++;
if ( currentStep >= stepCount ) currentStep = 0;
// The button might have been pressed or released, this make sure only presses are acted on, not releases
Serial.print("MULTIPLIER SECLECTED BY :"); // Just added to see if there is any output on power up(DEBUGING ONLY)
Serial.println(currentStep);//Dac_multipliers[]
Serial.print("MULTIPLIER BY :"); // Just added to see if there is any output on power up(DEBUGING ONLY)
Serial.println(Dac_multipliers[currentStep]);//Dac_multipliers[]
}
}
} else {
lastMillis = currentMillis; // Saves the current value of millis in last millis so the debounce timer starts from current millis
}
}
and this is the Uno code:
//node 03
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#define CE_PIN 7
#define CSN_PIN 10
RF24 radio (CE_PIN, CSN_PIN); // "myRadio" is the identifier you will use in following methods
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 03; // Address of this node in Octal format ( 04,031, etc)
boolean newData = true;
const long interval = 1500;
#define ECHO_TO_SERIAL 1 //change to zero = no serial output
//##################################
//##################################
typedef struct Motion_struct {
int RotateCounter;
int Siganl_good ;
//boolean StepperData.Siganl_good;
} Motion_struct;
//Create a struct_message called myData
Motion_struct StepperData;
//Print data to serail port
void printRecord(Print* pr, char sep = ',') {
pr->print(StepperData.RotateCounter); // Print btye 7 Platform_Level
pr->print(sep);
pr->print(StepperData.Siganl_good); // Print btye 7 Platform_Level
pr->print(sep);
pr->print(newData); // Print btye 7 Platform_Level
pr->println();
}
void setup()
{
Serial.begin(9600);
SPI.begin();
radio.begin();
network.begin(90, this_node); //(channel, node address)
radio.setDataRate(RF24_2MBPS);
}
void loop()
{
network.update();
//===== Receiving =====//
while ( network.available() ) { // Is there any incoming data?
RF24NetworkHeader header;
network.read(header, &StepperData, sizeof(StepperData)); // Read the incoming data
}
#if ECHO_TO_SERIAL
printRecord(&Serial);
#endif //ECHO_TO_SERIAL
}
and the Serial monitor from the uno
// postive direction, Ignoring the 1 at the end(not used yet).
7,0,1 // should be 7,10,1
7,0,1 // should be 7,10,1
7,0,1 // should be 7,10,1
7,0,1 // should be 7,10,1
7,0,1 // should be 7,10,1
7,0,1 // should be 7,10,1
// Negative direction
-7,-1,1 // should be -7,10,1
-7,-1,1 //should be -7,10,1
-7,-1,1 //should be -7,10,1
-7,-1,1 //should be -7,10,1
-7,-1,1 //should be -7,10,1
-7,-1,1 //should be -7,10,1
-7,-1,1 //should be -7,10,1
ESP32 output on serial monitor
// postive direction.
7,10
7,10
7,10
7,10
7,10
7,10
// Negative direction
-7,10
-7,10
-7,10
-7,10
-7,10
-7,10
-7,10