quadratic formula programming

I am trying to get arduino to recieve the 3 quadratic function values and return the answer(s):

void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT);
    pinMode(13, OUTPUT); 

  int a = 0;
  int b  = 0;
  int c = 0;


Serial.println("Please Enter Your Function's a: ");
while (a <=  0)
{
 a = Serial.read();  
// delay(3000);
}
//delay(1000);
//a = a - 48;
Serial.println("Please Enter Your Function's b: ");
while (b <=  0)
{
b = Serial.read();
// delay(3000);
}
//delay(1000);
//b = b - 48;
Serial.println("Please Enter Your Function's c: ");
while (c <=  0)

{
c = Serial.read();  
// delay(3000);
}
//c = c - 48; 
int  d = sqrt(b * b - 4. * a * c);

int x1 = (-b + d) / (2. * a);
int hh = (-b - d) / (2. * a);


Serial.println(x1);
Serial.println(hh); 


}
void loop()
{
/*  int a = 0;
  int b  = 0;
  int c = 0;
int d = 0;

Serial.println("Please Enter Your Function's a: ");
while (a <=  0)
{
 a = Serial.read();  
// delay(3000);
}
//delay(1000);
//a = a - 48;
Serial.println("Please Enter Your Function's b: ");
while (b <=  0)
{
b = Serial.read();
// delay(3000);
}
//delay(1000);
//b = b - 48;
Serial.println("Please Enter Your Function's c: ");
while (c <=  0)

{
c = Serial.read();  
// delay(3000);
}
//c = c - 48; 
  d = sqrt(b * b - 4. * a * c);

int x1 = (-b + d) / (2. * a);
int hh = (-b - d) / (2. * a);


Serial.println(x1);
Serial.println(hh); */

}

it does not work
can anyone help me out?

You need to check that you have good data prior to reading, else you are reading garbage input all the time. http://arduino.cc/en/Serial/Available

I would also flush the incoming buffer prior to reading http://arduino.cc/en/Serial/Flush

I see two problems:

For your variables you need to use type float rather than int. Ints are integers and get rounded off.

Serial.read() returns a value of type char. So if someone types in "3" read() is going to return the ascii value of the character "3" which is 51. What you want to do is read from the serial port into a string and then use sscanf() to convert the string into a float.

The question is, what does not work?

Anyway, 3 things I do notice: 1) the reading of the values from the serial is not clean (it will only fetch numbers from 0 to 9, parsing as keyword). 2) the loop code is in comments, maybe this is the problem? 3) You use integer values with some float calculation. You want to calculate in int or in float?

HTH Juergen

  1. why only 1-9?
  2. It wasnt commented out at first... I moved it to setup and now changed it back so just ignore that.
  3. for now Im trying an int after that works I want to switch to float or double. Im now reading meterial about all of your suggestions(all of you) and will trying them now.

The reading of characters is addressed by the (51-x). Putting the code in setup() just ensures it runs only once.

defining #s as float probably makes sense. After reading all the data from the serial port, convert to float http://arduino.cc/en/Reference/FloatCast http://arduino.cc/en/Reference/Float and then do the math.

I didnt understand all of your suggestions but as far as I understood this is what I did and still doesnt work
btw I couldnt figure out how to use sscanf() so I used atoi() with chars:

void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT);
    pinMode(13, OUTPUT); 
    char a1[3] = "3";



}
void loop()
{
  char a1[3] = "";
 char b1[3] = "";
 char c1[3] = "";
 int a = 0;
 int b = 0;
 int c = 0;
int d = 0;
Serial.flush();
Serial.println("Please Enter Your Function's a: ");
while (a <=  0)
{
 a = Serial.read();  
// delay(3000);
}
//delay(1000);
//a = a - 48;
Serial.println("Please Enter Your Function's b: ");
while (b <=  0)
{
b = Serial.read();
// delay(3000);
}
//delay(1000);
//b = b - 48;
Serial.println("Please Enter Your Function's c: ");
while (c <=  0)

{
c = Serial.read();  
// delay(3000);
}
//c = c - 48; 
a = atoi(a1);
b = atoi(b1);
c = atoi(c1);
  d = sqrt(b * b - 4 * a * c);

int x1 = (-b + d) / (2 * a);
int hh = (-b - d) / (2 * a);


Serial.println(x1);
Serial.println(hh); 

}

I removed the periods before multipling and it prints out numbers(not the right ones but still)

As you probably know by now Im new here.
Can someone please edit my code and repost it because I dont understand some of the suggestions
I will understand if you wont do that but I though I would at least ask.

ok ok now I got it!!! when it pritns zero it doesnt meen zero it means it cannot calculate! because theres no solution when I print a b and c out from the original code it shows me the hex value or whatever heres the million dollar question: how do I convert 49 to 1? I would say hex to ascii but im not sure thats their names

*not hex to ascii but dec to ascii

how do I convert 49 to 1?

By subtracting '0'.