My new Serial starting code

Here is my new serial starting code. It takes some time to start printing the serial after the command is set. Usually you simply add a delay, but that delay can be longer than the time your serial takes to connect, so you’re waiting for nothing and when you’re trying to troubleshoot that becomes frustrating. The other option is the check if the serial started with while (!Serial) { delay(10); } but then your code doesn’t run without a serial connection so it just hangs if you try it off a battery or USB charger block.

Here comes the best of both worlds!

It checks for a serial connection every 10 ms but only for 3 seconds, then it goes on. The commented out line is to print how long it takes for your serial to show up. It was useful to determine 3s was a good number (I landed around 2100 to 2700 ms so 3000 gives me just a bit of padding while still being fairly fast when not plugged to a serial console.

Bonus is the giant ASCII START logo I made so I can easily see where it started when scrolling back up.

You can throw this at the top of your setup() or even better, make it a function and call it, so you only need to comment out a single line when you go testing it without a computer and save those precious 3 seconds at startup.

Enjoy!


<span style="color:#323232;">  Serial.begin(115200);
</span><span style="color:#323232;">  unsigned long time_begin = millis();
</span><span style="color:#323232;">  while (!Serial && (millis() - time_begin) < 3000) {
</span><span style="color:#323232;">    delay(10);
</span><span style="color:#323232;">    // Serial.println(millis() - time_begin);
</span><span style="color:#323232;">  }
</span><span style="color:#323232;">  delay(10);
</span><span style="color:#323232;">  Serial.println(" ");
</span><span style="color:#323232;">  Serial.println("       ::::::::   :::::::::::       :::        :::::::::   ::::::::::: ");
</span><span style="color:#323232;">  Serial.println("     :+:    :+:      :+:         :+: :+:      :+:    :+:      :+:      ");
</span><span style="color:#323232;">  Serial.println("    +:+             +:+        +:+   +:+     +:+    +:+      +:+       ");
</span><span style="color:#323232;">  Serial.println("   +#++:++#++      +#+       +#++:++#++:    +#++:++#:       +#+        ");
</span><span style="color:#323232;">  Serial.println("         +#+      +#+       +#+     +#+    +#+    +#+      +#+         ");
</span><span style="color:#323232;">  Serial.println(" #+#    #+#      #+#       #+#     #+#    #+#    #+#      #+#          ");
</span><span style="color:#323232;">  Serial.println(" ########       ###       ###     ###    ###    ###      ###           ");
</span><span style="color:#323232;">  Serial.println(" ");
</span><span style="color:#323232;">  Serial.println(" ");
</span>

PS: it looks like Lemmy can’t handle color coded markdown… :(

  • All
  • Subscribed
  • Moderated
  • Favorites
  • arduino@lemmy.ca
  • DreamBathrooms
  • ngwrru68w68
  • modclub
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • GTA5RPClips
  • JUstTest
  • ethstaker
  • normalnudes
  • tester
  • osvaldo12
  • everett
  • cubers
  • tacticalgear
  • anitta
  • provamag3
  • Leos
  • cisconetworking
  • megavids
  • lostlight
  • All magazines