Learn how to build a single CUBIC METER with ARDUINO.
Example.jpg

As seen in picture above, this system can be set to read up to 3 different axis, once each Sensor read correspond distance, final meters Area read is shown in 1.44" TFT.

This project is great to practice the interaction of different hardware in Arduino and to devise better technological solutions for home and industry.

Project

The product consists of measuring from a simple linear distance, square meters and up to cubic meters. Since we are using HC-SR04 ultrasound sensors, distances should not exceed between 3.5 meters and 4 meters long,and especially there should be no obstacles in the line that is registering the measure

According to the measurement that we select in the TFT screen menu, the LEDs will begin to flash indicating where each sensor should point, so that each distance registers the angle for square meters, cubic meters, or simply linear distance. After that, just make click on center of ROTARY ENCODER to Start collecting ("MEDIR option") each distance. An single 3-axis graphic will show each measure and confrimed by each LED that stays ON. And according selected Option in menu Mts2 or Mts3, results will be shown on top right corner.

Code explanation

Libraries <SPI.h> , <Adafruit_GFX-h>, <TFT_ILI9163C.h> and <RotaryEncoder.h> are the main base to let our hardware to be recognized and configured.

    #include <SPI.h>
#include <Adafruit_GFX.h>
#include <TFT_ILI9163C.h>
#include <RotaryEncoder.h>

this segment contains the predefined variables of the colors that we will use in the TFT screen image display, and also the variables to store fluctuating data.

    // Color definitions
#define	BLACK   0x0000
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0  
#define WHITE   0xFFFF
#define ORANGE  0xF5A9
int menu_select= 0;
int menu_square= 0;
int menu_cubic = 0;
int menu_lineal= 0;
int menu_apaga = 0;
int mts2M      = 0;
int mts2P      = 0;
int mts3       = 0;
float lineal     = 0;
int SELECT     = 0;
int seleccion  = 0;
int aboutA = 0;
const int Echo_Alto    = 2;
const int Trigger_Alto = 3;
const int Echo_Ancho   = 4;
const int Trigger_Ancho= 5;
const int Echo_Largo   = 6;
const int Trigger_Largo= 7;
const int Led_An    = A5;
const int Led_L     = A4;
const int Led_Al    = A0;
const int Backlight = A1;

int ledState = LOW;   // ledState se usará para establecer el estado del LED
long previousMillis = 0;   // almacenará la última vez que el estado del LED
long interval = 500;

this aditional lines, defines where TFT pins are connected to Arduino. Besides, some extra variables are to store each Area measured from Ultrasonic sensors reading. Finally, code show where Rotary Encoder Pins position are connected to Arduino (A2,A3) and where it´s push button goes as well (Pin8).

    #define __CS 10
#define __DC 9
#define __RST 12
long duration_L;
long duration_An;
long duration_Al;
float distance_L;
float distance_An;
float distance_Al;
float mtscuadrado =0;
float mtscubico =0;

RotaryEncoder encoder(A2, A3);

const int Push = 8;  // Botón push de Rotary encoder
TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC,__RST);

we initialize the TFT screen, then Serial Com to 9600 BPS. after this, pinMode is set to each digital and analog Arduino Pin for HC-SR04 Sensors and Rotary Encoder. at the end you will see the digitalWrite(Backlight, LOW) command because normal state of this CUBE is in Stand By mode. At the bottom code is sent to VOID power().

    void setup() {
  tft.begin();
  Serial.begin(9600);
  pinMode(Push, INPUT_PULLUP);
  pinMode(Trigger_Alto, OUTPUT);
  pinMode(Echo_Alto, INPUT);
  pinMode(Trigger_Ancho, OUTPUT);
  pinMode(Echo_Ancho, INPUT);
  pinMode(Trigger_Largo, OUTPUT);
  pinMode(Echo_Largo, INPUT);
  pinMode(Led_An, OUTPUT);
  pinMode(Led_Al, OUTPUT);
  pinMode(Led_L,  OUTPUT);
  pinMode(Backlight,OUTPUT);
  digitalWrite(Backlight, LOW);
  power();
  }
    void loop(){
  
  //tft.fillScreen();
  tft.setCursor(35, 20);
  tft.setTextColor(ORANGE); 
  tft.setTextSize(2);
  tft.println("CUBIC");
  tft.setTextSize(1);
  tft.setCursor(93, 13);
  tft.println("3");
  startup();
  delay(4000);
  menu();
  }

