Ok. I'm learning and making progress here but I still am struggling when it comes to merging codes together and getting them to operate in the right sequence. Here are the two codes I'm trying to merge.
#include <LiquidCrystal_I2C.h> // LiquidCrystal I2C - Version: 1.1.2
#include <fastIO.h> // fastIO - Version: Latest
#include <Wire.h>
#include "DHT.h"
#define DHT11Pin 4
#define DHTType DHT11 //OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
DHT HT(DHT11Pin,DHTType);
float humi;
float tempC;
float tempF;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
LiquidCrystal_I2C lcd(0x27,20,4); // double check your LCD address first using: i2C_scanner
int analogInPin1 = A0; // Speed Trap beam Analog input Lane 1 A0 Pin
int analogOutPin1 = A1; // Finish line beam lane 1
int analogInPin2 = A3; // Speed trap beam Analog input Lane 2 A3 Pin
int analogOutPin2 = A2; // Finish line beam lane 2
const int ledPin1 = 11; //lane 1 win LED
const int ledPin2 = 12; //Lane 2 win LED
int StartSwitch = 2;
int sensorValueStart1 = 0;
int sensorValueFinish1 = 0;
int sensorValueStart2 = 0;
int sensorValueFinish2 = 0;
int StartState = 0;
int sensorThresh = 800; //adjustable trigger sensing threshold of the IR receivers. 1000 = unblocked(high)
unsigned long timeFirst1; //lane 1 speed trap beam
unsigned long timeSecond1; //lane 1 finish line
unsigned long timeFirst2; //lane 2 speed trap beam
unsigned long timeSecond2; //lane 2 finish line
float StartTime = 0;
float StartTime1 = 0;
float trapTime1 = 0;
float velocity1 = 0;
float trapTime2 = 0;
float velocity2 = 0;
float speedConst = 1800; // ((distance between the Speed Gate and Finish IR sensors in mm) x 3600)/1000)to convert mm/millis to km/h)
// In this project we have 500mm between IR sensors on proto board >>> 500x3600/1000 = 1800
float ET1 = 0;
float ET2 = 0;
void setup()
{
Serial.begin(9600);
pinMode(StartSwitch, INPUT);
pinMode(11, OUTPUT); //lane 1
pinMode(12, OUTPUT); //lane 2
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
timeSecond1 = 0;
timeFirst1= 0;
timeSecond2 = 0;
timeFirst2= 0;
trapTime2 = 0;
trapTime1 = 0;
StartTime = 0;
lcd.init();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Drag Strip Timer");
lcd.setCursor(3,1);
lcd.print("by Ricky Kelso");
lcd.setCursor(1,2);
lcd.print("EET411L Sr Project");
delay(2000);
lcd.setCursor(1,3);
lcd.print("Ready to race...v6");
HT.begin();//For DHT11
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 //For OLED I2C
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display(); //Display logo
display.clearDisplay();
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
delay(3000);
digitalWrite(5, HIGH); // turn on LED2
delay(1000); // wait for 1000ms
digitalWrite(5, LOW); // turn off LED1
digitalWrite(6, HIGH); // turn on LED2
delay(1000);
digitalWrite(6, LOW); // turn off LED2
digitalWrite(7, HIGH); // turn on LED3
delay(1000); // wait for 1000ms
digitalWrite(7, LOW);
}
void loop()
{
StartState = digitalRead(StartSwitch); //Check state of start gate. 0 = closed & 1 = open.
if(StartState == 0)
{
StartTime = 0;
ET1 = 0;
ET2 = 0;
}
// NOTE: Race timer will reset itself when the startgate is closed, so don't close the gate when the race is still going!
StartState = digitalRead(StartSwitch); //if start gate is trigger, race started. 1 = open.
if(StartState == 1 && StartTime == 0)
{
StartTime = micros(); //use micro seconds since the races can be that close!
}
//read the analog in value of IR's:
sensorValueStart1 = analogRead(analogInPin1); // Speed trap Lane 1
sensorValueStart2 = analogRead(analogInPin2); // Finish line Lane 1
sensorValueFinish1 = analogRead(analogOutPin1); // Speed trap Lane 2
sensorValueFinish2 = analogRead(analogOutPin2); //Finish line Lane 2
// wait for the speed trap start sensors to be triggered
if(sensorValueStart1 < sensorThresh)
{
timeFirst1 = micros(); //Lane 1 speed trap start time (First IR Sensor)
}
if(sensorValueStart2 < sensorThresh)
{
timeFirst2 = micros(); //Lane 2 Speed trap start time (First IR Sensor)
}
// wait/check for the Finish Line sensors to be triggered
if(sensorValueFinish1 < sensorThresh && timeFirst1 > 0 && ET1 == 0)
{
timeSecond1 = micros();
ET1 = timeSecond1 - StartTime;
ET1 = ET1 / 1000000; //converting microseconds to milliseconds
}
if(sensorValueFinish2 < sensorThresh && timeFirst2 > 0 && ET2 == 0)
{
timeSecond2 = micros();
ET2 = timeSecond2 - StartTime;
ET2 = ET2 / 1000000; //converting microseconds to milliseconds
}
if (ET1 < ET2 && ET1 != 0 && ET2 != 0)// Set winner Lane 1, turn on winner LED
{
digitalWrite(11, HIGH);
}
if(ET1 > ET2 && ET2 != 0 && ET1 != 0) // Set winner Lane 2, turn on winner LED
{
digitalWrite(12, HIGH);
}
if(ET1 > 0 && ET2 > 0) // Race is over, display times for both lanes.
{
trapTime1 = timeSecond1 - timeFirst1;
trapTime2 = timeSecond2 - timeFirst2;
trapTime1 = trapTime1 / 1000;
velocity1 = speedConst / trapTime1;//get the Speed converted from mm/millis to km/h.
velocity1 = velocity1 * 0.621371; // use this line to convert KM/H to MP/H
trapTime2 = trapTime2 / 1000;
velocity2 = speedConst / trapTime2;//get the Speed converted from mm/millis to km/h.
velocity2 = velocity2 * 0.621371; // use this line to convert KM/H to MP/H
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Lane 1" );
lcd.setCursor(0,1);
lcd.print("ET:");
lcd.setCursor(3,1);
lcd.print(ET1, 4);
lcd.setCursor(10,1);
lcd.print("@ ");
lcd.print(velocity1);
lcd.setCursor(17,1);
lcd.print("mph"); //km or mph
if (digitalRead(11) == HIGH)
{
lcd.setCursor(10,0);
lcd.print("*WINNER*");
}
lcd.setCursor(0,2);
lcd.print("Lane 2" );
lcd.setCursor(0,3);
lcd.print("ET:");
lcd.setCursor(3,3);
lcd.print(ET2, 4);
lcd.setCursor(10,3);
lcd.print("@ ");
lcd.print(velocity2);
lcd.setCursor(17,3);
lcd.print("mph"); //km or mph
if (digitalRead(12) == HIGH)
{
lcd.setCursor(10,2);
lcd.print("*WINNER*");
}
delay(10000);
digitalWrite(11, LOW); //turn off lane winner LED
digitalWrite(12, LOW); //turn off lane winner LED
timeSecond1 = 0;
timeFirst1= 0;
timeSecond2 = 0;
timeFirst2= 0;
trapTime2 = 0;
trapTime1 = 0;
velocity1 = 0;
velocity2 = 0;
StartTime = 0;
ET1 = 0;
ET2 = 0;
}
humi = HT.readHumidity();
tempC = HT.readTemperature();
tempF = HT.readTemperature(true);
display.clearDisplay();
oledDisplayHeader();
oledDisplay(3,5,28,humi,"%");
oledDisplay(3,60,28,tempF,"F");
display.display();
}
void oledDisplayHeader(){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print(" Hum ");
display.setCursor(60, 0);
display.print(" Temp");
}
void oledDisplay(int size, int x,int y, float value, String unit){
int charLen=12;
int xo=x+charLen*3.2;
int xunit=x+charLen*3.6;
int xval = x;
display.setTextSize(size);
display.setTextColor(WHITE);
if (unit=="%"){
display.setCursor(x, y);
display.print(value,0); //decimal places for humidity
display.print(unit);
} else {
if (value>99){
xval=x;
} else {
xval=x+charLen;
}
display.setCursor(xval, y);
display.print(value,0);
display.drawCircle(xo, y+2, 2, WHITE); // print degree symbols ( )
display.setCursor(xunit, y);
display.print(unit);
}
}
This is the second code-
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT); //Lane 1 prestage.
pinMode(A6, INPUT); //Lane 1.
pinMode(8, OUTPUT); //Lane 2 prestage
pinMode(A7, INPUT); // Lane 2
}
void loop()
{
digitalWrite(9, analogRead(A6) <= 300);// LED on, if analog input <= 300
digitalWrite(8, analogRead(A7) <= 300);
}
These 2 leds need to come on before the countdown starts here
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
delay(3000);
digitalWrite(5, HIGH); // turn on LED2
delay(1000); // wait for 1000ms
digitalWrite(5, LOW); // turn off LED1
digitalWrite(6, HIGH); // turn on LED2
delay(1000);
digitalWrite(6, LOW); // turn off LED2
digitalWrite(7, HIGH); // turn on LED3
delay(1000); // wait for 1000ms
digitalWrite(7, LOW);
After this the button is depressed, the cars roll off and then the two leads should go out. I've tried every placement of the second code I can think of and I can't get it to work. Is the delay (xx) throwing me off or what?