Hi there folks,
I have been Arduinoing for a few weeks now, just messing about and trying to figure out how it all goes and I think I have just about completed my first proper project.
I say completed - the breadboarding and coding seems to be about on point (at least it compiles and seems to do what I want it to at least most of the time...) and I am currently 3D printing the prototype casing for the hardware ready to start building it in.
While it's printing I thought I'd jump in here, post the code and schematic to see if a) anyone can see any obvious mistakes or improvements and b) anyone wants to take it and have a go.
I'm using a Elegoo Uno R3 clone, 2 IR sensors (the type with 2 LEDs on the end (a dark and a clear one (although Tinkercad doesn't have that type of sensor)), and the 16x2 LCD that came in the Elegoo kit.
I've been using the arduino.cc references and all sorts of other code examples to get it where it now is - learning C++/Arduino language is kicking my butt but is very satisfying when you find that tiny syntax error or typo that's been erroring out all day...
It's for measuring the speed of a 1/10 scale RC car when passing the trap. The idea is that it measures the speed and displays it on the LCD screen. When the speed recorded is a new maximum recorded speed the idea is that the Uno saves the new top speed and flashes a couple of LEDs.
Well, that's the plan anyway. Here's the schematic...
And the code that goes with it...
//Load LCD library
#include <LiquidCrystal.h>
//Global pin assigners and defined containers
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Assign pins for LCD
int SenOne = 6; //Assign pin 6 to SenOne
int SenTwo = 7; //Assign pin 7 to SenTwo
int BlueLEDPin = 8; //Assign signal pin for blue LED
int RedLEDPin = 9; //Assign signal pin for red LED
float SenOneTrigger; //Create float container named 'SenOneTrigger'
float SenTwoTrigger; //Create float container named 'SenTwotrigger'
float Difference; //Create float container named 'Difference', value = 'SenTwoTrigger' minus 'SenOneTrigger' (time in ms to cover 33mm)
float DifferencePerMm; //Create float container named 'DifferencePerMm', value = Difference divided by 33 (time in ms to cover 1mm) ((Milliseconds per Millimetre))
float VelocityInMPS; //Create float container named 'VelocityInMPS', value = DifferencePerMm times 1000 (time in ms to cover 1m) ((Seconds per metre))
float FinalVelocity; //Create float container named 'FinalVelocity', value = VelocityInMPS times 2.237 to get MPH.
float Vmax; //Create float container named 'Vmax', value = 0
float PersonalBest; //Create flost container namec 'PersonalBest'
//-----START OF SETUP SECTION-----
void setup()
{
//Define pinModes and start the LCD
lcd.begin(16, 2); //Start LCD
pinMode(SenOne, INPUT); //Set pinMode of SenOne to INPUT
pinMode(SenTwo, INPUT); //Set pinMode of SenTwo to INPUT
pinMode(BlueLEDPin, OUTPUT); //Set pinMode of BlueLEDPin to OUTPUT
pinMode(RedLEDPin, OUTPUT); //Set pinMode of RedLEDPin to OUTPUT
Vmax = 0; //Set value of var 'Vmax' to 0
}
//-----START OF LOOP SECTION-----
void loop()
{
lcd.setCursor(0, 0); //Set position of LCD cursor
lcd.print("*RC speed trap*"); //Display title
//Actions
while (digitalRead(SenOne)); //Read SenOne
while (digitalRead(SenOne) == 0); //Not entirely sure TBH
SenOneTrigger = millis(); //Save timestamp value in 'SenOneTrigger'
while (digitalRead(SenTwo)); //Read SenTwo
SenTwoTrigger = millis(); //Save timestamp value in 'SenTwoTrigger'
//Calculations
Difference = (SenTwoTrigger - SenOneTrigger); //Set value of var 'Difference' to 'SenTwoTrigger' minus 'SenOneTrigger'
DifferencePerMm = (Difference / 33); //Set value of var 'DifferencePerMm' to 'Difference' divided by 33 (sensor distance in mm) to get per mm
FinalVelocity = DifferencePerMm; //Set value of var 'FinalVelocity' to value of 'DifferencePerMm'
//VelocityInMPS = (DifferencePerMm * 1000); //Unused calculation
//FinalVelocity = (VelocityInMPS * 2.237); //Unused calculation
// Send ouput to LCD display
lcd.setCursor(0, 1);
lcd.print(FinalVelocity); //Var 'FinalVelocity' -> LCD
lcd.print("MPH"); //Text "MPH" -> LCD
//Set vmax of the session
if (FinalVelocity > Vmax) //If variable 'FinalVelocity' is higher than recorded Vmax...
{
Vmax = FinalVelocity; // Set variable Vmax to value in FinalVelocity
//Flashing lights for new Vmax
digitalWrite(BlueLEDPin, HIGH);
delay(50);
digitalWrite(RedLEDPin, HIGH);
delay(50);
digitalWrite(BlueLEDPin, LOW);
delay(50);
digitalWrite(RedLEDPin, LOW);
delay(50);
digitalWrite(BlueLEDPin, HIGH);
delay(50);
digitalWrite(RedLEDPin, HIGH);
delay(50);
digitalWrite(BlueLEDPin, LOW);
delay(50);
digitalWrite(RedLEDPin, LOW);
delay(50);
digitalWrite(BlueLEDPin, HIGH);
delay(50);
digitalWrite(RedLEDPin, HIGH);
delay(50);
digitalWrite(BlueLEDPin, LOW);
delay(50);
digitalWrite(RedLEDPin, LOW);
delay(50);
digitalWrite(BlueLEDPin, HIGH);
delay(50);
digitalWrite(RedLEDPin, HIGH);
delay(50);
digitalWrite(BlueLEDPin, LOW);
delay(50);
digitalWrite(RedLEDPin, LOW);
delay (1000); //Wait 1 second
lcd.clear(); //Clear LCD
}
else { //If not a new 'Vmax'...
lcd.clear(); //Clear LCD
}
}
Any ideas/comments/suggestions?
Thanks folks,
Drew.