As promised, below is the code:
#include <stdio.h>
/* Font */
/* font pixels */
byte Font5x9Mono[535] =
{
// Bytes removed from code brevity
}; /* 535 bytes */
/* 4-bit serial communication LDS183 LCD module */
const int SCOMMAND = 0;
const int SDA2TA = 1;
const int DC = 4;
const int CS = 2;
const int SDA2 = 6;
const int RESET = 3;
const int CLK = 5;
byte gr;
byte gg;
byte gb;
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
char diminput[3];
float sensor1_cal = 0.0;
float sensor2_cal = 0.0;
float sensor3_cal = 0.0;
float sensor4_cal = 0.0;
void setup() {
//Open up a serial connection
Serial.begin(9600);
sensor1_cal = sensor_cal(30, 32);
delay(200);
sensor2_cal = sensor_cal(22, 24);
delay(200);
sensor3_cal = sensor_cal(34, 36);
delay(200);
sensor4_cal = sensor_cal(26, 28);
pinMode(DC, OUTPUT);
pinMode(CS, OUTPUT);
pinMode(SDA2, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(CLK, OUTPUT);
lcdInit();
//Wait for the serial connection
delay(500);
// background color
sendCMD(0x2C);
int i, j, k, l;
for (i=0; i<5504; i++) {
setPixel(0, 0, 0);
}
for (i=5504; i<128*128; i++) {
setPixel(0, 255, 0);
}
setColor(255, 255, 255);
printString("Device is Ready...", 0, 0);
printString("Last Read:", 0, 20);
printString("000000000", 0, 30);
}
// sensor1, pins 22-trig, 24-echo
// sensor2, pins 26-trig, 28-echo
// sensor3, pins 30-trig, 32-echo
// sensor4, pins 34-trig, 36-echo
//Refer to diagram below for sensor arrangement
////////BACK////////
//2 3 4//
// //
// 1 //
void loop()
{
if(Comp("D") == 0)
{
//read sensor 1
String sensor1_rd = sensor_read(30, 32, sensor1_cal);
//String sensor1_rd = "333";
//Wait between sensor readings to prevent interaction between sensors
delay(100);
String sensor3_rd = sensor_read(34, 36, sensor3_cal);
delay(100);
String sensor_combined_rd = sensor_read_dual(22, 24, 26, 28, sensor2_cal, sensor4_cal);
Serial.print('0');
String composite = sensor_combined_rd + sensor1_rd + sensor3_rd;
Serial.print(composite);
Serial.println();
char charBuf[10];
composite.toCharArray(charBuf, 10);
setColor(255, 255, 255);
printString(charBuf, 0, 30);
sendCMD(0x2C);
}
}
String sensor_read_dual(int pingPin1,int inPin1, int pingPin2,int inPin2, float initial_distance1, float initial_distance2)
{
float inches1 = sensor_cal(pingPin1, inPin1);
delay(100);
float inches2 = sensor_cal(pingPin2, inPin2);
float combined_initial= (initial_distance1 + initial_distance2)/2;
float combined_inches = inches1 + inches2;
float inches_width = (combined_initial-combined_inches);
float inches_modulo = fmod(combined_inches,1);
int inches_width_rounded = 0;
if(inches_modulo >= 0.27)
{
inches_width_rounded = (inches_width-inches_modulo)+1;
}
else
{
inches_width_rounded = (inches_width-inches_modulo);
}
if(inches_width_rounded < 0)
{
inches_width_rounded = 0;
}
String sensor_rd = String(inches_width_rounded);
if(sensor_rd.length() == 0)
{
sensor_rd = "000";
}
else if(sensor_rd.length() == 1)
{
sensor_rd = "00" + sensor_rd;
}
else if(sensor_rd.length() == 2)
{
sensor_rd = "0" + sensor_rd;
}
return sensor_rd;
}
String sensor_read(int pingPin,int inPin, float initial_distance)
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
float duration, inches, cm, inches_modulo, inches_width;
int inches_width_rounded = 0;
// offset by 0.82 inches from 11
//float initial_distance = 10.18;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
// convert the time into a distance
inches = (duration / 73.746) / 2;
inches_width = (initial_distance-inches);
inches_modulo = fmod(inches_width,1);
if(inches_modulo >= 0.36)
{
inches_width_rounded = (inches_width-inches_modulo)+1;
}
else
{
inches_width_rounded = (inches_width-inches_modulo);
}
if(inches_width_rounded < 0)
{
inches_width_rounded = 0;
}
String sensor1_rd = String(inches_width_rounded);
if(sensor1_rd.length() == 0)
{
sensor1_rd = "000";
}
else if(sensor1_rd.length() == 1)
{
sensor1_rd = "00" + sensor1_rd;
}
else if(sensor1_rd.length() == 2)
{
sensor1_rd = "0" + sensor1_rd;
}
return sensor1_rd;
}
float sensor_cal(int pingPin,int inPin)
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
float duration, inches, cm, inches_modulo, inches_width;
int inches_width_rounded = 0;
// offset by 0.82 inches from 11
//float initial_distance = 10.18;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
// convert the time into a distance
inches = (duration / 73.746) / 2;
return inches;
}
char Comp(char* This){
while(Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
if(inChar != '\r')
{
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
}
String convchar = String(inData);
if(convchar==This){
for(int i=0;i<19;i++){
inData[i]=0;
}
index=0;
return(0);
}
else{
for(int i=0;i<19;i++){
inData[i]=0;
}
index=0;
return(1);
}
}
void shiftBits(byte b, int dc) {
digitalWrite(CLK, 0);
if ((b&128)!=0)
digitalWrite(SDA2, 1);
else
digitalWrite(SDA2, 0);
digitalWrite(CLK, 1);
digitalWrite(CLK, 0);
if ((b&64)!=0)
digitalWrite(SDA2, 1);
else
digitalWrite(SDA2, 0);
digitalWrite(CLK, 1);
digitalWrite(CLK, 0);
if ((b&32)!=0)
digitalWrite(SDA2, 1);
else
digitalWrite(SDA2, 0);
digitalWrite(CLK, 1);
digitalWrite(CLK, 0);
if ((b&16)!=0)
digitalWrite(SDA2, 1);
else
digitalWrite(SDA2, 0);
digitalWrite(CLK, 1);
digitalWrite(CLK, 0);
if ((b&8)!=0)
digitalWrite(SDA2, 1);
else
digitalWrite(SDA2, 0);
digitalWrite(CLK, 1);
digitalWrite(CLK, 0);
if ((b&4)!=0)
digitalWrite(SDA2, 1);
else
digitalWrite(SDA2, 0);
digitalWrite(CLK, 1);
digitalWrite(CLK, 0);
if ((b&2)!=0)
digitalWrite(SDA2, 1);
else
digitalWrite(SDA2, 0);
digitalWrite(CLK, 1);
digitalWrite(CLK, 0);
if ((b&1)!=0)
digitalWrite(SDA2, 1);
else
digitalWrite(SDA2, 0);
if (dc == SDA2TA)
digitalWrite(DC, 1);
else
digitalWrite(DC, 0);
digitalWrite(CLK, 1);
}
//send data
void sendData(byte data) {
shiftBits(data, SDA2TA);
}
//send cmd
void sendCMD(byte data) {
shiftBits(data, SCOMMAND);
}
void setXY(byte x, byte y, byte dx, byte dy) {
sendCMD(0x2A);
sendData(x);
sendData(x+dx-1);
sendCMD(0x2B);
sendData(y);
sendData(y+dy-1);
sendCMD(0x2C);
}
//converts a 3*8Bit-RGB-Pixel to the 2-Byte-RGBRGB 565 Format of the Display
void setPixel(byte r,byte g,byte b) {
sendData((r&248)|g>>5);
sendData((g&7)<<5|b>>3);
}
void printString(char *st, byte x, byte y) {
int stl, i;
stl = strlen(st);
for (i=0; i<stl; i++)
printChar(*st++, x + (i*6), y);
}