void menu(){
  tft.fillScreen();
  if(seleccion == 1){
    tft.setTextColor(GREEN,BLACK);
    tft.setCursor(5,7);
    tft.println("Mts  PISO");
    tft.setTextColor(YELLOW,BLACK);
    tft.setCursor(26,4);
    tft.println("2");
    }

  if(seleccion == 2){
    tft.setTextColor(GREEN,BLACK);
    tft.setCursor(5,7);
    tft.println("Mts  MURO");
    tft.setTextColor(YELLOW,BLACK);
    tft.setCursor(26,4);
    tft.println("2");
    }
   
  if(seleccion == 3){
    tft.setTextColor(GREEN,BLACK);
    tft.setCursor(5,7);
    tft.println("Mts");
    tft.setTextColor(YELLOW,BLACK);
    tft.setCursor(26,4);
    tft.println("3");
    }

  if(seleccion == 4){
    tft.setTextColor(GREEN,BLACK);
    tft.setCursor(5,7);
    tft.println("Lectura lineal");
  } 

  tft.drawLine(0, 15, 125, 15, ORANGE);
  tft.setCursor(40,20);
  tft.setTextColor(ORANGE); 
  tft.setTextSize(2);
  tft.println("MENU");
  tft.setTextSize(1);
  tft.setTextColor(WHITE);
  tft.setCursor(5,45);
  tft.println("<> Mts  PISO");
  tft.setCursor(44,40);
  tft.println("2");
  tft.setCursor(5,60);
  tft.println("<> Mts  MURO");
  tft.setCursor(44,55);
  tft.println("2");
  tft.setCursor(5,75);
  tft.println("<> Mts");
  tft.setCursor(44,70);
  tft.println("3");
  tft.setCursor(5,90);
  tft.println("<> Lectura lineal");
  tft.setTextColor(ORANGE);
  tft.setCursor(15,110);
  tft.println("Apagar");
  tft.setCursor(90,110);
  tft.println("Info");
  menu_select = 1;
  select();
}

void select(){
while(menu_select == 1){
  static int pos = 0;
  encoder.tick();
  int newPos = encoder.getPosition();

  if (pos > newPos){           // Sentencia para generar desplazamiento en pantalla
    SELECT = SELECT-1 ;
    if (SELECT < 1){
    SELECT = 1;
    }
    MENU1();
    Serial.print(SELECT);
    Serial.print(" Menu arriba    ");
    }
    
  if (pos < newPos){          // Sentencia para generar desplazamiento en pantalla
    SELECT = SELECT+1 ;
    if (SELECT > 6){
    SELECT = 6;
    }
    MENU1();
    Serial.print(SELECT);
    Serial.print(" Menu abajo    ");
    }
  if (pos != newPos) {
    pos = newPos;      
}
  if(SELECT == 1){
    digitalWrite(Led_Al, LOW);
    LED_PISO();
  }

  if(digitalRead(Push) == LOW && SELECT == 1){   // seleciona Mts2 Piso
    seleccion   = SELECT;
    menu_select = 0;
    menu_square  = 1;
    SELECT = 0;
    M2();
}

  if(SELECT == 2){
    digitalWrite(Led_L, LOW);
    LED_MURO();
  }
  
if(digitalRead(Push) == LOW && SELECT == 2){     // selecciona Mts2 Muro
    seleccion   = SELECT;
    menu_select = 0;
    menu_square = 1;
    SELECT = 0;
    M2();
}

if(SELECT == 3){
    LED_CUBICO();
  }

if(digitalRead(Push) == LOW && SELECT == 3){    // selecciona Mts3
      seleccion   = SELECT;
      menu_select = 0;
      menu_cubic  = 1;
      SELECT = 0;
      M3();
  }

if(SELECT == 4){
  digitalWrite(Led_L, LOW);
  digitalWrite(Led_An,LOW);
  LINEAL();
}

if(digitalRead(Push) == LOW && SELECT == 4){    // selecciona Mts lineal
      seleccion   = SELECT;
      menu_select = 0;
      menu_cubic  = 0;
      menu_lineal = 1;
      SELECT = 0;
    Lineal();
  }

if(SELECT ==5 || SELECT ==6){
  digitalWrite(Led_L, LOW);
  digitalWrite(Led_Al,LOW);
  digitalWrite(Led_An,LOW);
}

if(digitalRead(Push) == LOW && SELECT == 5){
  seleccion   = SELECT;
      menu_select = 0;
      menu_cubic  = 0;
      menu_lineal = 0;
      menu_apaga  = 1;
      SELECT = 0;
      OFF();
}

if(digitalRead(Push) == LOW && SELECT == 6){
   aboutA = 1;
   about();
  }  
}
}

