Transform a "rookie" code to a more professional looking code - tricks asked

Good evening everyone,

I come back here asking your help.

I tried to design a code in order to help my son to create custom characters on a lcd screen with a arduino.

The idea is to have a 5*8 frame and check the rectangles you want black to create your own special character.Then a arduino type code is automatically generated in the console and you just have to copy and paste it in your arduino IDE.

My question : my code works but is very very long, and I wonder I should use a array or something like that to make it shorter and more “beautiful” ans learn more " coding-proof skills to improve myself and learn more.
But I have a lot of variables, conditions,things to do and I can’t figure out how to use these tiny tricks in this huge mess…

Do you have any ideas to start with ??

Thanks a lot

Best regards


// all rect switches variables

boolean clic11 = false;
boolean clic12 = false;
boolean clic13 = false;
boolean clic14 = false;
boolean clic15 = false;

boolean clic21 = false;
boolean clic22 = false;
boolean clic23 = false;
boolean clic24 = false;
boolean clic25 = false;

boolean clic31 = false;
boolean clic32 = false;
boolean clic33 = false;
boolean clic34 = false;
boolean clic35 = false;

boolean clic41 = false;
boolean clic42 = false;
boolean clic43 = false;
boolean clic44 = false;
boolean clic45 = false;

boolean clic51 = false;
boolean clic52 = false;
boolean clic53 = false;
boolean clic54 = false;
boolean clic55 = false;

boolean clic61 = false;
boolean clic62 = false;
boolean clic63 = false;
boolean clic64 = false;
boolean clic65 = false;

boolean clic71 = false;
boolean clic72 = false;
boolean clic73 = false;
boolean clic74 = false;
boolean clic75 = false;

boolean clic81 = false;
boolean clic82 = false;
boolean clic83 = false;
boolean clic84 = false;
boolean clic85 = false;

int offset = 25;
int side = 50;

