Equation not working properly.

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;
    }
  }
}

You should read up on the rules for integer division.
The correct result of dividing 1000 by 1123 is zero.

Welcome :slight_smile:

As jremington said, maybe not clearly, the result of 1000/1123 ( = 0.89 ) is truncated because of integer division, so it returns 0 (and the other division as well) :slight_smile:

The solution is simple: divide by a float instead of an int.

In other words, this:

carTime = ((1000/1123)*(carDistance)) + (431/1123);

should be written as:

carTime = ((1000./1123)*(carDistance)) + (431./1123);

(I hope that I do not get into too much trouble for having handed you an answer.)

Thanks to both of you!! :smiley:

jremington:
You should read up on the rules for integer division.
The correct result of dividing 1000 by 1123 is zero.

guix:
Welcome :slight_smile:

As jremington said, maybe not clearly, the result of 1000/1123 ( = 0.89 ) is truncated because of integer division, so it returns 0 (and the other division as well) :slight_smile:

The solution is simple: divide by a float instead of an int.

I wont tell :wink: I figured it out from the other posts as well, thanks! :slight_smile:

vaj4088:
(I hope that I do not get into too much trouble for having handed you an answer.)

carTime = ((1000/1123)*(carDistance)) + (431/1123);

I wonder if it would work if you kept it in integers. It would certainly be smaller and faster!

carTime = (1000L * carDistance + 431) / 1123;

By doing the multiply and add first the division can give an accurate integer result. The 'L' at the end of the constant '1000L' make it a 'long int' so it can hold values up to 2.1 Billion. That keeps the math from overflowing for values of 'carDistance' greater than 32.

guix:
Welcome :slight_smile:

As jremington said, maybe not clearly, the result of 1000/1123 ( = 0.89 ) is truncated because of integer division, so it returns 0 (and the other division as well) :slight_smile:

The solution is simple: divide by a float instead of an int.

The other way is to do the multoplications first.

y = (1000/1123)x + (431/1123);

becomes
y = (1000*x + 431)/1123;

But this can cause overflows depending on what x is. In this case, you'd want to make the values long
y = (1000L*x + 431L)/1123L;

hi,
Try it in this way. Add .0 to your constants values like 1000.0. Using floating point you need to use all the numbers in floating point.

float carTime;
float carDistance = 0.0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
carTime = ((1000.0/1123.0)*(carDistance)) + (431.0/1123.0);

Serial.print(carTime); // Output = 0.39
delay(5000);
}