Go Down

Topic: NodeMCU - parts of void setup() are skipped (Read 346 times) previous topic - next topic

tuila

Mar 12, 2018, 10:51 pm Last Edit: Mar 13, 2018, 07:37 pm by tuila
So I have tried 5 different NodeMCU boards now. I have correctly added esp8266 library to board manager and download the USB software to upload sketches.

I am able to upload sketches onto these boards. However, it appears that void setup is not running properly. Here is the basic test code I am running that leads me to this conclusion:

Code: [Select]
void setup() {
  // put your setup code here, to run once:
 
  Serial.begin(9600);
  
   Serial.println("Test void setup");
  
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Test loop");
  delay(1000);


Check the attachments for what happens in the serial monitor.

Essentially, the void loop runs fine but the serial.println is skipped. When I hit the reset button, this line is printed next to some gibberish. I thought maybe I could work around this for my project that will utilize and ultrasonic sensor to upload data to a Blynk app.

But, when I tried running that code:

Code: [Select]
// D = 1/2 Ã-- T Ã-- C

const int ECHOPIN = 4;
const int TRIGPIN = 3;

void setup() {
  Serial.begin(9600);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  Serial.println("Begin reading distance");
}

void loop() {
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);

  // Distance calc
  float distance = pulseIn(ECHOPIN, HIGH);
  distance = distance/58;
  Serial.print(distance);
  Serial.println(" cm");
  delay(2000);
}


Nothing printed at all on the serial monitor (including loop print statements). That is a bad sign because I tested this on the Arduino Uno and it works fine. I do not think these boards are defective because this problem has repeated 4 times. Note I did not purchase 4 consecutive boards, I ordered the first two as a pair and thought maybe that brand had a bad batch. Then they sent me a replacement, after I had ordered another one from a different brand. Each has the exact same problem.

ieee488

#1
Mar 12, 2018, 11:03 pm Last Edit: Mar 12, 2018, 11:04 pm by ieee488
You do realize serial.begin(9600); has to be called first, right ?  ? ?

tuila

Yes I had it there before and then moved it in front just to see if it would change anything. Either way it does not work and only shows up when I hit the reset button.

Delta_G

I noticed something similar in a esp8266 project I was working on.  I was able to solve it by placing a bit of delay (250ms IIRC but I didn't experiment) between the Serial.begin and the Serial.print.  That seemed to fix it.  Not sure why.  Maybe the ESP just needs some time to get things started. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

tuila

I noticed something similar in a esp8266 project I was working on.  I was able to solve it by placing a bit of delay (250ms IIRC but I didn't experiment) between the Serial.begin and the Serial.print.  That seemed to fix it.  Not sure why.  Maybe the ESP just needs some time to get things started. 
So do you mean you just used a delay(250); line in the void setup?

Delta_G

|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

tuila

Darn that  did not work. Although it did delay the print statement to come out .250 seconds after I hit the reset (after the same gibberish letters).

Go Up