This code is for a device that times how long an object takes to pass using two sensors and calculates the initial and final speeds from the time they are triggered for. For some reason, the lcd.print command is not displaying more than one letter per command. Also, it outputs this correctly:
Serial.println(abs((float)(speed2 / 10000.000)),2);
//and
lcd.print(abs((float)(speed2 / 10000.000)),2);
but not this:
lcd.print(abs((float)(speed1 / 10000.000)),2);
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int unit = A3;
int sens1 = 4;
int sens2 = 5;
long starttime1;
long starttime2;
int speed1 = 0;
int speed2 = 0;
long tempvalue;
int delaytime = 10;
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
pinMode(sens1, INPUT);
pinMode(sens2, INPUT);
pinMode(A3, INPUT);
lcd.setCursor(0,0);
if (analogRead(A3) < 930)
{lcd.print("m");
lcd.setCursor(1,0);
lcd.print("/");
lcd.setCursor(2,0);
lcd.print("s");
lcd.setCursor(3,0);
lcd.print(" ");
}
else {
lcd.print("c");
lcd.setCursor(1,0);
lcd.print("m");
lcd.setCursor(2,0);
lcd.print("/");
lcd.setCursor(3,0);
lcd.print("s");
}
lcd.setCursor(0,1);
lcd.print("t=");
lcd.setCursor(1,1);
lcd.print("=");
lcd.setCursor(8,0);
lcd.print("V");
lcd.setCursor(8,1);
lcd.print("V");
lcd.setCursor(9,0);
lcd.print("1");
lcd.setCursor(9,1);
lcd.print("2");
lcd.setCursor(10,0);
lcd.print("=");
lcd.setCursor(10,1);
lcd.print("=");
}
void loop() {
Serial.begin(9600);
//sensor 1 (pin 4) tripped first
fail1:
if ((digitalRead(4) == 1) && (speed1 == 0)) {
delay(delaytime);
if (digitalRead(4) == 0) {
goto fail1;
}
else {
Serial.println("sens1 triggered");
//log start time
unsigned long starttime1 = (millis()-(long)delaytime);
//wait until sensor 1 is no longer tripped
fail2:
do {Serial.println("waiting");}
while (digitalRead(4) == 1);
delay(delaytime);
if (digitalRead(4) == 1) {
goto fail2;
}
else{
//calculate initial speed
int speed1 = 500000L / (long)((millis()- (long)delaytime) - (long)starttime1);
Serial.println(abs((float)(speed1 / 10000.000)),2);
}
fail3:
//wait until sensor 2 (pin 5) is tripped
do {}
while (digitalRead(5) == 0);
delay(delaytime);
if (digitalRead(5) == 0) {
goto fail3;
}
else {
//log time it's tripped at
unsigned long starttime2 = (millis()-(long)delaytime);
Serial.println((long)starttime2);
//wait until sensor 2 is no longer tripped
fail4:
do {}
while (digitalRead(5) == 1);
delay(delaytime);
if (digitalRead(5) == 1) {
goto fail4;
}
else {
//calculate final speed
int speed2 = 500000L / (long)((millis()- (long)delaytime) - (long)starttime2);
Serial.println(abs((float)(speed2 / 10000.000)),2);
//calculate total time
long totaltime = ((long)starttime2) - ((long)starttime1);
//print calculated values
lcd.setCursor(3,1);
Serial.println(((float)totaltime / 1000.000),2);
lcd.print(" ");
lcd.setCursor(3,1);
lcd.print(((float)totaltime / 1000.000),2);
//if unit is m/s
if ((analogRead(A3) < 930))
{
lcd.setCursor(12,0);
lcd.print(abs((float)(speed1 / 10000.000)),2);
lcd.setCursor(12,1);
lcd.print(abs((float)(speed2 / 10000.000)),2);
}
//if unit is cm/s
else
{
lcd.setCursor(12,0);
lcd.print(abs((float)(speed1 / 100.000)),2);
lcd.setCursor(12,1);
lcd.print(abs((float)(speed2 / 100.000)),2);
}
delay(5000);
}
}
}
}
//sensor 2 tripped first
if ((digitalRead(5) == 1) && (speed1 == 0)) {
delay(delaytime);
if (digitalRead(5) == 0) {
goto fail1;
}
else {
Serial.println("sens2 triggered");
//log start time
unsigned long starttime1 = (millis()-delaytime);
//wait until sensor 2 is no longer tripped
fail5:
do {Serial.println("waiting");}
while (digitalRead(5) == 1);
delay(delaytime);
if (digitalRead(5) == 1) {
goto fail5;
}
else{
//calculate initial speed
int speed1 = 500000L / (long)((millis()- (long)delaytime) - (long)starttime1);
Serial.println(abs((float)(speed1 / 10000.000)),2);
}
fail6:
//wait until sensor 1 (pin 4) is tripped
do {}
while (digitalRead(4) == 0);
delay(delaytime);
if (digitalRead(4) == 0) {
goto fail6;
}
else {
//log time it's tripped at
unsigned long starttime2 = (millis()- (long)delaytime);
Serial.println((long)starttime2);
//wait until sensor 1 is no longer tripped
fail7:
do {}
while (digitalRead(4) == 1);
delay(delaytime);
if (digitalRead(4) == 1) {
goto fail7;
}
else {
//calculate final speed
int speed2 = 500000L / (long)((millis()- (long)delaytime) - (long)starttime2);
Serial.println(abs((float)(speed2 / 10000.000)),2);
//calculate total time
long totaltime = ((long)starttime2) - ((long)starttime1);
//print calculated values
lcd.setCursor(3,1);
Serial.println(((float)totaltime / 1000.000),2);
lcd.print(" ");
lcd.setCursor(3,1);
lcd.print(((float)totaltime / 1000.000),2);
//if unit is m/s
if ((analogRead(A3) < 930))
{
lcd.setCursor(12,0);
lcd.print(abs((float)(speed1 / 10000.000)),2);
lcd.setCursor(12,1);
lcd.print(abs((float)(speed2 / 10000.000)),2);
}
//if unit is cm/s
else
{
lcd.setCursor(12,0);
lcd.print(abs((float)(speed1 / 100.000)),2);
lcd.setCursor(12,1);
lcd.print(abs((float)(speed2 / 100.000)),2);
}
delay(5000);
}
}
}
}
}