How to Print ASCII value

Hello, for school homework i need to make a piece of code that counts in the serial monitor and when certain bits are 1 it also needs to add the ASCII and Binairy value.

But i am having trouble with printing the ASCII value, now it just prints "25970" when the ASCII value is asked.

 /* 
 Jasper van Roest
 9/30/2024
 gebruiken maken van unsigned long char en het lezen van bits wat een extra commando stuurt als iets aan
 bepaalde waardes voldoet.
 */
 
 
 
 int Number = 0;
 int Waarde_Bit_4;
 int Waarde_Bit_5;
 int Waarde_Bit_6;

//====================================================================================
//====================================================================================
void setup() {
Serial.begin(9600);
}

//====================================================================================
//====================================================================================

void Print_Values() {

int ASCII_Number = 'Number';
Serial.print("\t"); Serial.print(ASCII_Number); //print de ASCII waarden, dit geeft altijd hetzelfde nummer. Werkt dus niet
delay(20);
Serial.print("\t"); Serial.print(Number, BIN); // print de binaire waarde 
delay(20);
Serial.print(" \t"); Serial.print(Number); // print de decimale waarden
Serial.println();
 delay(50);

}

//====================================================================================
//====================================================================================

void loop() {
 for (unsigned char Counter = 0; Counter < 256; Counter++ ) {
Serial.print(Counter);
 delay(50);

  Number++;

 Waarde_Bit_4 = bitRead(Counter, 4);
 Waarde_Bit_5 = bitRead(Counter, 5);
 Waarde_Bit_6 = bitRead(Counter, 6);

 if ((Waarde_Bit_4 && Waarde_Bit_5) || Waarde_Bit_6 == 1) { //bit 4 EN 5 moeten 1 zijn of bit 6, dan gebeurt er iets extra's
  Print_Values();
 }
  else {Serial.println();}


 if (Counter == 255) {Number = 0;} //Nummer resetten naar nul anders blijft die doortellen tot na 255
 }

}

I also tried printing it immediately like this:

Serial.println('Number');

but that gave the same result.

That is not how you assign a value to an int type variable. Under file/preferences, set the Compile warnings to All and pay attention to the warnings.

C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2024923-21360-d9dby1.rdwp5\sketch_oct23a\sketch_oct23a.ino:28:22: warning: character constant too long for its type
   int ASCII_Number = 'Number';
                      ^~~~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2024923-21360-d9dby1.rdwp5\sketch_oct23a\sketch_oct23a.ino: In function 'void loop()':
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2024923-21360-d9dby1.rdwp5\sketch_oct23a\sketch_oct23a.ino:46:43: warning: comparison is always true due to limited range of data type [-Wtype-limits]
   for (unsigned char Counter = 0; Counter < 256; Counter++)
                                   ~~~~~~~~^~~~~

Try int ASCII_Number = Number;

what do i achieve by this? because now i just give ASCII_Number the value of Number.
but that is not what i need, i need the actual ASCII value of Number

You need to store it as a char rather than an int. The print function will print it out based on its data type, so if you don't have it set for a character, it's not going to display a character.

char Number = 0;

You can then alter it by either way:

Number = 48;
Number = '0';

These lines both assign the same value to Number.

Serial.print("Number: ");
Serial.println(Number);

The above will now output:

Number: 0

Hope this helps

Thank you!
that is what i needed to know. Turns out i still need to learn my data types :slight_smile:

Final Code:

 /* 
 Jasper van Roest
 10/23/2024
 gebruiken maken van unsigned long char en het lezen van bits wat een extra commando stuurt als iets aan
 bepaalde waardes voldoet.
 */
 
 
 
 int Number = 0;
 int Waarde_Bit_4;
 int Waarde_Bit_5;
 int Waarde_Bit_6;

//====================================================================================
//====================================================================================
void setup() {
Serial.begin(9600);
}

//====================================================================================
//====================================================================================

void Print_Values() {

char ASCII_Number = Number;
Serial.print("\t"); Serial.print(ASCII_Number); //print de ASCII waarden, dit geeft altijd hetzelfde nummer. Werkt dus niet
delay(20);
Serial.print("\t"); Serial.print(Number, BIN); // print de binaire waarde 
delay(20);
Serial.print(" \t"); Serial.print(Number); // print de decimale waarden
Serial.println();
 delay(50);

}

//====================================================================================
//====================================================================================

void loop() {
 for (unsigned char Counter = 0; Counter < 256; Counter++ ) {
Serial.print(Counter);
 delay(50);

  Number++;

 Waarde_Bit_4 = bitRead(Counter, 4);
 Waarde_Bit_5 = bitRead(Counter, 5);
 Waarde_Bit_6 = bitRead(Counter, 6);

 if ((Waarde_Bit_4 && Waarde_Bit_5) || Waarde_Bit_6 == 1) { //bit 4 EN 5 moeten 1 zijn of bit 6, dan gebeurt er iets extra's
  Print_Values();
 }
  else {Serial.println();}


 if (Counter == 255) {Number = 0;} //Nummer resetten naar nul anders blijft die doortellen tot na 255
 }

}

2 Likes

Glad I could help! Happy coding

I forgot to mention, you could also just type cast in most cases, which can save you a variable and lines of code if you only need that value as another data type for one instance. It would look like this in your case:

Serial.print("\t"); Serial.print((char)Number);

This just tells your compiler that it should treat the value as a char instead of its int data type.

Doesn't necessarily optimize a program as simple as that, but it's good to know!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.