void MENU1(){
  switch (SELECT){
    case 1 :
  tft.setTextColor(GREEN);
  tft.setCursor(5,45);
  tft.println("<> Mts  PISO");
  tft.setCursor(44,40);
  tft.println("2");
  tft.setTextColor(WHITE);
  tft.setCursor(5,60);
  tft.println("<> Mts  MURO");
  tft.setCursor(44,55);
  tft.println("2");
  break;

    case 2 :
  tft.setTextColor(WHITE);
  tft.setCursor(5,45);
  tft.println("<> Mts  PISO");
  tft.setCursor(44,40);
  tft.println("2");
  tft.setTextColor(GREEN);
  tft.setCursor(5,60);
  tft.println("<> Mts  MURO");
  tft.setCursor(44,55);
  tft.println("2");
  tft.setTextColor(WHITE);
  tft.setCursor(5,75);
  tft.println("<> Mts");
  tft.setCursor(44,70);
  tft.println("3");
  break;
  
    case 3 :
  tft.setTextColor(WHITE);
  tft.setCursor(5,60);
  tft.println("<> Mts  MURO");
  tft.setCursor(44,55);
  tft.println("2");
  tft.setTextColor(GREEN);
  tft.setCursor(5,75);
  tft.println("<> Mts");
  tft.setCursor(44,70);
  tft.println("3");
  tft.setTextColor(WHITE);
  tft.setCursor(5,90);
  tft.println("<> Lectura lineal");
  break;

    case 4 :
  tft.setTextColor(WHITE);
  tft.setCursor(5,75);
  tft.println("<> Mts");
  tft.setCursor(44,70);
  tft.println("3");
  tft.setTextColor(GREEN);
  tft.setCursor(5,90);
  tft.println("<> Lectura lineal");
  tft.setTextColor(ORANGE);  
  tft.setCursor(15,110);
  tft.println("Apagar");
  break;

    case 5 :
  tft.setTextColor(WHITE);
  tft.setCursor(5,90);
  tft.println("<> Lectura lineal");
  tft.setTextColor(RED);  
  tft.setCursor(15,110);
  tft.println("Apagar");
  tft.setTextColor(ORANGE);  
  tft.setCursor(90,110);
  tft.println("Info");
  break;

    case 6 :
  tft.setTextColor(ORANGE);  
  tft.setCursor(15,110);
  tft.println("Apagar");
  tft.setTextColor(RED);  
  tft.setCursor(90,110);
  tft.println("Info");
  break;
  }
  }

