Hi guys. This is my first time using arduino. I am using it on a teensy++. Heres what I am trying to do. I am taking an analog input from a pressure sensor and doing some math to it and eventually displaying it on a LCD.
Problem is I can only get two places past the decimal point. Here is the code so far. I also need to get it to display in notation, so .0001 shows up as 1e-4
Any ideas?
-Jerry
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = 0;
float torr = .0075;
float pa;
float volts;
float pressure;
float offset;
void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
float volts = average * .0097;
float offset = volts - 8.5000;
float pa = pow(10.0000, offset);
// send it to the computer (as ASCII digits)
Serial.println(offset);
Serial.println(pa);
delay(100);
}
I don't know of a way, short of writing your own, to get exponential
format for numeric output. I have tried sprintf() with a "%e" format
but that did not produce usable output.
Here's the final-ish code. I am probably going to add some setpoints to prevent certain valves from opening at high pressures.
-Jerry
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 10, 0, 1, 2, 3, 4, 5, 6, 7);
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = 0;
int exponent;
float torr = .0075;
float pa;
float volts;
float pressure;
float offset;
void setup()
{
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
lcd.begin(20, 4);
lcd.clear();
lcd.setCursor(1, 0);
// Print a message to the LCD.
lcd.print("Turbopump Pressure");
}
void loop() {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
float volts = average * .0097;
float pa = pow(10.0000, offset);
// send it to the computer (as ASCII digits)
if (volts <= 8.5 && volts > 7.62) {
exponent = -3;
offset = volts - 7.62;
}
if (volts <= 7.62 && volts > 6.62) {
exponent = -4;
offset = volts - 6.62;
}
if (volts <= 6.62 && volts > 5.62) {
exponent = -5;
offset = volts - 5.62;
}
if (volts <= 5.62 && volts > 4.62) {
exponent = -6;
offset = volts - 4.62;
}
if (volts <= 4.62 && volts > 3.62) {
exponent = -7;
offset = volts - 3.62;
}
if (volts <= 3.62 && volts > 2.62) {
exponent = -8;
offset = volts - 2.62;
}
if (volts <= 2.62 && volts > 1.8) {
exponent = -9;
offset = volts - 1.62;
}
float torr = pow(10, offset);
Serial.println(exponent);
Serial.println(volts);
Serial.println(offset);
Serial.println(torr);
lcd.setCursor(5, 1);
// Print a message to the LCD.
lcd.print("e Torr");
lcd.setCursor(1, 1);
lcd.print(torr);
lcd.setCursor(6, 1);
lcd.print(exponent);
if (volts > 8.5) {
lcd.setCursor(1, 2);
lcd.print("Overrange ");
}
if (volts < 1.8 && volts > 0.5) {
lcd.setCursor(1, 2);
lcd.print("Underrange ");
}
if (volts < 0.5) {
lcd.setCursor(1, 2);
lcd.print("Sensor Error ");
}
if (volts >= 1.8 && volts <= 8.5) {
lcd.setCursor(0, 2);
lcd.print(" ");
}
delay(50);
}