sketch too big

Can u help me to reduce this sketch ?? please :’(

int number = 0;
int number2 = 0;
int var = 0;
double val = 0;
double temp = 0;

void setup() {
  //Inizializzo i pin come uscite
  for(int i=2; i<6; i++) { 
    pinMode(i, OUTPUT);
    pinMode(i+7, OUTPUT);
  } 
} 

// 9 10 11 12 || 9 10 11 12
void loop() { 
  if(number==0) { 
    digitalWrite(9, LOW); 
    digitalWrite(12, LOW); 
    digitalWrite(11, LOW); 
    digitalWrite(10, LOW); 
  } 
  if(number==1) { 
    digitalWrite(9, HIGH); 
    digitalWrite(12, LOW); 
    digitalWrite(11, LOW); 
    digitalWrite(10, LOW);
  } 
  if(number==2) { 
    digitalWrite(9, LOW); 
    digitalWrite(12, HIGH); 
    digitalWrite(11, LOW); 
    digitalWrite(10, LOW); 
  } 
  if(number==3) { 
    digitalWrite(9, HIGH); 
    digitalWrite(12, HIGH); 
    digitalWrite(11, LOW); 
    digitalWrite(10, LOW); 
  } 
  if(number==4) { 
    digitalWrite(9, LOW); 
    digitalWrite(12, LOW); 
    digitalWrite(11, HIGH); 
    digitalWrite(10, LOW);
  } 
  if(number==5) { 
    digitalWrite(9, HIGH); 
    digitalWrite(12, LOW); 
    digitalWrite(11, HIGH); 
    digitalWrite(10, LOW); 
  }/* 
  if(number==6) { 
    digitalWrite(9, LOW); 
    digitalWrite(12, HIGH); 
    digitalWrite(11, HIGH); 
    digitalWrite(10, LOW);
  } 
  if(number==7) { 
    digitalWrite(9, HIGH); 
    digitalWrite(12, HIGH); 
    digitalWrite(11, HIGH); 
    digitalWrite(10, LOW);
  } 
  if(number==8) { 
    digitalWrite(9, LOW); 
    digitalWrite(12, LOW); 
    digitalWrite(11, LOW); 
    digitalWrite(10, HIGH);
  } 
  if(number==9) { 
    digitalWrite(9, HIGH); 
    digitalWrite(12, LOW); 
    digitalWrite(11, LOW); 
    digitalWrite(10, HIGH);
  }*/
  
  
  if(number2==0) { 
    digitalWrite(2, LOW); 
    digitalWrite(5, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(3, LOW); 
  } 
  if(number2==1) { 
    digitalWrite(2, HIGH); 
    digitalWrite(5, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(3, LOW);
  } 
  if(number2==2) { 
    digitalWrite(2, LOW); 
    digitalWrite(5, HIGH); 
    digitalWrite(4, LOW); 
    digitalWrite(3, LOW); 
  } 
  if(number2==3) { 
    digitalWrite(2, HIGH); 
    digitalWrite(5, HIGH); 
    digitalWrite(4, LOW); 
    digitalWrite(3, LOW); 
  } 
  if(number2==4) { 
    digitalWrite(2, LOW); 
    digitalWrite(5, LOW); 
    digitalWrite(4, HIGH); 
    digitalWrite(3, LOW);
  } 
  if(number2==5) { 
    digitalWrite(2, HIGH); 
    digitalWrite(5, LOW); 
    digitalWrite(4, HIGH); 
    digitalWrite(3, LOW); 
  } 
  if(number2==6) { 
    digitalWrite(2, LOW); 
    digitalWrite(5, HIGH); 
    digitalWrite(4, HIGH); 
    digitalWrite(3, LOW);
  } 
  if(number2==7) { 
    digitalWrite(2, HIGH); 
    digitalWrite(5, HIGH); 
    digitalWrite(4, HIGH); 
    digitalWrite(3, LOW);
  } 
  if(number2==8) { 
    digitalWrite(2, LOW); 
    digitalWrite(5, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(3, HIGH);
  } 
  if(number2==9) { 
    digitalWrite(2, HIGH); 
    digitalWrite(5, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(3, HIGH);
  } 
  delay(1000); // waits for 1000 milli seconds 
  val = analogRead(5);
  temp = (val * (5.0/1024.0)) / 0.01;
  var = temp * 1000;
  if((var % 1000) > 500) {
    temp++;
  }
  temp += 2;
  number = temp / 10;
  number2 = temp - (number * 10);
}

It is 7754 bytes of 7168 bytes maximum.

avoid floating point numbers... they bring in several kb of code into the sketch

massimo

Hi originof,

I would suggest you get rid of the datatype double in your calculations. If you can move
the math into the range of an int you would be surprised how much memory is wasted
in your code.

Looks like you are matching the analog read to a temperature. If you could do this math with integers
you’ll save a lot.

Eberhard

Here is a suggested change that gets rid of the floating point math and replaces the if then statements with a switch case structure. This compiles to 4320 bytes

int number = 0;
int number2 = 0;
int var = 0;
int val = 0;
long temp = 0;

void setup() {
  //Inizializzo i pin come uscite
  for(int i=2; i<6; i++) { 
    pinMode(i, OUTPUT);
    pinMode(i+7, OUTPUT);
  } 
} 

// 9 10 11 12 || 9 10 11 12
void loop() 
{ 
  switch (number) {
   case 0: 
    digitalWrite(9, LOW); 
    digitalWrite(12, LOW); 
    digitalWrite(11, LOW); 
    digitalWrite(10, LOW); 
    break;
   case 1: 
    digitalWrite(9, HIGH); 
    digitalWrite(12, LOW); 
    digitalWrite(11, LOW); 
    digitalWrite(10, LOW);
    break;
   case 2: 
    digitalWrite(9, LOW); 
    digitalWrite(12, HIGH); 
    digitalWrite(11, LOW); 
    digitalWrite(10, LOW); 
    break;
   case 3: 
    digitalWrite(9, HIGH); 
    digitalWrite(12, HIGH); 
    digitalWrite(11, LOW); 
    digitalWrite(10, LOW); 
    break;
   case 4: 
    digitalWrite(9, LOW); 
    digitalWrite(12, LOW); 
    digitalWrite(11, HIGH); 
    digitalWrite(10, LOW);
    break;
   case 5: 
    digitalWrite(9, HIGH); 
    digitalWrite(12, LOW); 
    digitalWrite(11, HIGH); 
    digitalWrite(10, LOW); 
    break;
   case 6: 
    digitalWrite(9, LOW); 
    digitalWrite(12, HIGH); 
    digitalWrite(11, HIGH); 
    digitalWrite(10, LOW);
    break;
   case 7: 
    digitalWrite(9, HIGH); 
    digitalWrite(12, HIGH); 
    digitalWrite(11, HIGH); 
    digitalWrite(10, LOW);
    break;
   case 8: 
    digitalWrite(9, LOW); 
    digitalWrite(12, LOW); 
    digitalWrite(11, LOW); 
    digitalWrite(10, HIGH);
    break;
   case 9: 
    digitalWrite(9, HIGH); 
    digitalWrite(12, LOW); 
    digitalWrite(11, LOW); 
    digitalWrite(10, HIGH);
  }
  
  switch (number2) {
   case 0: 
    digitalWrite(2, LOW); 
    digitalWrite(5, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(3, LOW); 
    break;
   case 1: 
    digitalWrite(2, HIGH); 
    digitalWrite(5, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(3, LOW);
    break;
   case 2: 
    digitalWrite(2, LOW); 
    digitalWrite(5, HIGH); 
    digitalWrite(4, LOW); 
    digitalWrite(3, LOW); 
    break;
   case 3: 
    digitalWrite(2, HIGH); 
    digitalWrite(5, HIGH); 
    digitalWrite(4, LOW); 
    digitalWrite(3, LOW); 
    break;
   case 4: 
    digitalWrite(2, LOW); 
    digitalWrite(5, LOW); 
    digitalWrite(4, HIGH); 
    digitalWrite(3, LOW);
    break;
   case 5: 
    digitalWrite(2, HIGH); 
    digitalWrite(5, LOW); 
    digitalWrite(4, HIGH); 
    digitalWrite(3, LOW); 
    break;
   case 6: 
    digitalWrite(2, LOW); 
    digitalWrite(5, HIGH); 
    digitalWrite(4, HIGH); 
    digitalWrite(3, LOW);
    break;
   case 7: 
    digitalWrite(2, HIGH); 
    digitalWrite(5, HIGH); 
    digitalWrite(4, HIGH); 
    digitalWrite(3, LOW);
    break;
   case 8: 
    digitalWrite(2, LOW); 
    digitalWrite(5, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(3, HIGH);
    break;
   case 9: 
    digitalWrite(2, HIGH); 
    digitalWrite(5, LOW); 
    digitalWrite(4, LOW); 
    digitalWrite(3, HIGH);
  } 
  delay(1000); // waits for 1000 milli seconds 
  val = analogRead(5);
  //temp = (val * (5.0/1024.0)) / 0.01;
  temp = (val * 5 * 100) / 1024;
  var = temp * 1000;
  if((var % 1000) > 500) {
    temp++;
  }
  temp += 2;
  number = temp / 10;
  number2 = temp - (number * 10);
}