void M2(){
  tft.fillScreen();
  
  if(seleccion == 1){  // Crea título en zona superior izquierda de pantalla con MTS 2 PISO
    tft.setTextColor(YELLOW,BLACK);
    tft.setCursor(5,7);
    tft.println("Mts  PISO");
    tft.setCursor(26,4);
    tft.println("2");
    tft.setTextColor(WHITE);
    tft.setCursor(15,110);
    tft.println("medir");
    Draw_piso();
   }

  if(seleccion == 2){   // Crea título en zona superior izquierda de pantalla con MTS 2 MURO
    tft.setTextColor(YELLOW,BLACK);
    tft.setCursor(5,7);
    tft.println("Mts  MURO");
    tft.setCursor(26,4);
    tft.println("2");
    tft.setTextColor(WHITE);
    tft.setCursor(15,110);
    tft.println("medir");
    Draw_muro();
   }
 
  tft.setTextSize(1);
  tft.setTextColor(ORANGE);
  tft.setCursor(90,110);
  tft.println("salir");
 
  while (menu_square == 1){
    static int pos = 0;
  encoder.tick();
  int newPos = encoder.getPosition();

  if(seleccion == 1 && SELECT == 1){
    LED_PISO();
  }

  if(seleccion == 2 && SELECT == 1){
    LED_MURO();
  }

  if(SELECT == 2){
  digitalWrite(Led_L, LOW);
  digitalWrite(Led_An,LOW);
  digitalWrite(Led_Al,LOW);  
  }
  
  if (pos > newPos){           // Sentencia para generar desplazamiento en pantalla dentro Medir M2
    SELECT = SELECT-1 ;
    
    if (SELECT <= 1){
    SELECT = 1;
    }
    M_area_select();
    }
    
  if (pos < newPos){          // Sentencia para generar desplazamiento en pantalla
    SELECT = SELECT+1 ;
   
    if (SELECT >= 2){
    SELECT = 2;
    }
    M_area_select();
    }
  if (pos != newPos) {
    pos = newPos;      
}
  if(digitalRead(Push) == LOW && SELECT == 1 && seleccion ==1){ // Si Mt2 PISO seleccionado + opcion medir + prsiona boton ---> Medir Mt2 PISO
    digitalWrite(Led_L, LOW);
  digitalWrite(Led_An,LOW);
  digitalWrite(Led_Al,LOW); 
  tft.fillRect(5,80,33,15,BLACK);  // Borra en pantalla datos X
    tft.fillRect(90,80,30,15,BLACK); // Borra en pantalla datos Y
    SELECT = 0;

delay(700);
digitalWrite(Trigger_Ancho, LOW);
delayMicroseconds(2);
digitalWrite(Trigger_Ancho, HIGH);      // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(Trigger_Ancho, LOW);       // Reads the echoPin, returns the sound wave travel time in microseconds
duration_An = pulseIn(Echo_Ancho, HIGH);
distance_An= (duration_An*0.034/2)+10;       // Calculating the distance
tft.setCursor(20,80);
tft.println(distance_An,0);             // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
digitalWrite(Led_An, HIGH);             // Deja encendido el LED An cuando ya se ha tomado la lectura de dicho sensor

delay(700);
digitalWrite(Trigger_Largo, LOW);
delayMicroseconds(2);
digitalWrite(Trigger_Largo, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(Trigger_Largo, LOW);       // Reads the echoPin, returns the sound wave travel time in microseconds
duration_L = pulseIn(Echo_Largo, HIGH);
distance_L= (duration_L*0.034/2)+10;         // Calculating the distance
tft.setCursor(90,80);
tft.println(distance_L,0);              // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
digitalWrite(Led_L , HIGH);             // Deja encendido el LED L cuando ya se ha tomado la lectura de dicho sensor


SELECT = 0;                             // Una vez tomada la medida cuadrada, se da la instrucción para que los led queden encendidos
tft.setTextColor(WHITE);
  tft.setCursor(15,110);
  tft.println("medir");
mtscuadrado = (((distance_L/100)*(distance_An/100)));
tft.setTextColor(WHITE,BLACK);
    tft.setCursor(70,7);
    tft.println(mtscuadrado,2);         // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
    Serial.println(mtscuadrado,2);      //  la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
    duration_L  = 0;
    duration_An = 0;
    distance_L  = 0;
    distance_An = 0;
}

if(digitalRead(Push) == LOW && SELECT == 1 && seleccion ==2){ // Si Mt2 MURO seleccionado + opcion medir + prsiona boton ---> Medir Mt2 MURO
  digitalWrite(Led_L, LOW);
  digitalWrite(Led_An,LOW);
  digitalWrite(Led_Al,LOW); 
  tft.fillRect(5,80,33,15,BLACK);  // Borra en pantalla datos X
  tft.fillRect(50,18,30,15,BLACK); // Borra en pantalla datos Z
    SELECT = 0;

delay(700);
digitalWrite(Trigger_Ancho, LOW);
delayMicroseconds(2);
digitalWrite(Trigger_Ancho, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(Trigger_Ancho, LOW);// Reads the echoPin, returns the sound wave travel time in microseconds
duration_An = pulseIn(Echo_Ancho, HIGH);
distance_An= (duration_An*0.034/2)+10;// Calculating the distance
tft.setCursor(20,80);
tft.println(distance_An,0);  // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
digitalWrite(Led_An , HIGH);             // Deja encendido el LED L cuando ya se ha tomado la lectura de dicho sensor

delay(700);
digitalWrite(Trigger_Alto, LOW);
delayMicroseconds(2);
digitalWrite(Trigger_Alto, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(Trigger_Alto, LOW);// Reads the echoPin, returns the sound wave travel time in microseconds
duration_Al = pulseIn(Echo_Alto, HIGH);
distance_Al= (duration_Al*0.034/2)+10;// Calculating the distance
tft.setCursor(52,22);
tft.println(distance_Al,0);  // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
digitalWrite(Led_Al , HIGH);             // Deja encendido el LED L cuando ya se ha tomado la lectura de dicho sensor

SELECT = 0;                             // Una vez tomada la medida cuadrada, se da la instrucción para que los led queden encendidos
tft.setTextColor(WHITE);
  tft.setCursor(15,110);
  tft.println("medir");
mtscuadrado = (((distance_Al/100)*(distance_An/100)));
tft.setTextColor(WHITE,BLACK);
    tft.setCursor(70,7);
    tft.println(mtscuadrado,2);  // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
    Serial.println(mtscuadrado,2);  //  la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
    duration_Al = 0;
    duration_An = 0;
    distance_Al = 0;
    distance_An = 0;
}

if(digitalRead(Push) == LOW && SELECT == 2){
  menu_square = 0;
  menu_select = 1;
  SELECT =0;
  menu();
}
}
}

void M_area_select(){
  switch (SELECT){
    case 1 :
  tft.setTextColor(GREEN);
  tft.setCursor(15,110);
  tft.println("medir");
  tft.setTextColor(ORANGE);
  tft.setCursor(90,110);
  tft.println("salir");
  break;

    case 2 :
  tft.setTextColor(WHITE);
  tft.setCursor(15,110);
  tft.println("medir");
  tft.setTextColor(RED);
  tft.setCursor(90,110);
  tft.println("salir");
  break;

 }
}



void apaga(){
  switch (SELECT){
    case 1 :
  tft.setTextColor(GREEN);
  tft.setCursor(40,55);
  tft.println("<> Si");
  tft.setTextColor(WHITE);
  tft.setCursor(40,80);
  tft.println("<> No");
  break;

    case 2 :
  tft.setTextColor(WHITE);
  tft.setCursor(40,55);
  tft.println("<> Si");
  tft.setTextColor(GREEN);
  tft.setCursor(40,80);
  tft.println("<> No");
  break;
  
}
}

void cuadrado(){
tft.drawLine(45, 70, 70, 45, WHITE);
tft.drawLine(85, 70, 110, 45, WHITE);
tft.drawLine(45, 70, 85, 70, WHITE);
tft.drawLine(70, 45, 110, 45, WHITE);
 }

void cubo(){
  tft.setTextColor(WHITE);
 tft.setCursor(58,35);
  tft.println("Z");
  tft.setCursor(20,100);
  tft.println("X");
  tft.setCursor(96,100);
  tft.println("Y");
  tft.drawLine(60,45,60,80,GREEN);
  tft.drawLine(55,50,60,45,GREEN);
  tft.drawLine(65,50,60,45,GREEN);
  tft.drawLine(60,80,90,100,RED);
  tft.drawLine(86,90,90,100,RED);
  tft.drawLine(81,100,90,100,RED);
  tft.drawLine(60,80,30,100,WHITE);
  tft.drawLine(35,90,30,100,WHITE);
  tft.drawLine(39,100,30,100,WHITE);
}


void M3(){
  tft.fillScreen();
  cubo();
  if(seleccion == 3){  // Crea título en zona superior izquierda de pantalla con MTS 2 PISO
    tft.setTextColor(YELLOW,BLACK);
    tft.setCursor(5,7);
    tft.println("Mts       ");
    tft.setCursor(26,4);
    tft.println("3");
    tft.setTextColor(WHITE);
    tft.setCursor(15,110);
    tft.println("medir");
   }
  while (menu_cubic == 1){
    static int pos = 0;
  encoder.tick();
  int newPos = encoder.getPosition();

  if(seleccion == 3 && SELECT == 1){
    LED_CUBICO();
  }
  
  if(SELECT == 2){
  digitalWrite(Led_L, LOW);
  digitalWrite(Led_An,LOW);
  digitalWrite(Led_Al,LOW);  
  }
  
  if (pos > newPos){           // Sentencia para generar desplazamiento en pantalla dentro Medir M2
    SELECT = SELECT-1 ;
    
    if (SELECT <= 1){
    SELECT = 1;
    }
    M_area_select();
    }
    
  if (pos < newPos){          // Sentencia para generar desplazamiento en pantalla
    SELECT = SELECT+1 ;
   
    if (SELECT >= 2){
    SELECT = 2;
    }
    M_area_select();
    }
  if (pos != newPos) {
    pos = newPos;      
}
  if(digitalRead(Push) == LOW && SELECT == 1 && seleccion ==3){ // Si Mt2 PISO seleccionado + opcion medir + prsiona boton ---> Medir Mt2 PISO
    digitalWrite(Led_L, LOW);
    digitalWrite(Led_An,LOW);
    digitalWrite(Led_Al,LOW);
    tft.fillRect(50,18,30,15,BLACK); // Borra en pantalla datos Z
    tft.fillRect(5,80,33,15,BLACK);  // Borra en pantalla datos X
    tft.fillRect(90,80,30,15,BLACK); // Borra en pantalla datos Y
    SELECT = 0;



delay(700);
digitalWrite(Trigger_Ancho, LOW);
delayMicroseconds(2);
digitalWrite(Trigger_Ancho, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(Trigger_Ancho, LOW);       // Reads the echoPin, returns the sound wave travel time in microseconds
duration_An = pulseIn(Echo_Ancho, HIGH);
distance_An= (duration_An*0.034/2)+10;         // Calculating the distance
tft.setCursor(15,80);
tft.println(distance_An,0);              // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
digitalWrite(Led_An , HIGH);             // Deja encendido el LED L cuando ya se ha tomado la lectura de dicho sensor

delay(700);
digitalWrite(Trigger_Largo, LOW);
delayMicroseconds(2);
digitalWrite(Trigger_Largo, HIGH);      // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(Trigger_Largo, LOW);       // Reads the echoPin, returns the sound wave travel time in microseconds
duration_L = pulseIn(Echo_Largo, HIGH);
distance_L= (duration_L*0.034/2)+10;       // Calculating the distance
tft.setCursor(90,80);
tft.println(distance_L,0);             // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
digitalWrite(Led_L, HIGH);             // Deja encendido el LED An cuando ya se ha tomado la lectura de dicho sensor

delay(700);
digitalWrite(Trigger_Alto, LOW);
delayMicroseconds(2);
digitalWrite(Trigger_Alto, HIGH);      // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(Trigger_Alto, LOW);       // Reads the echoPin, returns the sound wave travel time in microseconds
duration_Al = pulseIn(Echo_Alto, HIGH);
distance_Al= (duration_Al*0.034/2)+10;       // Calculating the distance
tft.setCursor(53,25);
tft.println(distance_Al,0);             // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
digitalWrite(Led_Al, HIGH);             // Deja encendido el LED An cuando ya se ha tomado la lectura de dicho sensor
SELECT = 0;                             // Una vez tomada la medida cuadrada, se da la instrucción para que los led queden encendidos
tft.setTextColor(WHITE);
  tft.setCursor(15,110);
  tft.println("medir");
mtscubico = ((distance_L/100)*(distance_An/100)*(distance_Al/100));
tft.setTextColor(WHITE,BLACK);
    tft.setCursor(70,7);
    tft.println(mtscubico,2);         // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
    Serial.println(mtscubico,2);      //  la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
    duration_L  = 0;
    duration_An = 0;
    duration_Al = 0;
    distance_L  = 0;
    distance_An = 0;
    distance_Al = 0;
}
 if(digitalRead(Push) == LOW && SELECT == 2){
  menu_cubic = 0;
  menu_select = 1;
  SELECT =0;
  menu();
}
}
}

void Lineal(){
 tft.fillScreen();
 Draw_lineal(); 
  if(seleccion == 4){  // Crea título en zona superior izquierda de pantalla con MTS 2 PISO
    tft.setTextColor(YELLOW,BLACK);
    tft.setCursor(5,7);
    tft.println("Mts Lineal");
    tft.setTextColor(WHITE);
    tft.setCursor(15,110);
    tft.println("medir");
   }
  while (menu_lineal == 1){
    static int pos = 0;
  encoder.tick();
  int newPos = encoder.getPosition();

  if(seleccion == 4 && SELECT == 1){
    LINEAL();
  }
  
  if(SELECT == 2){
  digitalWrite(Led_L, LOW);
  digitalWrite(Led_An,LOW);
  digitalWrite(Led_Al,LOW);  
  }
  
  if (pos > newPos){           // Sentencia para generar desplazamiento en pantalla dentro Medir M2
    SELECT = SELECT-1 ;
    
    if (SELECT <= 1){
    SELECT = 1;
    }
    M_area_select();
    }
    
  if (pos < newPos){          // Sentencia para generar desplazamiento en pantalla
    SELECT = SELECT+1 ;
   
    if (SELECT >= 2){
    SELECT = 2;
    }
    M_area_select();
    }
  if (pos != newPos) {
    pos = newPos;      
}
  if(digitalRead(Push) == LOW && SELECT == 1 && seleccion ==4){ // Si Mt2 PISO seleccionado + opcion medir + prsiona boton ---> Medir Mt2 PISO
    digitalWrite(Led_L, LOW);
    digitalWrite(Led_An,LOW);
    digitalWrite(Led_Al,LOW); 
    tft.fillRect(50,18,25,15,BLACK);
    SELECT = 0;
delay(700);
digitalWrite(Trigger_Alto, LOW);
delayMicroseconds(2);
digitalWrite(Trigger_Alto, HIGH);      // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(Trigger_Alto, LOW);       // Reads the echoPin, returns the sound wave travel time in microseconds
duration_Al = pulseIn(Echo_Alto, HIGH);
distance_Al= ((duration_Al*0.034/2)+10);       // Calculating the distance
tft.setCursor(53,25);
tft.println(distance_Al,0);             // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
digitalWrite(Led_Al, HIGH);             // Deja encendido el LED An cuando ya se ha tomado la lectura de dicho sensor
SELECT = 0;                             // Una vez tomada la medida cuadrada, se da la instrucción para que los led queden encendidos
tft.setTextColor(WHITE);
  tft.setCursor(15,110);
  tft.println("medir");
lineal = (distance_Al/100);
tft.setTextColor(WHITE,BLACK);
    tft.setCursor(70,7);
    tft.println(lineal,2);         // la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
    Serial.println(lineal,2);      //  la coma " , " indica numero de decimales que nos muestra la medida tomada en el LCD
    duration_L  = 0;
    duration_An = 0;
    duration_Al = 0;
    distance_L  = 0;
    distance_An = 0;
    distance_Al = 0;
}
 if(digitalRead(Push) == LOW && SELECT == 2){
  menu_cubic = 0;
  menu_select = 0;
  menu_lineal = 0;
  SELECT =0;
  menu();
}
}
}

void about(){
  tft.fillScreen();
  tft.setCursor(35, 20);
  tft.setTextColor(GREEN); 
  tft.setTextSize(2);
  tft.println("CUBIC");
  tft.setTextSize(1);
  tft.setCursor(93, 13);
  tft.println("3");
  tft.setTextSize(1);
  tft.setCursor(15, 60);
  tft.println("Desarrollado por");
  tft.setTextSize(1);
  tft.setCursor(20, 80);
  tft.println("RODRIGO MEJIAS");
 while(aboutA == 1){
  if(digitalRead(Push) == LOW){
    aboutA = 0;
  SELECT = 0;
  menu();
  menu_select = 1;
}
 }
}

void LED_PISO(){
unsigned long currentMillis = millis();    // Se toma el tiempo actual
if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(Led_An, ledState);
digitalWrite(Led_L,  ledState);
}
}

void LED_MURO(){
 unsigned long currentMillis = millis();    // Se toma el tiempo actual
if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(Led_An, ledState);
digitalWrite(Led_Al,  ledState);
} 
}

void LED_CUBICO(){
  unsigned long currentMillis = millis();    // Se toma el tiempo actual
if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(Led_An, ledState);
digitalWrite(Led_Al,  ledState);
digitalWrite(Led_L ,  ledState);
}  
}

void LINEAL(){
   unsigned long currentMillis = millis();    // Se toma el tiempo actual
if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(Led_Al, ledState);
}   
}

