Feedback on expandible digit display

Hello everyone,
I made an expandable digit display and I am wondering what you think of it. (As you can maybe see) I am new to coding and made this myself. I did not copy anything from the internet. Does anyone has any tips for me to improve this code?
If you press ‘q’ on your keyboard, the number will increase.

In advance, thank you!

Digit d1, d2, d3;  //Each single digit
int  n1, n2, n3;   //The number that the digit needs to display

float sc = 2;

void setup() {
  size(1500, 500); 
  float x = width/2;
  float y = height/2;
  d1 = new Digit(x+150*sc, y, sc);
  d2 = new Digit(x, y, sc, #E227E5);
  d3 = new Digit(x-150*sc, y, sc);
  d1.sketch();
  d2.sketch();
  d3.sketch();
}



void draw() {
  if ( n1 >= 10) {
    n2 += 1;
    n1 = 0;
  }
  if (n2 >= 10) {
    n3 += 1;
    n2 = 0;
  }
  d1.number = n1;
  d2.number = n2;
  d3.number = n3;

  d1.execute();
  d2.execute();
  d3.execute();
}

void keyPressed() {
  if (key == 'q') {
    n1 += 1;
  }
}

class Digit {
  float posx, posy, size;
  color c_off = #B9B9B9; //color of the number when off, is the begin/standard color
  color c_on = #0BA2B4;  //Standard color of the number when on
  Digit(float x, float y, float scale) {
    posx = x;
    posy = y;
    size = scale;
  }
  Digit(float x, float y, float scale, color on) { //extra value added to change the color of specific digits. 
    posx = x;
    posy = y;
    size = scale;
    c_on = on;   //color of the number when on.
  }

  PShape n, nm;
  void sketch() {      //Sketches a part of the digit
    n = createShape();  //The edge shape of the digit
    n.beginShape();
    n.noStroke();
    n.fill(c_off);
    n.vertex(0, 0);
    n.vertex(100, 0);
    n.vertex(80, 20);
    n.vertex(20, 20);
    n.endShape(CLOSE);

    nm = createShape();  //The middle shape of the digit
    nm.beginShape();
    nm.noStroke();
    nm.fill(c_off);
    nm.vertex(15, 0);
    nm.vertex(95, 0);
    nm.vertex(110, 12.5);
    nm.vertex(95, 25);
    nm.vertex(15, 25);
    nm.vertex(0, 12.5);
    nm.endShape(CLOSE);
  }

  //t = top, m = middle, rt = right top, rb = right bodum, b = bodum, lb = left bodum, lt = left top.
  //false = the part of the digit is off. true = the part of the digit is on.
  boolean t, m, rt, rb, b, lb, lt = false; 

  //The number that the digit has to display
  int number;
  void execute() {
    switch(number) {  //Determines which part of the digit are turned on.
    case 0:
      t = true;
      m = false;
      rt = true;
      rb = true;
      b = true;
      lb = true;
      lt = true;
      break;
    case 1:
      t = false;
      m = false;
      rt = true;
      rb = true;
      b = false;
      lb = false;
      lt = false;
      break;
    case 2:
      t = true;
      m = true;
      rt = true;
      rb = false;
      b = true;
      lb = true;
      lt = false;
      break;
    case 3:
      t = true;
      m = true;
      rt = true;
      rb = true;
      b = true;
      lb = false;
      lt = false;
      break;
    case 4:
      t = false;
      m = true;
      rt = true;
      rb = true;
      b = false;
      lb = false;
      lt = true;
      break;
    case 5:
      t = true;
      m = true;
      rt = false;
      rb = true;
      b = true;
      lb = false;
      lt = true;
      break;
    case 6:
      t = true;
      m = true;
      rt = false;
      rb = true;
      b = true;
      lb = true;
      lt = true;
      break;
    case 7:
      t = true;
      m = false;
      rt = true;
      rb = true;
      b = false;
      lb = false;
      lt = false;
      break;
    case 8:
      t = true;
      m = true;
      rt = true;
      rb = true;
      b = true;
      lb = true;
      lt = true;
      break;
    case 9:
      t = true;
      m = true;
      rt = true;
      rb = true;
      b = true;
      lb = false;
      lt = true;
      break;
    default:
      t = false;
      m = true;
      rt = false;
      rb = false;
      b = false;
      lb = false;
      lt = false;
      break;
    }
    //The full digit gets drawn with the appropriate color (off or on).
    pushMatrix();    
    translate(posx, posy);
    scale(size);
    shapeMode(CENTER);
    if (t == true) {
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, 0, -100);  //Top
    if (m == true) {
      nm.setFill(c_on);
    } else {
      nm.setFill(c_off);
    }
    shape(nm, 0, 0);   //Middle
    pushMatrix();
    rotate(PI/2);
    if (rt == true) {    //Right top
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, -55, -50);  

    if (rb == true) {
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, 55, -50);  //Right bodum
    popMatrix();
    pushMatrix();
    rotate(PI);
    if (b == true) {    //bodum side
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, 0, -100);
    popMatrix();
    pushMatrix();
    rotate(PI*1.5);
    if (lt == true) {   //left top
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, 55, -50);

    if (lb == true) {    //Left bodum
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, -55, -50);
    popMatrix();
    popMatrix();
  }
}

Hi there SWijnen,

Welcome to the Processing forum, good to have you here.

It might be interesting to try and figure out how you could use arrays for your class instances. Currently you declare, initialize, and call the methods for each instance separately, but it might be nice to do this automatically. When using an array you could use for loops to iterate through all the instances rather than repeating similar lines of code such as these:

  d1.execute();
  d2.execute();
  d3.execute();

I get the impression you like figuring things out yourself, so I’ll give you some starting points rather than explaining how to do it:

It’s likely you’ve already gone through these pages, but now study them from a different angle: How could you combine arrays and classes?

Hopefully this helps. Good luck and have fun :slight_smile:

1 Like

Thank you for the tip! I have improved my code. (I left the last part out because that is still the same.) I now also made improvements on the counting system. That it will automatically start changing the next digit.

Digit[] d;  //Each single digit
int[] n; //The number that the digit needs to display

int nmbr = 4;  //The number of digits

float sc = 0.6;

void setup() {
  size(1500, 500); 
  float x = width/2;
  float y = height/2;

  d = new Digit[nmbr];
  n = new int[nmbr];

  d[0] = new Digit(x+150*sc, y, sc, #000000); //d[0] is always the most right digit.
  d[1] = new Digit(x, y, sc, #FA640D);
  d[2] = new Digit(x-150*sc, y, sc);
  d[3] = new Digit(x-150*sc*2, y, sc, #BAC614);

  for (int i = 0; i < nmbr; i++) {
    d[i].sketch();
  }
}

void draw() {
  for (int w = 0; w < nmbr-1; w++) {
    if ( n[w] >= 10) {
      n[w+1] += 1;
      n[w] = 0;
    }
  }

  for (int i = 0; i < nmbr; i++) {
    d[i].number = n[i];
    d[i].execute();
  }

void keyPressed() {
  if (key == 'q') {
    n[0] += 1;
  }
}
1 Like

If you want an “adjustable Array”, you can use the ArrayList, too:

https://processing.org/reference/ArrayList.html
https://forum.processing.org/one/topic/how-to-use-arraylist.html
https://www.youtube.com/watch?v=HnSJZ4qTcwY

Hey everyone,

I made some extra adjustments to my code so that it is easier to use. You can now set ‘counter’ to each value, and the digit display will display it. (if it has enough digits of course).

If you have any feedback on my code, feel free to let me know!


Digit[] d;  //Each single digit. d[0] is the most right digit. And d["last"] is the most left digit.

int[] countsplit; //The number that each single digit needs to display
int counter; //the total number that will be displayed.
int countermax = 99999;
int countermin = 0;

int quant = 5;  //The number of digits

float sc = 0.6;

void setup() {
  size(1500, 500); 
  float x = width/2;
  float y = height/2;

  d = new Digit[quant];
  countsplit = new int[quant];
  
  //All the digits of the digit display.
  d[0] = new Digit(x+150*sc, y, sc, #15CB3D);  //The most right digit.
  d[1] = new Digit(x, y, sc);
  d[2] = new Digit(x-150*sc, y, sc);
  d[3] = new Digit(x-150*sc*2, y, sc);
  d[4] = new Digit(x-150*sc*3, y, sc);        //The most left digit

  for (int i = 0; i < quant; i++) {
    d[i].sketch();
  }
  counter = 12079;  //The number that the digit display will display.
}

void draw() {
  //This splits the total counter in single numbers. 
  for (int i = 0; i < quant; i++) {
    countsplit[i] = int((counter % pow(10, (i+1)))/pow(10, i));
  }

  //Gives the right number to the right digit.
  for (int i = 0; i < quant; i++) {
    d[i].number = countsplit[i];
    d[i].execute();
  }
}

void keyPressed() {
  if (counter > countermin) {
    if (key == 'q') {
      counter -= 1;
    }
  }
  if ( counter < countermax) {
    if (key == 'w') {
      counter += 1;
    }
  }
}


class Digit {
  float posx, posy, size;
  color c_off = #B9B9B9; //color of the number when off, is the begin/standard color
  color c_on = #0BA2B4;  //Standard color of the number when on
  Digit(float x, float y, float scale) {
    posx = x;
    posy = y;
    size = scale;
  }
  Digit(float x, float y, float scale, color on) { //extra value added to change the color of specific digits. 
    posx = x;
    posy = y;
    size = scale;
    c_on = on;   //color of the number when on.
  }

  PShape n, nm;
  void sketch() {      //Sketches a part of the digit
    n = createShape();  //The edge shape of the digit
    n.beginShape();
    n.noStroke();
    n.fill(c_off);
    n.vertex(0, 0);
    n.vertex(100, 0);
    n.vertex(80, 20);
    n.vertex(20, 20);
    n.endShape(CLOSE);

    nm = createShape();  //The middle shape of the digit
    nm.beginShape();
    nm.noStroke();
    nm.fill(c_off);
    nm.vertex(15, 0);
    nm.vertex(95, 0);
    nm.vertex(110, 12.5);
    nm.vertex(95, 25);
    nm.vertex(15, 25);
    nm.vertex(0, 12.5);
    nm.endShape(CLOSE);
  }

  //t = top, m = middle, rt = right top, rb = right bodum, b = bodum, lb = left bodum, lt = left top.
  //false = the part of the digit is off. true = the part of the digit is on.
  boolean t, m, rt, rb, b, lb, lt = false; 

  //The number that the digit has to display
  int number;
  void execute() {
    switch(number) {  //Determines which part of the digit are turned on.
    case 0:
      t = true;
      m = false;
      rt = true;
      rb = true;
      b = true;
      lb = true;
      lt = true;
      break;
    case 1:
      t = false;
      m = false;
      rt = true;
      rb = true;
      b = false;
      lb = false;
      lt = false;
      break;
    case 2:
      t = true;
      m = true;
      rt = true;
      rb = false;
      b = true;
      lb = true;
      lt = false;
      break;
    case 3:
      t = true;
      m = true;
      rt = true;
      rb = true;
      b = true;
      lb = false;
      lt = false;
      break;
    case 4:
      t = false;
      m = true;
      rt = true;
      rb = true;
      b = false;
      lb = false;
      lt = true;
      break;
    case 5:
      t = true;
      m = true;
      rt = false;
      rb = true;
      b = true;
      lb = false;
      lt = true;
      break;
    case 6:
      t = true;
      m = true;
      rt = false;
      rb = true;
      b = true;
      lb = true;
      lt = true;
      break;
    case 7:
      t = true;
      m = false;
      rt = true;
      rb = true;
      b = false;
      lb = false;
      lt = false;
      break;
    case 8:
      t = true;
      m = true;
      rt = true;
      rb = true;
      b = true;
      lb = true;
      lt = true;
      break;
    case 9:
      t = true;
      m = true;
      rt = true;
      rb = true;
      b = true;
      lb = false;
      lt = true;
      break;
    default:
      t = false;
      m = true;
      rt = false;
      rb = false;
      b = false;
      lb = false;
      lt = false;
      break;
    }
    //The full digit gets drawn with the appropriate color (off or on).
    pushMatrix();    
    translate(posx, posy);
    scale(size);
    shapeMode(CENTER);
    if (t == true) {
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, 0, -100);  //Top
    if (m == true) {
      nm.setFill(c_on);
    } else {
      nm.setFill(c_off);
    }
    shape(nm, 0, 0);   //Middle
    pushMatrix();
    rotate(PI/2);
    if (rt == true) {    //Right top
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, -55, -50);  

    if (rb == true) {
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, 55, -50);  //Right bodum
    popMatrix();
    pushMatrix();
    rotate(PI);
    if (b == true) {    //bodum side
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, 0, -100);
    popMatrix();
    pushMatrix();
    rotate(PI*1.5);
    if (lt == true) {   //left top
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, 55, -50);

    if (lb == true) {    //Left bodum
      n.setFill(c_on);
    } else {
      n.setFill(c_off);
    }
    shape(n, -55, -50);
    popMatrix();
    popMatrix();
  }
}

nice, possibly following can shorten it?

int  counti = 12079; //___________________________________________ a 5 digit number we want to display

boolean T = true, F = false; //___________________________________ not need to much typing
boolean[][] seven_segment = { 
  //t = top, m = middle, rt = right top, rb = right bottom, b = bottom, lb = left bottom, lt = left top.
  { T, F, T, T, T, T, T }, // 0 
  { F, F, T, T, F, F, F }, // 1 
  { T, T, T, F, T, T, F }, // 2 
  { T, T, T, T, T, F, F }, // 3 
  { F, T, T, T, F, F, T }, // 4 
  { T, T, F, T, T, F, T }, // 5 
  { T, T, F, T, T, T, T }, // 6 
  { T, F, T, T, F, F, F }, // 7 
  { T, T, T, T, T, T, T }, // 8   
  { T, T, T, T, T, F, T }, // 9
  { F, F, F, F, F, F, F }  // DEFAULT OFF [10]
};

String counts = str(counti); //___________________________________ number to string
for ( int i = 0; i < counts.length(); i++) { //___________________ look for each digit in the numbers string
  char c = counts.charAt(i); //___________________________________ get its character at this position
  int n = Character.getNumericValue( c ); //______________________ make it a number again
  print( c, n, "_"); //___________________________________________ print both
  for ( int k=0; k<7; k++) print( seven_segment[n][k] +"," ); //__ and its truth table "segment ON"
  println();
}

shows

1 1 _false,false,true,true,false,false,false,
2 2 _true,true,true,false,true,true,false,
0 0 _true,false,true,true,true,true,true,
7 7 _true,false,true,true,false,false,false,
9 9 _true,true,true,true,true,false,true,
1 Like

add i have the idea to plan for / use 6 digits so it can be better used for clock…

also should use the official definition of the segments for a seven segment module
and optional HEX show of it.
( so i changed your truth table array )

int  counti = 0;//120790; //___________________________________________ a 6 digit number we want to display
String counts = str6right(counti); //______________________________ number to string

boolean T = true, _ = false; //___________________________________ BAD style but not need too much typing
//________________________________________________________________ https://en.wikipedia.org/wiki/Seven-segment_display
boolean[][] seven_segment = { //__________________________________ revision to official HEX encoding
  // [0] A: t = top, 
  // [1] B: rt = right top, 
  // [2] C: rb = right bottom, 
  // [3] D: b = bottom, 
  // [4] E: lb = left bottom, 
  // [5] F: lt = left top.
  // [6] G: m = middle, 
  { T, T, T, T, T, T, _ }, // 0
  { _, T, T, _, _, _, _ }, // 1
  { T, T, _, T, T, _, T }, // 2
  { T, T, T, T, _, _, T }, // 3
  { _, T, T, _, _, T, T }, // 4
  { T, _, T, T, _, T, T }, // 5
  { T, _, T, T, T, T, T }, // 6
  { T, T, T, _, _, _, _ }, // 7
  { T, T, T, T, T, T, T }, // 8
  { T, T, T, T, _, T, T }, // 9
  //__________________________________________________________________ for HEX use
  { T, T, T, _, T, T, T }, // 10 A
  { _, _, T, T, T, T, T }, // 11 b
  { T, _, _, T, T, T, _ }, // 12 C
  { _, T, T, T, T, _, T }, // 13 d
  { T, _, _, T, T, T, T }, // 14 E
  { T, _, _, _, T, T, T }, // 15 F
  { _, _, _, _, _, _, _ }  // DEFAULT OFF [16]
};
boolean diagp = false;//true; //____________________________________ diagnostic printing
boolean clock = true;//false; //____________________________________ show 23:59:59
boolean hex   = false;//true; //____________________________________ show hex number
int lastsec=0;
int x0=50, y0=height/2, h = 110, w = 190; //________________________ layout
String s, ni, nh;

//__________________________________________________________________ basic segments
PShape nm; //_______________________________________________________ one LED
color c_off = color(0, 0, 100);
color c_on  = color(100, 180, 0);

void segment() { //_________________________________________________ a part of the digit
  nm = createShape(); //____________________________________________ The middle shape of the digit
  nm.beginShape();
  nm.noStroke();
  nm.fill(c_off);
  nm.vertex(13, 0);
  nm.vertex(97, 0);
  nm.vertex(110, 12.5);
  nm.vertex(97, 25);
  nm.vertex(13, 25);
  nm.vertex(0, 12.5);
  nm.endShape(CLOSE);
}

void digit(int pos, int n) { //_____________________________________ all 7 segments of one digit
  x0=50; 
  y0=height/2; 
  h = 110; 
  w = 190; //__________________________ layout
  if ( diagp ) {
    print("digit pos ", pos, " n ", n, "_ ");
    for ( int k=0; k<7; k++) print( seven_segment[n][k] +"," ); //____ and its truth table "segment ON"
    println();
  }
  push();
  translate( x0+pos * w, y0);

  if ( seven_segment[n][0] ) nm.setFill(c_on);
  else                       nm.setFill(c_off);
  shape(nm, 0, -h); //______________ A: t [0]
  if ( seven_segment[n][6] ) nm.setFill(c_on);
  else                       nm.setFill(c_off);
  shape(nm, 0, 0);  //______________ G: m [6]
  if ( seven_segment[n][3] ) nm.setFill(c_on);
  else                       nm.setFill(c_off);
  shape(nm, 0, h);  //______________ D: b [3]
  push();
  rotate( radians(-90) );
  if ( seven_segment[n][5] ) nm.setFill(c_on);
  else                       nm.setFill(c_off);
  shape(nm, -12.5, -12.5); //_______ F: lt [5]
  if ( seven_segment[n][1] ) nm.setFill(c_on);
  else                       nm.setFill(c_off);
  shape(nm, -12.5, -12.5+h); //_____ B: rt [1]
  if ( seven_segment[n][4] ) nm.setFill(c_on);
  else                       nm.setFill(c_off);
  shape(nm, -12.5-h, -12.5); //_____ E: lb [4]
  if ( seven_segment[n][2] ) nm.setFill(c_on);
  else                       nm.setFill(c_off);
  shape(nm, -12.5-h, -12.5+h); //___ C: rb [2]
  pop();

  pop();
}

void digits() {
  for ( int i = 0; i < counts.length(); i++) {
    char c = counts.charAt(i); //___________________________________ get its character at this position
    int n = 16;
    if ( c > 47 && c < 58 ) n = Character.getNumericValue( c ); //__ 0 .. 9 make it a number again
    else if ( c == 'a' ) n = 10; //_________________________________ hex use
    else if ( c == 'b' ) n = 11;
    else if ( c == 'c' ) n = 12;
    else if ( c == 'd' ) n = 13;
    else if ( c == 'e' ) n = 14;
    else if ( c == 'f' ) n = 15;
    else if ( c == ' ' ) n = 16; //__________________________________ default empty / right justified
    digit(i, n);
  }
  add_clockticks(); //_______________________________________________ show clock ":" or number ni or hex ni + nh
}

void setup() {
  size(1160, 300);
  segment(); //_____________________________________________________ create basic shape
  if ( !clock ) println("number: "+counti+"\nany key press ++");
  //noLoop();
}

void draw() {
  background(0, 0, 80);
  show_clock();
  digits();
}

void keyPressed() {
  counti++;
  println("number: "+counti);
  counts = str6right(counti);
}

String str6right(int n) { //__________________________________________ format numbers a integer or hex, but not for clock!
  s = "      "; //____________________________________________________ fill left with blanks
  ni = str(n); //_____________________________________________________ 0123456789
  nh = Integer.toHexString(n); //_____________________________________ 0123456789adcdef
  if ( hex )      s += nh;
  else            s += ni;
  int chars = s.length();
  if (diagp ) println(s);
  s=s.substring(chars-6, chars); //___________________________________ use the right 6 chars only
  return s;
}

void show_clock() {
  if ( clock ) {
    int newsec = second();
    if ( lastsec != newsec ) { //_____________________________________ update clock
      counts = nows();
      lastsec = newsec;
    }
  }
}

void add_clockticks() { //____________________________________________ if clock make the 2 ':'
  if ( clock ) {
    push();
    fill(c_on);
    text(dates(), 10, 15); //_________________________________________ show small date time
    translate(2*w, 200);
    rotate( radians(-45) );
    fill(c_on);
    rect(0, 0, 15, 15);
    rect(40, -40, 15, 15);
    pop();
    push();
    translate(4*w, 200);
    rotate( radians(-45) );
    fill(c_on);
    rect(0, 0, 15, 15);
    rect(40, -40, 15, 15);
    pop();
  }
  if ( ! clock ) {
    push();
    fill(c_on);
    if ( hex )   text( " DEC " + ni+" HEX "+nh, 10, 15); //_____________ or number and HEX value
    else         text( " DEC " + ni, 10, 15); //______________________ or number only
    pop();
  }
}

String nows() {
  return ""+nf(hour(), 2)+nf(minute(), 2)+nf(second(), 2);
}

String dates() {
  return ""+year()+"_"+nf(month(), 2)+"_"+nf(day(), 2)+" "+nf(hour(), 2)+":"+nf(minute(), 2)+":"+nf(second(), 2);
}


1 Like