Vs code PlatfromIO/ while loop not working

Hi !
It’s my first time using PlatformIO. I’ve encountered a problem. When I uploaded a code with an while loop, and print something out with Serial.println, I can not get an output on the Serial monitor. I’ve tried using different boards, Arduino IDE, but they behave the same. Also I have checked the serial port, baud rate, matching board. After hours trying to find the problem, I’ve noticed the Arduino boards that wasn’t programmed by Vs code PIO, it works fine when programmed by Arduino IDE. Any help? Thanks!

#include <Arduino.h>

int i = 0;

void setup() {

  Serial.begin(9600);

   

}

void loop() {

   while(i < 3) {

      Serial.println("hello world");

      

      i = 1 + i;   

      

   }

   

}

Can you show a log or screenshot of what happens when you build and upload to the board, via the left PlatformIO sidebar (alien) and Project Tasks → Upload?

Also what’s the full platformio.ini of your project and with what exact board are you working with?

1 Like

Hi ! Thanks for your reply!

The steps I took
open vs code > PIO home > open > under quick access > + New Project > inside project wizard Board: Arduino Yun, Framework: Arduino > src > main.cpp

oh and I forgot to mention that, when I remove the " i = i +1;" in the while loop, I was able to get an output from the serial monitor.

Terminal

> Executing task: C:\Users\W10\.platformio\penv\Scripts\pio.exe run --target upload <

Processing yun (platform: atmelavr; board: yun; framework: arduino)
----------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION:
PLATFORM: Atmel AVR (3.0.0) > Arduino Yun
HARDWARE: ATMEGA32U4 16MHz, 2.50KB RAM, 28KB Flash
DEBUG: Current (simavr) On-board (simavr)
PACKAGES:
 - framework-arduino-avr 5.1.0
 - tool-avrdude 1.60300.200527 (6.3.0)
 - toolchain-atmelavr 1.50400.190710 (5.4.0)
LDF: Library Dependency Finder -> //bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 5 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio\build\yun\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   6.4% (used 165 bytes from 2560 bytes)
Flash: [=         ]  12.9% (used 3700 bytes from 28672 bytes)
Configuring upload protocol...
AVAILABLE: avr109
CURRENT: upload_protocol = avr109
Looking for upload port...
Auto-detected: COM6
Forcing reset using 1200bps open/close on port COM6
Waiting for the new upload port...
Uploading .pio\build\yun\firmware.hex

Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% -0.00s

avrdude: Device signature = 0x1e9587 (probably m32u4)
avrdude: reading input file ".pio\build\yun\firmware.hex"
avrdude: writing flash (3700 bytes):

Writing | ################################################## | 100% 0.27s

avrdude: 3700 bytes of flash written
avrdude: verifying flash memory against .pio\build\yun\firmware.hex:
avrdude: load data flash data from input file .pio\build\yun\firmware.hex:
avrdude: input file .pio\build\yun\firmware.hex contains 3700 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.03s

avrdude: verifying ...
avrdude: 3700 bytes of flash verified

avrdude: safemode: Fuses OK (E:C8, H:D8, L:FF)

avrdude done.  Thank you.

====================================== [SUCCESS] Took 3.46 seconds ======================================

Terminal will be reused by tasks, press any key to close it.

PlatformIO serial monitor

> Executing task: C:\Users\W10\.platformio\penv\Scripts\pio.exe device monitor <

--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at 
could not open port 'COM6': could not open port 'COM6': PermissionError(13, None, 5)
The terminal process "C:\Users\W10\.platformio\penv\Scripts\pio.exe 'device', 'monitor'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

> Executing task: C:\Users\W10\.platformio\penv\Scripts\pio.exe device monitor <

--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at 
--- Miniterm on COM6  9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---

also

> Executing task: C:\Users\W10\.platformio\penv\Scripts\pio.exe device monitor <

--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at 

--- Available ports:
--- Enter port index or full name: com6
--- Miniterm on com6  9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---

My guess would just be that you run the serial monitor after the output has already appeared, and the serial monitor doesn’t reset the board to re-execute the firmware.

Can you

  1. Add Serial.println("Starting firmware"); at the end of setup() to have an indication of when the firmware starts
  2. After Uploading and opening the serial monitor, press the reset button on the board to reset it and observe the serial output
  3. Alternatively add a delay(3000); in setup() and use the project task “Upload and Monitor” to not have a big delay between uploading and opening the serial monitor
1 Like

Hi! sorry for the late reply…
like you mentioned above, I’ve made changes to the code. The delay under Serial.begin, if I give it 3000 it seems fifty fifty chance of it working. 5000 is working flawless! Thanks for your help!

#include <Arduino.h>

int i = 0;

void setup() {

Serial.begin(9600);

delay(5000);

Serial.println(“Starting firmware”);

}

void loop() {

while(i < 3) {

  Serial.println("hello world");

  

  i = 1 + i;   

}

}

1 Like