Serial.print is not working on ESP32

Hello everyone! I am trying to run a program that controls a DC motor. I've tried the program and the motor runs perfectly but the serial monitor is not showing anything as I intended, not even the "Start" in the void setup. Here is my code:

const int motorPin1 = 26;
const int motorPin2 = 27;
const int buzzerPin = 25;
int motorEn = 0;

void setup() {
  Serial.begin(9600);
  Serial.print("Start!");

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorEn, OUTPUT);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorEn, 0);
}

void loop() {
  unsigned long startTime = millis();
  float counter = 0.0;

  while (millis() - startTime < 6 * 60 * 60 * 1000) {
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorEn, 255);

    counter += 4.3;

    delay(1 * 60 * 1000);

    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorEn, 0);

    delay(3 * 60 * 1000 + 11 * 1000);

    Serial.println(counter);
  }
}

Is there anything wrong with it?

Thank you in advance for your responses!

Is SerialMonitor set to 9600 too ?

I've got in the habit of adding a delay:

Serial.begin(9600);
delay(2000);
Serial.println("Ready!");

Hello, thank you for your response. Adding a delay did work but how about printing the counter? it still didn't work

That's a long time, isn't it?

Did you want:

static float counter = 0.0;

And you're mixing delay() and millis() - that makes everything weird.

Yeah, I've monitored the serial for some time to see if it works and also tried changing it to a shorter time but the serial didn't print anything

Like I said, your mixing delay() and millis() makes everything weird.

You might want to print out how long that delay is, I'm not sure how the compiler behaves for the ESP32, usually for AVR processors the arithmetic is done as a 16-bit signed integer, but the delay() function takes an unsigned 32-bit integer as an argument. Try explicitly making the numbers unsigned long.

< edit > On an UNO, that equation gives a delay of 4294961688mS.

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