I'm having issues with turning the information on my buffer from the Ultrasonic sensor and putting it on my 4-digit display. Morse-so the issue seems to be in the way that the data is structured. I setup an array[4] and I'm trying to separate the data so that if I get two values of 45 and 50, the led doesn't show 4550. I know there is a way to separate these two inputs, just don't know how. Any help would be appreciated. I attached the code. The display i'm using is a TM1637 display and the sensor is the hc-sr04.
Any other questions feel free to ask.
#include <TM1637.h>
#define Echo_pin 5
#define Trig_pin 4
#define CLK 7//pins definitions for TM1637 and can be changed to other ports
#define DIO 6
TM1637 tm1637(CLK,DIO);
int distance[4];
void setup() {
// put your setup code here, to run once:
pinMode ( Echo_pin, INPUT);
pinMode ( Trig_pin, OUTPUT);
Serial.begin (9600);
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
Serial.println("<Arduino is ready>");
int data;
}
void loop()
{
for (int i=0; i<4; i++) // This is where the issue is. Need to add some code
// The current for loop just prints out the numbers
// nonstop, I need a way to separate each value
// measured by the Ultrasonic sensor, i.e 40,100,34,1.
{ // Right now it would print it as 4010, 0341.
distance[i]= measure();
Serial.println(distance[i]);
}
int8_t NumTab[] = {0,1,2,3,4,5,6,7,8,9};//0~9,A,b,C,d,E,F
int8_t ListDisp[4];
unsigned char i = 0;
unsigned char count = 0;
tm1637.display(0,distance[3]); / /digit 1 input parameter(digit#,value to be display)
tm1637.display(1,distance[2]); // digit 2 input
tm1637.display(2,distance[1]); // digit 3
tm1637.display(3,distance[0]); // digit 4
delay(300);
}
int measure()
{
digitalWrite(Trig_pin, HIGH);
digitalWrite(Trig_pin, LOW);
int distance = pulseIn(Echo_pin, HIGH, 15000) / 50;
delay(3000);
return constrain(distance, 1, 300);
}