Int to char for display

I am trying to convert an int to a char to be displayed as ASCII but I cannot figure out how to get it to work. I am sure there is an answer to this already but I cannot find one that works. Any help would be greatly appreciated. Thank you

int b
char display_b

if (vertical <= 450){
  b++;
  delay(200);
}
if (b >= 10){
  b = 0;
}
display_b = b;

alpha4.writeDigitAscii(0, display_b);
alpha4.writeDisplay();
  int a = 65; // ASCII equivalent of A
  Serial.write(a);

or

  int a = 9;
  Serial.write(a + '0');

or

  int a = 9;
  Serial.write(a + 48);

What is it that you are writing to, ie, what is alpha4 and which library is it using ?

I am using an Adafruit 7-segment LED display with featherwings. It uses the Adafruit_GFX and Adafruit_LEDBackpack libraries. A link to the product is below

int bb;
char display_bb [10] = {};

void setup ()
{
  Serial.begin(19200);
  Serial.println("ready. . .");
}

void loop ()
{
  bb = 9;
  itoa(bb, display_bb, 10);
  Serial.println(display_bb);
  delay(1000);
    
  bb = 1001;
  itoa(bb, display_bb, 10);
  Serial.println(display_bb);
  delay(1000);
  
  bb = 2002;
  itoa(bb, display_bb, 10);
  Serial.println(display_bb);
  delay(2000);
}

Thank you for the reply. I modified this code to send display_bb to my display but the char is still empty so it doesn't display anything. Would you have any advice on how to change this?

I'm not sure how that could be - I ran it and the Serial.println executes as per plan.

I guess
alpha4.writeDigitAscii(0, display_b);
is supposed to make digit_0 register a char value
and
alpha4.writeDisplay();
is supposed to update the output register
? ?
I have no experience with that library.

these are using a HT16K33.
I have a library for the HT16K33 which is based on the common Print.h interface:
https://werner.rothschopf.net/201909_arduino_ht16k33.htm

it can print integers with a simple

alpha4.print(b);

and all other types supported by Print.h

I found a way to get it to work. Not the nicest looking solution but it works for what I need it to do. Thanks for all your help everyone

// look at these websites
// https://learn.adafruit.com/adafruit-7-segment-led-featherwings/arduino-usage
// https://forum.arduino.cc/t/using-an-adafruit-7-segment-display-with-i2c-backpack/462141

#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"

Adafruit_AlphaNum4 alpha4_blue = Adafruit_AlphaNum4();  // changed name; might be able to use two at a time this way
Adafruit_AlphaNum4 alpha4_red = Adafruit_AlphaNum4();

// pins for joystick
const int VERT = 0; // analog A0
const int HORIZ = 1; // analog A1
const int SEL = 2; // digital S2
int vertical, horizontal, select;

int score; // number to manipulate with vertical input for RED
int score_b; // number to manupulate with horizontal input for BLUE
int slot_select;  // slot select variable; so score can be displayed
char display_score_0, display_score_1, display_score_2, display_score_3; // score to be displayed RED
char ds_b_0, ds_b_1, ds_b_2, ds_b_3; // score to be displayed BLUE


void setup() {

  // make the SEL line an input
  pinMode(SEL, INPUT_PULLUP);
  // set up serial port for output
  Serial.begin(9600);

slot_select = 0;  // give slot select initial value


alpha4_red.begin(0x71);  // pass in the address - RED(0x71), BLUE(0x72)

  alpha4_red.writeDigitRaw(3, 0x0);
  alpha4_red.writeDigitRaw(0, 0xFFFF);
  alpha4_red.writeDisplay();
  delay(200);
  alpha4_red.writeDigitRaw(0, 0x0);
  alpha4_red.writeDigitRaw(1, 0xFFFF);
  alpha4_red.writeDisplay();
  delay(200);
  alpha4_red.writeDigitRaw(1, 0x0);
  alpha4_red.writeDigitRaw(2, 0xFFFF);
  alpha4_red.writeDisplay();
  delay(200);
  alpha4_red.writeDigitRaw(2, 0x0);
  alpha4_red.writeDigitRaw(3, 0xFFFF);
  alpha4_red.writeDisplay();
  delay(200);
  
  alpha4_red.clear();
  alpha4_red.writeDisplay();

// blue display
alpha4_blue.begin(0x72);  // pass in the address - RED(0x71), BLUE(0x72)

  alpha4_blue.writeDigitRaw(3, 0x0);
  alpha4_blue.writeDigitRaw(0, 0xFFFF);
  alpha4_blue.writeDisplay();
  delay(200);
  alpha4_blue.writeDigitRaw(0, 0x0);
  alpha4_blue.writeDigitRaw(1, 0xFFFF);
  alpha4_blue.writeDisplay();
  delay(200);
  alpha4_blue.writeDigitRaw(1, 0x0);
  alpha4_blue.writeDigitRaw(2, 0xFFFF);
  alpha4_blue.writeDisplay();
  delay(200);
  alpha4_blue.writeDigitRaw(2, 0x0);
  alpha4_blue.writeDigitRaw(3, 0xFFFF);
  alpha4_blue.writeDisplay();
  delay(200);
  
  alpha4_blue.clear();
  alpha4_blue.writeDisplay();

}

