My new Serial starting code - eviltoast

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!

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

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