void startup(){
  tft.drawLine(60,45,60,80,GREEN);
  tft.drawLine(55,50,60,45,GREEN);
  tft.drawLine(65,50,60,45,GREEN);
  tft.drawLine(60,80,90,100,RED);
  tft.drawLine(86,90,90,100,RED);
  tft.drawLine(81,100,90,100,RED);
  tft.drawLine(60,80,30,100,WHITE);
  tft.drawLine(35,90,30,100,WHITE);
  tft.drawLine(39,100,30,100,WHITE);
}

void Draw_lineal(){
 tft.drawLine(60,45,60,80,GREEN);
  tft.drawLine(55,50,60,45,GREEN);
  tft.drawLine(65,50,60,45,GREEN); 
}

void Draw_piso(){
  tft.drawLine(60,80,90,100,RED);
  tft.drawLine(86,90,90,100,RED);
  tft.drawLine(81,100,90,100,RED);
  tft.drawLine(60,80,30,100,WHITE);
  tft.drawLine(35,90,30,100,WHITE);
  tft.drawLine(39,100,30,100,WHITE);
 // tft.setTextColor(WHITE);
  tft.setCursor(20,100);
  tft.println("X");
  tft.setCursor(96,100);
  tft.println("Y");
}

void Draw_muro(){
  tft.drawLine(60,45,60,80,GREEN);
  tft.drawLine(55,50,60,45,GREEN);
  tft.drawLine(65,50,60,45,GREEN);
  tft.drawLine(60,80,30,100,WHITE);
  tft.drawLine(35,90,30,100,WHITE);
  tft.drawLine(39,100,30,100,WHITE);
 // tft.setTextColor(WHITE);
  tft.setCursor(20,100);
  tft.println("X");
  tft.setCursor(58,35);
  tft.println("Z");
}

