From b10389238f0024c508fe0adb7014f5037ddba6bd Mon Sep 17 00:00:00 2001 From: Mitchell Pomery <bob_george33@hotmail.com> Date: Sun, 2 Nov 2014 00:04:34 +0800 Subject: [PATCH] Squashed the weird garbled output bug --- BlinkenLights.ino | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/BlinkenLights.ino b/BlinkenLights.ino index 4106d86..f990f17 100644 --- a/BlinkenLights.ino +++ b/BlinkenLights.ino @@ -69,6 +69,10 @@ char hexToChar(char* c) { return (char) strtol(c, NULL, 16); } +char cthret[3]; // We return a pointer to a character below. +// If we don't have this, then the returned value can be overwritten before we +// read it. + char* charToHex(char c) { char base_digits[16] = { @@ -85,28 +89,30 @@ char* charToHex(char c) { index++; } index--; // back up to last entry in the array - char ret[3]; + int i = 0; if (index < 0) { - ret[i] = base_digits[0]; + cthret[i] = base_digits[0]; i++; } if (index < 1) { - ret[i] = base_digits[0]; + cthret[i] = base_digits[0]; i++; } for(; index >= 0 && i < 2; index--) // go backward through array { - ret[i] = base_digits[converted_number[index]]; + cthret[i] = base_digits[converted_number[index]]; i++; } - ret[2] = '\0'; - Serial.print(ret); // NEVER REMOVE THIS LINE. OR DIE + cthret[2] = '\0'; + //Serial.print(cthret); // NEVER REMOVE THIS LINE. OR DIE // WITHOUT IT WE GET GARBLED OUTPUT HERE // YOU HAVE BEEN WARNED - return ret; + // Fixed by adding cthret + + return cthret; } // ============================ LIGHT MANIPULATION ============================ -- GitLab