char displaybuffer[4] = {' ', ' ', ' ', ' '};
int a;  // integer for slot_select
int r_1, r_2, r_3; // intergers for red score
int b_1, b_2, b_3; // integers for blue score


void loop_1() {
  if (select == HIGH) {
    Serial.println('WORKING');  }
}

void loop() {


  // scroll down display
  displaybuffer[0] = displaybuffer[1];
  displaybuffer[1] = displaybuffer[2];
  displaybuffer[2] = displaybuffer[3];
  displaybuffer[3] = displaybuffer[0];
  // set every digit to the buffer
  alpha4_red.writeDigitAscii(0, displaybuffer[0]);
    alpha4_blue.writeDigitAscii(0, displaybuffer[0]);
  alpha4_red.writeDigitAscii(1, displaybuffer[1]);
    alpha4_blue.writeDigitAscii(1, displaybuffer[1]);
  alpha4_red.writeDigitAscii(2, displaybuffer[2]);
    alpha4_blue.writeDigitAscii(2, displaybuffer[2]);
  alpha4_red.writeDigitAscii(3, displaybuffer[3]);
    alpha4_blue.writeDigitAscii(3, displaybuffer[3]);
  delay(200);

// read all values from the joystick
  vertical = analogRead(VERT); // will be 0-1023
  horizontal = analogRead(HORIZ); // will be 0-1023
  select = digitalRead(SEL); // will be HIGH (1) if not pressed, and LOW (0) if pressed

  // print out the values
  Serial.print("vertical: ");
  Serial.print(vertical, DEC);
  Serial.print(" horizontal: ");
  Serial.print(horizontal, DEC);
  Serial.print(" select: ");
  if (select == HIGH) {
    Serial.println(slot_select);  }
  else {
    Serial.println("PRESSED!");  }


// // // // button for slot selection to write score
if (select == LOW){   // if select is pressed, a increases
  a++;
  slot_select = a;
  delay(200);}
if (slot_select >= 4){  // set_slot select back to 0
  slot_select = 0;
  a = 0;}
else {  // sets slot_select to a if slot_select <= 0
  slot_select = a;}
// // // // button for slot selection to write score

///////////increment score BLUE: from horizontal

  /////////////////////////////// slot 1
if (horizontal >= 750 && slot_select == 1){
  b_1++;
  score_b = b_1;
  delay(200);}
if (horizontal <= 200 && slot_select == 1){
  b_1--;
  score_b = b_1;
  delay(200);}
if (b_1 >= 10 || b_1 <= 0){
  b_1 = 0;}
  /////////////////////////////// slot 2
if (horizontal >= 750 && slot_select == 2){
  b_2++;
  score_b = r_2;
  delay(200);}
if (horizontal <= 200 && slot_select == 2){
  b_2--;
  score_b = b_2;
  delay(200);}
if (b_2 >= 10 || b_2 <= 0){
  b_2 = 0;}
/////////////////////////////// slot 3
if (horizontal >= 750 && slot_select == 3){
  b_3++;
  score_b = b_3;
  delay(200);}
if (horizontal <= 200 && slot_select == 3){
  b_3--;
  score_b = b_3;
  delay(200);}
if (b_3 >= 10 || b_3 <= 0){
  b_3 = 0;}


/////////// increment score RED: from vertical. watch the direction of the Joystick!!

  /////////////////////////////// slot 1
if (vertical <= 250 && slot_select == 1){
  r_1++;
  score = r_1;
  delay(200);}
if (vertical >= 800 && slot_select == 1){
  r_1--;
  score = r_1;
  delay(200);}
if (r_1 >= 10 || r_1 <= 0){
  r_1 = 0;}
  /////////////////////////////// slot 2
if (vertical <= 250 && slot_select == 2){
  r_2++;
  score = r_2;
  delay(200);}
if (vertical >= 800 && slot_select == 2){
  r_2--;
  score = r_2;
  delay(200);}
if (r_2 >= 10 || r_2 <= 0){
  r_2 = 0;}
/////////////////////////////// slot 3
if (vertical <= 250 && slot_select == 3){
  r_3++;
  score = r_3;
  delay(200);}
if (vertical >= 800 && slot_select == 3){
  r_3--;
  score = r_3;
  delay(200);}
if (r_3 >= 10 || r_3 <= 0){
  r_3 = 0;}

//////////////////////////////////////////////////////////////////////////////// shitty way to do it?
if (a == 0){display_score_0 = ' ';}
if (a == 1){display_score_0 = '.';}
if (a == 2){display_score_0 = '-';}
if (a == 3){display_score_0 = '/';}

//////////////red display scores
if (r_1 == 0){display_score_1 = '0';}   // r_1
if (r_1 == 1){display_score_1 = '1';}
if (r_1 == 2){display_score_1 = '2';}
if (r_1 == 3){display_score_1 = '3';}
if (r_1 == 4){display_score_1 = '4';}
if (r_1 == 5){display_score_1 = '5';}
if (r_1 == 6){display_score_1 = '6';}
if (r_1 == 7){display_score_1 = '7';}
if (r_1 == 8){display_score_1 = '8';}
if (r_1 == 9){display_score_1 = '9';}
if (r_2 == 0){display_score_2 = '0';}   // r_2
if (r_2 == 1){display_score_2 = '1';}
if (r_2 == 2){display_score_2 = '2';}
if (r_2 == 3){display_score_2 = '3';}
if (r_2 == 4){display_score_2 = '4';}
if (r_2 == 5){display_score_2 = '5';}
if (r_2 == 6){display_score_2 = '6';}
if (r_2 == 7){display_score_2 = '7';}
if (r_2 == 8){display_score_2 = '8';}
if (r_2 == 9){display_score_2 = '9';}
if (r_3 == 0){display_score_3 = '0';}   // r_3
if (r_3 == 1){display_score_3 = '1';}
if (r_3 == 2){display_score_3 = '2';}
if (r_3 == 3){display_score_3 = '3';}
if (r_3 == 4){display_score_3 = '4';}
if (r_3 == 5){display_score_3 = '5';}
if (r_3 == 6){display_score_3 = '6';}
if (r_3 == 7){display_score_3 = '7';}
if (r_3 == 8){display_score_3 = '8';}
if (r_3 == 9){display_score_3 = '9';}
//////////////red display scores

//////////////blue display scores
if (b_1 == 0){ds_b_1 = '0';}   // b_1
if (b_1 == 1){ds_b_1 = '1';}
if (b_1 == 2){ds_b_1 = '2';}
if (b_1 == 3){ds_b_1 = '3';}
if (b_1 == 4){ds_b_1 = '4';}
if (b_1 == 5){ds_b_1 = '5';}
if (b_1 == 6){ds_b_1 = '6';}
if (b_1 == 7){ds_b_1 = '7';}
if (b_1 == 8){ds_b_1 = '8';}
if (b_1 == 9){ds_b_1 = '9';}
if (b_2 == 0){ds_b_2 = '0';}   // b_2
if (b_2 == 1){ds_b_2 = '1';}
if (b_2 == 2){ds_b_2 = '2';}
if (b_2 == 3){ds_b_2 = '3';}
if (b_2 == 4){ds_b_2 = '4';}
if (b_2 == 5){ds_b_2 = '5';}
if (b_2 == 6){ds_b_2 = '6';}
if (b_2 == 7){ds_b_2 = '7';}
if (b_2 == 8){ds_b_2 = '8';}
if (b_2 == 9){ds_b_2 = '9';}
if (b_3 == 0){ds_b_3 = '0';}   // b_3
if (b_3 == 1){ds_b_3 = '1';}
if (b_3 == 2){ds_b_3 = '2';}
if (b_3 == 3){ds_b_3 = '3';}
if (b_3 == 4){ds_b_3 = '4';}
if (b_3 == 5){ds_b_3 = '5';}
if (b_3 == 6){ds_b_3 = '6';}
if (b_3 == 7){ds_b_3 = '7';}
if (b_3 == 8){ds_b_3 = '8';}
if (b_3 == 9){ds_b_3 = '9';}
//////////////blue display scores
//////////////////////////////////////////////////////////////////////////////// shitty way to do it?

////////// write to display RED
alpha4_red.writeDigitAscii(0, display_score_0);
alpha4_red.writeDigitAscii(1, display_score_1);
alpha4_red.writeDigitAscii(2, display_score_2);
alpha4_red.writeDigitAscii(3, display_score_3);
alpha4_red.writeDisplay();
////////// write to display RED

////////// write to display BLUE
alpha4_blue.writeDigitAscii(0, display_score_0);
alpha4_blue.writeDigitAscii(1, ds_b_1);
alpha4_blue.writeDigitAscii(2, ds_b_2);
alpha4_blue.writeDigitAscii(3, ds_b_3);
alpha4_blue.writeDisplay();
////////// write to display BLUE

//alpha4.writeDigitRaw(0, 0x0);     // Send - character position [first value], data value [second value] - to the display buffer 
//alpha4.writeDigitRaw(1, a);       // Send - character position [first value: left to right -> 0,1,2,3], data [second value] - to the display buffer.
//alpha4.writeDisplay();            // Write the display buffer to the physical display




}

Your code contains a lot of repeating lines like this:

Characters in C are just integers, they can also be added. In order to convert an integer from 0 to 9 into the corresponding symbol, just add its value to the symbol '0':

if (r_1 >= 0 && r_1 <= 9)  display_score_1 = r_1 +  '0';

This line is completely equivalent to the nine lines above

Do the same for r_2, r_3 and b_1 - b_3 and your code will become significantly shorter

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