void power(){
    while(digitalRead(Push) == HIGH){
  }
tft.begin();
  digitalWrite(Backlight,HIGH);
  }

void OFF(){
  tft.fillScreen();
  while (menu_apaga == 1){
    static int pos = 0;
  encoder.tick();
  int newPos = encoder.getPosition();

  if (pos > newPos){           // Sentencia para generar desplazamiento en pantalla dentro Medir M2
    SELECT = SELECT-1 ;
    
    if (SELECT <= 1){
    SELECT = 1;
    }
    apaga();
    }
    
  if (pos < newPos){          // Sentencia para generar desplazamiento en pantalla
    SELECT = SELECT+1 ;
   
    if (SELECT >= 2){
    SELECT = 2;
    }
    apaga();
    }
  if (pos != newPos) {
    pos = newPos;      
}
  if(digitalRead(Push) == LOW && SELECT == 1){
  delay(500);
  menu_cubic = 0;
  menu_select = 0;
  menu_lineal = 0;
  menu_apaga  = 0;
  SELECT =0;
  digitalWrite(Backlight,LOW);
  power();
}

  if(digitalRead(Push) == LOW && SELECT == 2){
  delay(500);
  menu_cubic = 0;
  menu_select = 0;
  menu_lineal = 0;
  menu_apaga  = 0;
  SELECT =0;
  //digitalWrite(Backlight,LOW);
  menu();
}
  }
}


Maker Pro Logo
Continue to site
Quote of the day

-