void setup() {



void draw () {
  for (int i = 1; i<=5;i++) {for (int j = 1 ; j<=8;j++) {rect(i*side,j*side,side,side);}}

//rect become black
  if (clic11==true) {fill(0);rect(1*side,1*side,50,50);}
  if (clic12==true) {fill(0);rect(2*side,1*side,50,50);}
  if (clic13==true) {fill(0);rect(3*side,1*side,50,50);}
  if (clic14==true) {fill(0);rect(4*side,1*side,50,50);}
  if (clic15==true) {fill(0);rect(5*side,1*side,50,50);}
  if (clic21==true) {fill(0);rect(1*side,2*side,50,50);}
  if (clic22==true) {fill(0);rect(2*side,2*side,50,50);}
  if (clic23==true) {fill(0);rect(3*side,2*side,50,50);}
  if (clic24==true) {fill(0);rect(4*side,2*side,50,50);}
  if (clic25==true) {fill(0);rect(5*side,2*side,50,50);}
  if (clic31==true) {fill(0);rect(1*side,3*side,50,50);}
  if (clic32==true) {fill(0);rect(2*side,3*side,50,50);}
  if (clic33==true) {fill(0);rect(3*side,3*side,50,50);}
  if (clic34==true) {fill(0);rect(4*side,3*side,50,50);}
  if (clic35==true) {fill(0);rect(5*side,3*side,50,50);}
  if (clic41==true) {fill(0);rect(1*side,4*side,50,50);}
  if (clic42==true) {fill(0);rect(2*side,4*side,50,50);}
  if (clic43==true) {fill(0);rect(3*side,4*side,50,50);}
  if (clic44==true) {fill(0);rect(4*side,4*side,50,50);}
  if (clic45==true) {fill(0);rect(5*side,4*side,50,50);}
  if (clic51==true) {fill(0);rect(1*side,5*side,50,50);}
  if (clic52==true) {fill(0);rect(2*side,5*side,50,50);}
  if (clic53==true) {fill(0);rect(3*side,5*side,50,50);}
  if (clic54==true) {fill(0);rect(4*side,5*side,50,50);}
  if (clic55==true) {fill(0);rect(5*side,5*side,50,50);}
  if (clic61==true) {fill(0);rect(1*side,6*side,50,50);}
  if (clic62==true) {fill(0);rect(2*side,6*side,50,50);}
  if (clic63==true) {fill(0);rect(3*side,6*side,50,50);}
  if (clic64==true) {fill(0);rect(4*side,6*side,50,50);}
  if (clic65==true) {fill(0);rect(5*side,6*side,50,50);}
  if (clic71==true) {fill(0);rect(1*side,7*side,50,50);}
  if (clic72==true) {fill(0);rect(2*side,7*side,50,50);}
  if (clic73==true) {fill(0);rect(3*side,7*side,50,50);}
  if (clic74==true) {fill(0);rect(4*side,7*side,50,50);}
  if (clic75==true) {fill(0);rect(5*side,7*side,50,50);}
  if (clic81==true) {fill(0);rect(1*side,8*side,50,50);}
  if (clic82==true) {fill(0);rect(2*side,8*side,50,50);}
  if (clic83==true) {fill(0);rect(3*side,8*side,50,50);}
  if (clic84==true) {fill(0);rect(4*side,8*side,50,50);}
  if (clic85==true) {fill(0);rect(5*side,8*side,50,50);}
 // convert boolean to 1 or 0 variables
  int pos11 = clic11?1:0;int pos12 = clic12?1:0;int pos13 = clic13?1:0;int pos14 = clic14?1:0;int pos15 = clic15?1:0;
  int pos21 = clic21?1:0;int pos22 = clic22?1:0;int pos23 = clic23?1:0;int pos24 = clic24?1:0;int pos25 = clic25?1:0;
  int pos31 = clic31?1:0;int pos32 = clic32?1:0;int pos33 = clic33?1:0;int pos34 = clic34?1:0;int pos35 = clic35?1:0;
  int pos41 = clic41?1:0;int pos42 = clic42?1:0;int pos43 = clic43?1:0;int pos44 = clic44?1:0;int pos45 = clic45?1:0;
  int pos51 = clic51?1:0;int pos52 = clic52?1:0;int pos53 = clic53?1:0;int pos54 = clic54?1:0;int pos55 = clic55?1:0;
  int pos61 = clic61?1:0;int pos62 = clic62?1:0;int pos63 = clic63?1:0;int pos64 = clic64?1:0;int pos65 = clic65?1:0;
  int pos71 = clic71?1:0;int pos72 = clic72?1:0;int pos73 = clic73?1:0;int pos74 = clic74?1:0;int pos75 = clic75?1:0;
  int pos81 = clic81?1:0;int pos82 = clic82?1:0;int pos83 = clic83?1:0;int pos84 = clic84?1:0;int pos85 = clic85?1:0;

// text binary code on the right side of the frame

 // create arduino code from the picture in the console
 println(" ");
 println("byte nom[8] = {");
 println(" ");

  void mouseReleased() {
    // locate rect and interact with + boolean switches
    if (dist(mouseX,mouseY,1*side+offset,1*side+offset)<=offset) {clic11 =! clic11;}
    if (dist(mouseX,mouseY,2*side+offset,1*side+offset)<=offset) {clic12 =! clic12;}
    if (dist(mouseX,mouseY,3*side+offset,1*side+offset)<=offset) {clic13 =! clic13;}
    if (dist(mouseX,mouseY,4*side+offset,1*side+offset)<=offset) {clic14 =! clic14;}
    if (dist(mouseX,mouseY,5*side+offset,1*side+offset)<=offset) {clic15 =! clic15;}
    if (dist(mouseX,mouseY,1*side+offset,2*side+offset)<=offset) {clic21 =! clic21;}
    if (dist(mouseX,mouseY,2*side+offset,2*side+offset)<=offset) {clic22 =! clic22;}
    if (dist(mouseX,mouseY,3*side+offset,2*side+offset)<=offset) {clic23 =! clic23;}
    if (dist(mouseX,mouseY,4*side+offset,2*side+offset)<=offset) {clic24 =! clic24;}
    if (dist(mouseX,mouseY,5*side+offset,2*side+offset)<=offset) {clic25 =! clic25;}
    if (dist(mouseX,mouseY,1*side+offset,3*side+offset)<=offset) {clic31 =! clic31;}
    if (dist(mouseX,mouseY,2*side+offset,3*side+offset)<=offset) {clic32 =! clic32;}
    if (dist(mouseX,mouseY,3*side+offset,3*side+offset)<=offset) {clic33 =! clic33;}
    if (dist(mouseX,mouseY,4*side+offset,3*side+offset)<=offset) {clic34 =! clic34;}
    if (dist(mouseX,mouseY,5*side+offset,3*side+offset)<=offset) {clic35 =! clic35;}
    if (dist(mouseX,mouseY,1*side+offset,4*side+offset)<=offset) {clic41 =! clic41;}
    if (dist(mouseX,mouseY,2*side+offset,4*side+offset)<=offset) {clic42 =! clic42;}
    if (dist(mouseX,mouseY,3*side+offset,4*side+offset)<=offset) {clic43 =! clic43;}
    if (dist(mouseX,mouseY,4*side+offset,4*side+offset)<=offset) {clic44 =! clic44;}
    if (dist(mouseX,mouseY,5*side+offset,4*side+offset)<=offset) {clic45 =! clic45;}
    if (dist(mouseX,mouseY,1*side+offset,5*side+offset)<=offset) {clic51 =! clic51;}
    if (dist(mouseX,mouseY,2*side+offset,5*side+offset)<=offset) {clic52 =! clic52;}
    if (dist(mouseX,mouseY,3*side+offset,5*side+offset)<=offset) {clic53 =! clic53;}
    if (dist(mouseX,mouseY,4*side+offset,5*side+offset)<=offset) {clic54 =! clic54;}
    if (dist(mouseX,mouseY,5*side+offset,5*side+offset)<=offset) {clic55 =! clic55;}
    if (dist(mouseX,mouseY,1*side+offset,6*side+offset)<=offset) {clic61 =! clic61;}
    if (dist(mouseX,mouseY,2*side+offset,6*side+offset)<=offset) {clic62 =! clic62;}
    if (dist(mouseX,mouseY,3*side+offset,6*side+offset)<=offset) {clic63 =! clic63;}
    if (dist(mouseX,mouseY,4*side+offset,6*side+offset)<=offset) {clic64 =! clic64;}
    if (dist(mouseX,mouseY,5*side+offset,6*side+offset)<=offset) {clic65 =! clic65;}
    if (dist(mouseX,mouseY,1*side+offset,7*side+offset)<=offset) {clic71 =! clic71;}
    if (dist(mouseX,mouseY,2*side+offset,7*side+offset)<=offset) {clic72 =! clic72;}
    if (dist(mouseX,mouseY,3*side+offset,7*side+offset)<=offset) {clic73 =! clic73;}
    if (dist(mouseX,mouseY,4*side+offset,7*side+offset)<=offset) {clic74 =! clic74;}
    if (dist(mouseX,mouseY,5*side+offset,7*side+offset)<=offset) {clic75 =! clic75;}
    if (dist(mouseX,mouseY,1*side+offset,8*side+offset)<=offset) {clic81 =! clic81;}
    if (dist(mouseX,mouseY,2*side+offset,8*side+offset)<=offset) {clic82 =! clic82;}
    if (dist(mouseX,mouseY,3*side+offset,8*side+offset)<=offset) {clic83 =! clic83;}
    if (dist(mouseX,mouseY,4*side+offset,8*side+offset)<=offset) {clic84 =! clic84;}
    if (dist(mouseX,mouseY,5*side+offset,8*side+offset)<=offset) {clic85 =! clic85;}


Yes I can see that ahah :sweat_smile: (make sure to format your code by using the </> button when editing your message)

You got it, this is exactly what programming was made for! If you see repetition in your code, you’ll most likely be able to remove it by using loops / arrays / functions, etc…

You mentioned a 5*8 frame with switches, they are written explicitly in your code as boolean variables. As you can see this is very tedious but we have 2D arrays!

Let’s take a look at that code example :

boolean[][] switches = new boolean[5][8];
  • On the left you have the type (since Java is a typed language) : boolean[][]

    This means an array of arrays (yes you heard it right), let’s see an example :

    boolean[][] switches = {
      {true, false, true},
      {false, false, true},

    This is a way to explicitly declare arrays with brackets and here we initialize a 2*3 two dimensional array. Two dimensional because its an array that contains other arrays (therefore the type [][])

  • And on the right side, you use the new keyword in order to construct a new array and you give it the dimensions (a 5 * 8).

    By default when you don’t specify any values, an array of booleans will be initialized with false everywhere (so you don’t need to manually set them to false)

You can see the tutorial on the Processing website : Two-Dimensional Arrays \

Now how do you access a value at location (1, 5) for example?

You use square brackets :

// Get it
boolean isOn = switches[1][5];

// Set it
switches[1][5] = true;

Now how would you go about printing that array in the console? I’ll leave you that as an exercise.

Using a two dimensional array is going to divide the number of lines by a lot! :yum:

You just need to extract the logic from your code and see how you can work with a 2D array of booleans instead of dozens of separate variables.

Hope it helps! :wink:

1 Like

Id recommend looking at classes so you can reduce the repetition in code.

1 Like


A good start would be formatting your code:


here’s a class example, of course you would have to rename the class and fill it with the variables and methods you need, but this is to give you an idea of what the code would look like if you chose to use classes.

myClass m1;
int total;
ArrayList<myClass> classes = new ArrayList<myClass>();
void setup(){
  m1 = new myClass(100,100);
  for(int i=0;i<total;i++){
    myClass m = new myClass(random(100),random(100));

void draw(){
  for(int i=0;i<total;i++){
    myClass m = classes.get(i);

class myClass{
  boolean click,mdown;
  int offset = 25, side = 50;
  float someVar1,someVar2;
  myClass(float a,float b){
    someVar1 = a;
    someVar2 = b;
  void draw(){
  void logic(){
  boolean click(){
    if (mousePressed&&!mdown&&dist(mouseX,mouseY,1*side+offset,1*side+offset)<=offset){
      mdown = true;
      click = true;
      mdown = false;
      click = false;
    return click;
1 Like

hello, thanks a lot for all these topics & for your precious time

I’ll try to understand all that new world …


There is a nice textual tutorial about objects on the website, section tutorials