I am fairly new to c++ and arduino. I have a project for school where I am designing an electric vehicle that is supposed to travel a certain distance between 9 m and 12 m and the distance won't be given until test day. I created an arduino program to accept an input from an lcd display and to be able to use that input to turn on a motor for the time given. This part has worked fine. Now I need to be able to input a distance and have an equation spit out the correct time to turn the motor on. I am having trouble with my equation as it always returns 0 instead of the correct answer. I know that my equation is correct because I have checked many times on my calculator, I just don't know why it's not working. I got the equation from collecting some data points and graphing it and finding the equation for the line and inverting it. This is what i got: y = (1000/1123)x + (431/1123).
Here is all of my code. Please help!!
#include <LiquidCrystal.h>
#define motorPin 1
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int acd_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// define the variable for distance/time
float carDistance = 0.0;
float carTime;
// read the buttons
int read_LCD_buttons()
{
acd_key_in = analogRead(0); // read the value from the sensor
if (acd_key_in > 1000) return btnNONE;
if (acd_key_in < 50) return btnRIGHT;
if (acd_key_in < 250) return btnUP;
if (acd_key_in < 450) return btnDOWN;
if (acd_key_in < 650) return btnLEFT;
if (acd_key_in < 850) return btnSELECT;
return btnNONE;
}
void setup()
{
lcd.begin(16, 2); // start the library
pinMode(motorPin, OUTPUT);
}
void loop()
{
lcd_key = read_LCD_buttons(); // read the buttons
lcd.setCursor(0,0);
lcd.print("Enter Distance:");
lcd.setCursor(0,1);
lcd.print(carDistance);
lcd.setCursor(12,1);
lcd.print(millis()/1000);
switch (lcd_key)
{
case btnUP:
{
carDistance ++; // increase time by 1
delay(150); // delays the button
break;
}
case btnDOWN:
{
carDistance --; // decrease time by 1
delay(150);
if(carDistance < 0) { // if the distance goes negative, change back to 0
carDistance = 0;
}
break;
}
case btnRIGHT:
{
carDistance += 0.01; // increase time by 0.01
delay(150);
break;
}
case btnLEFT:
{
carDistance -= 0.01; //decrese time by 0.01
delay(150);
if(carDistance < 0) { // if the distance goes negative, change back to 0
carDistance = 0;
}
break;
}
case btnSELECT:
{
lcd.setCursor(7,1); // sets cursor
lcd.print("Go!");
delay(1100);
lcd.noDisplay();
// equation to convert distance into a delay time
// y = carTime
// x = carDistance
carTime = ((1000/1123)*(carDistance)) + (431/1123);
digitalWrite(motorPin, HIGH); // set the motor pin to HIGH
// delay(carDistance * 1000); // sets delay based on the # of seconds entered
delay(carTime * 1000); // sets delay based on the equation to convert meters to seconds
digitalWrite(motorPin, LOW); // turn the motor pin back to LOW, turning off the motor
lcd.display(); // turns the display back on
lcd.clear();
break;
}
}
}