/* Right Triangle - User Interactive via Serial Terminal
M. Ray Burnette 20130125
Arduino Nano 328P target processor
Binary sketch size: 5,202 bytes (of a 30,720 byte maximum)
*/
#include "math.h" // include the Math Library
#define BAUD 9600
#define timeoutPeriod 2147483647 // Long var... about 25 days
float a;
float b;
float h;
char junk = ' ';
void setup() // run once, when the sketch starts
{
Serial.begin(BAUD); // set up Serial library at 9600 bps
Serial.setTimeout(timeoutPeriod); // default is 1 second
Serial.println("Let's calculate a hypotenuse, h");
Serial.println(" /|");
Serial.println(" / |");
Serial.println(" / |");
Serial.println(" h / |");
Serial.println(" / b|");
Serial.println(" / |");
Serial.println(" / |");
Serial.println(" /________a_____|");
Serial.println("");
Serial.println("");
}
void loop()
{
Serial.println("Enter value for leg 'a', Press ENTER");
while (Serial.available() == 0) ; // Wait here until input buffer has a character
{
//Side 1
a = Serial.parseFloat(); // new command in 1.0 forward
Serial.print("a = "); Serial.println(a, DEC);
while (Serial.available() > 0) // .parseFloat() can leave non-numeric characters
{ junk = Serial.read() ; } // clear the keyboard buffer
}
Serial.println("Enter value for leg 'b', Press ENTER");
while (Serial.available() == 0) ;
{
//Side 2
b = Serial.parseFloat();
Serial.print("b = "); Serial.println(b, DEC);
while (Serial.available() > 0)
{ junk = Serial.read() ; }
h = sqrt ( a*a + b*b );
Serial.print("hypotenuse = ");
Serial.println(h, DEC); Serial.println();
}
}
Much more complex:
Don't Cross The Streams (FP scientific calculator serial co-processor) - Community / Exhibition / Gallery - Arduino Forum