### Using Arduino to calculate Quadratic function

This is a simple and quick code I made for teach variables with arduino programming,
where we enter the variable numbers a, b, c in and we get the answer using the Serial Monitor..

[cc lang=”c”]

Simple code showing how to use arduino to calculate Quadratic function.

X => [-b +- sqrt( b^2 – 4ac)] / 2a

Change the a, b and c to get the result via Serial Monitor
*/
int a = 1;
int b = -10;
int c = 25;
float delta, result1, result2;

#include “math.h” // Math!

void setup() {

Serial.begin(9600);

// Calculte delta
// b^2 – 4 * a * c

delta = pow(b,2) – 4 * a * c;

// Analize delta

if(delta<0) {
Serial.print(“DELTA=”);Serial.println(delta);
Serial.println(“Delta < 0, can't calculate!"); // Ends the program } else if (delta==0) { Serial.println("Delta = 0, we can calculate!!!"); result1 = result2 = (-b) / (2 * a); Serial.print("X1=X2="); Serial.println(result1); } else if (delta>0) {

// X1 != X2

delta = sqrt(delta);

Serial.print(“DELTA=”);Serial.println(delta);
Serial.println(“Delta > 0, we can calculate”);

result1 = (-b + delta) / (2 * a);
result2 = (-b – delta) / (2 * a);

Serial.print(“X1=”); Serial.println(result1);
Serial.print(“X2=”); Serial.println(result2);
}

//Done

}

void loop() { }

[/cc]

🙂

## 4 thoughts on “Using Arduino to calculate Quadratic function”

This site uses Akismet to reduce spam. Learn how your comment data is processed.