Uploading sketches on the Arduino Nano ESP32 has always been different for me compared to doing so on other Arduino boards. It's super slow but in the past, after the compiling, the board would disconnect and reconnect after uploading is done - and it would work.
Now every time the sketch is uploaded the board won't reconnect automatically, needing me to reset the board, causing the code to not run at all. The LED blinks in an RGB pattern.
I had similar issues and in the past I could solve them by pressing the reset button twice, and it would couple of times before having to repeat the process. It also involved reopening Arduino IDE, unplugging and plugging everything.
Now pressing reset twice doesn't help anymore and somehow pressing once now gives me this phasing green blink. The same issue persists here where it would disconnect before finishing up the upload. I read that it is a boot loader mode, so I tried to reset the boot loader and download the firmware again. I short B1 and GND as given the instruction and the firmware downloading worked, but it didn't fix anything...
This is the code that I was planning to upload
// Importing external libraries
#include <ESP32Servo.h>
#include <Encoder.h>
// Declare pinout
const int p1_encoderPinA = D2;
const int p1_encoderPinB = D3;
const int p1_servoRPin = D4;
const int p1_servoLPin = D5;
const int receiverPin_A0 = A0;
const int signalPuffer = -5; // Increase puffer if servo twitches at end positions
const int averageFactor = 2; // Increase factor for more stable inputs, but slower computation
const int p1_minConstrainValue = 1000 - signalPuffer;
const int p1_maxConstrainValue = 1500 - signalPuffer;
const int p1_minServoSpeed = 55; // lowest == 0
const int p1_maxServoSpeed = 125; // highest == 180
const int stopServoValue = 90;
const int p1_maxEncoderPosition = 40; // Change according to how far P1 is allowed to roll
// Variables for interruptISR && encoder
unsigned long p1_lastIncReadTime = micros();
unsigned long p1_lastDecReadTime = micros();
volatile int p1_encoderPosition = 0;
int p1_encoderTargetPosition = 0;
// Create encoder
Encoder p1_encoder(p1_encoderPinA, p1_encoderPinB);
// Create servos
Servo p1_servoR;
Servo p1_servoL;
void setup() {
// Setup role for each pin
pinMode(receiverPin_A0, INPUT_PULLUP);
pinMode(p1_encoderPinA, INPUT_PULLUP);
pinMode(p1_encoderPinB, INPUT_PULLUP);
pinMode(p1_servoRPin, OUTPUT);
pinMode(p1_servoLPin, OUTPUT);
// Configure servos
p1_servoR.attach(p1_servoRPin);
p1_servoL.attach(p1_servoLPin);
// Enable external interrupt
attachInterrupt(digitalPinToInterrupt(p1_encoderPinA), p1_encoderISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(p1_encoderPinB), p1_encoderISR, CHANGE);
Serial.begin(9600);
}
void loop() {
// Gather <averageFactor> amount of samples and return median value
int A0_averageReceiverValue = 0;
for (int i = 0; i < averageFactor; i++) {
A0_averageReceiverValue += pulseIn(receiverPin_A0, HIGH);
}
A0_averageReceiverValue /= averageFactor;
// Create segments that correspond to distinct <p1_encoderTargetPosition> to reference goal
int p1_mappedReceiverValue = constrain(A0_averageReceiverValue, p1_minConstrainValue, p1_maxConstrainValue);
float p1_fluctuationConstant = (p1_maxConstrainValue - p1_minConstrainValue) / p1_maxEncoderPosition;
if (p1_mappedReceiverValue > p1_minConstrainValue) {
for (int i = 1; i != p1_maxEncoderPosition; i++) {
float maxFluctuationConverter = p1_minConstrainValue + i * p1_fluctuationConstant;
float minFluctuationConverter = maxFluctuationConverter - p1_fluctuationConstant;
if (p1_mappedReceiverValue <= maxFluctuationConverter && p1_mappedReceiverValue > minFluctuationConverter) {
p1_encoderTargetPosition = i;
} else if (p1_mappedReceiverValue > maxFluctuationConverter) {
p1_encoderTargetPosition = p1_maxEncoderPosition - 1;
}
}
}
// Future failsafe redundancy; measure current spike to detect resistance and reset encoderPosition
// Rotate servo according to relation between <p1_encoderPosition> and <p1_encoderTargetPosition>
// Failsafe feature so that <p1_encoderPosition> doesn't breach boundary
if (p1_encoderPosition >= 0 && p1_encoderPosition <= p1_maxEncoderPosition) {
if (p1_encoderPosition < p1_encoderTargetPosition) {
// Reverse following if roational direction is reversed
p1_servoR.write(p1_minServoSpeed);
p1_servoL.write(p1_maxServoSpeed);
} else if (p1_encoderPosition > p1_encoderTargetPosition) {
// Reverse following if roational direction is reversed
p1_servoR.write(p1_maxServoSpeed);
p1_servoL.write(p1_minServoSpeed);
} else {
Serial.println("+-+-+Target position reached+-+-+");
}
} else {
// Failsafe feature so that <p1_encoderPosition> doesn't breach boundary
Serial.println("***WARNING, OUT OF BOUNDARY***");
if (p1_encoderPosition > p1_maxEncoderPosition) {
int overstep = p1_maxEncoderPosition - p1_encoderPosition;
p1_encoderPosition += overstep;
} else {
int overstep = p1_encoderPosition;
p1_encoderPosition -= overstep;
}
}
Serial.print("Stabilized A0 signal: ");
Serial.println(A0_averageReceiverValue);
Serial.print("P1 Encoder target position: ");
Serial.println(p1_encoderTargetPosition);
Serial.print(">>> P1 Encoder position: ");
Serial.println(p1_encoderPosition);
}
void p1_encoderISR() {
static uint8_t old_AB = 3;
static int8_t encval = 0;
static const int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
old_AB <<=2;
if (digitalRead(p1_encoderPinA)) old_AB |= 0x02;
if (digitalRead(p1_encoderPinB)) old_AB |= 0x01;
encval += enc_states[( old_AB & 0x0f )];
if(encval > 3) {
int changevalue = 1;
p1_lastIncReadTime = micros();
p1_encoderPosition = p1_encoderPosition + changevalue;
encval = 0;
}
else if(encval < -3) {
int changevalue = -1;
p1_lastDecReadTime = micros();
p1_encoderPosition = p1_encoderPosition + changevalue;
encval = 0;
}
}
I really can't think of ways to fix this issue now. It's possible to upload the same code to my Arduino Nano so I think the issues's with the ESP32.
Can anyone who has experience help?
Thank you for your time