The reason it bounces between 750/760 is probably just because of the resolution of the ADC inside the unit. Don't worry about it.
Code: Select all
#include <PID_v1.h>
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
#include <LiquidCrystalFast.h>
// initialize the library with the numbers of the interface pins
LiquidCrystalFast lcd(33, 32, 31, 30, 29, 28, 27);
// LCD pins: RS RW EN D4 D5 D6 D7
//Pressure Gauge Variables
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int indexr = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int decade;
int exponent;
double torr;
float pa;
float volts;
float pressure;
float significand;
float x;
int error;
//Evap Controller Variables
const int chanA = 3;
const int chanB = 4;
const int chanC = 5;
const int chanD = 6;
const int buttonRight = 9;
const int buttonMid = 12;
const int ButtonLeft = 15;
Encoder knobLeft(13, 14);
Encoder knobMid(10, 11);
Encoder knobRight(7, 8);
int chanAvolt = A12;
int chanCvolt = A11;
int chanDvolt = A10;
int pressVolt = A8;
int pressIdent = A9;
int chanArem = A7;
int chanCrem = A6;
int chanDrem = A5;
int chanAfb = A4;
int chanCfb = A3;
int chanDfb = A2;
double SetpointA, InputA, OutputA;
double SetpointC, InputC, OutputC;
double SetpointD, InputD, OutputD;
PID PIDchanA(&InputA, &OutputA, &SetpointA,2,5,1, DIRECT);
PID PIDchanC(&InputC, &OutputC, &SetpointC,2,5,1, DIRECT);
PID PIDchanD(&InputD, &OutputD, &SetpointD,2,5,1, DIRECT);
void setup() {
Serial.begin(9600);
// set up the LCD's number of rows and columns:
lcd.begin(24, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
analogWriteFrequency(3,40000);
analogWriteFrequency(4,40000);
analogWriteFrequency(5,40000);
analogWriteFrequency(6,40000);
pinMode(chanA, OUTPUT);
pinMode(chanB, OUTPUT);
pinMode(chanC, OUTPUT);
pinMode(chanD, OUTPUT);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
//initialize the variables we're linked to
InputA = analogRead(chanAfb);
InputC = analogRead(chanCfb);
InputD = analogRead(chanDfb);
SetpointA = 100;
SetpointC = 100;
SetpointD = 100;
//turn the PID on
PIDchanA.SetMode(AUTOMATIC);
PIDchanC.SetMode(AUTOMATIC);
PIDchanD.SetMode(AUTOMATIC);
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
analogWrite(chanA, 51);
analogWrite(chanB, 102);
analogWrite(chanC, 153);
analogWrite(chanD, 204);
//Gauge loop
// subtract the last reading:
total= total - readings[indexr];
// read from the sensor:
readings[indexr] = analogRead(pressVolt);
// add the reading to the total:
total= total + readings[indexr];
// advance to the next position in the array:
indexr = indexr + 1;
// if we're at the end of the array...
if (indexr >= numReadings)
// ...wrap around to the beginning:
indexr = 0;
// calculate the average:
average = total / numReadings;
volts = average * .0097;
torr = pow(10,(1.667 * volts - 11.46));
if torr
decade = floor(x);
exponent = decade - 7;
significand = pow(10,(x - decade));
//Serial.println(x);
Serial.println(volts);
Serial.println(torr);
//Serial.println(pressVolt);
//Serial.println(decade);
//Serial.println(significand);
if (volts >= 1.8 && volts <= 8.61) {
lcd.setCursor(4, 1);
// Print a message to the LCD.
lcd.print("e");
lcd.setCursor(0, 1);
lcd.print(significand);
lcd.setCursor(5, 1);
lcd.print(exponent);
error = 0;
lcd.setCursor(7, 1);
lcd.print(" Torr");
}
else {
if (volts > 8.61) {
lcd.setCursor(0, 1);
lcd.print(" Overrange ");
error = 1;
}
if (volts < 1.8 && volts > 0.5) {
lcd.setCursor(0, 1);
lcd.print(" Underrange ");
error = 0;
}
if (volts < 0.5) {
lcd.setCursor(0, 1);
lcd.print("Sensor Error ");
error = 1;
}
}
}
Code: Select all
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(PIN_D0, PIN_B7, PIN_B3, PIN_B2, PIN_B0, PIN_B1);
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 decade;
int inputPin = 9;
int exponent;
float torr;
float pa;
float volts;
float pressure;
float significand;
float x;
int error;
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
int state1 = HIGH; // the current state of the output pin
int reading1; // the current reading from the input pin
int previous1 = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long time1 = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
int state2 = HIGH; // the current state of the output pin
int reading2; // the current reading from the input pin
int previous2 = LOW; // the previous reading from the input pin
long time2 = 0; // the last time the output pin was toggled
int state3 = HIGH; // the current state of the output pin
int reading3; // the current reading from the input pin
int previous3 = LOW; // the previous reading from the input pin
long time3 = 0; // the last time the output pin was toggled
int state4 = HIGH; // the current state of the output pin
int reading4; // the current reading from the input pin
int previous4 = LOW; // the previous reading from the input pin
long time4 = 0; // the last time the output pin was toggled
int stateo = HIGH; // the current state of the output pin
int readingo; // the current reading from the input pin
int previouso = LOW; // the previous reading from the input pin
long timeo = 0;
int mvenab;
int pvenab;
int override = LOW;
int overridden = LOW;
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;
pinMode (PIN_F4, OUTPUT); //Turbo Pump
pinMode (PIN_F5, OUTPUT); //Fore Pump
pinMode (PIN_F0, OUTPUT); //Main Valve
pinMode (PIN_F1, OUTPUT); //Probe Valve
pinMode (PIN_D2, INPUT_PULLUP); //Override Input
pinMode (PIN_C7, INPUT_PULLUP); //Main Valve Input
pinMode (PIN_C6, INPUT_PULLUP); //Probe Valve Input
pinMode (PIN_D3, INPUT_PULLUP); //Turbo In
pinMode (PIN_D6, INPUT_PULLUP); //Fore In
pinMode (PIN_D1, INPUT_PULLUP); //Mode Switch
lcd.begin(24, 2);
lcd.clear();
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("FP: TP: MV: PV:");
}
void loop() {
reading = digitalRead(PIN_D3); // Turbo Control
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(PIN_F5, state);
previous = reading;
reading1 = digitalRead(PIN_D6); // Fore Control
if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time1 = millis();
}
previous1 = reading1;
digitalWrite(PIN_F4, state1);
if (state == 0) {
lcd.setCursor(10, 0);
lcd.print(byte(0x95));
}
else {
lcd.setCursor(10, 0);
lcd.print(byte(0x94));
}
if (state1 == 0) {
lcd.setCursor(4, 0);
lcd.print(byte(0x95));
}
else {
lcd.setCursor(4, 0);
lcd.print(byte(0x94));
}
reading2 = digitalRead(PIN_D1); // Pressure Mode Switch
if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce) {
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;
time2 = millis();
}
previous2 = reading2;
// 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;
volts = average * .0097;
if (state2 == 0) torr = volts - 2.1249;
else torr = volts - 2;
x = torr - 1.5;
decade = floor(x);
exponent = decade - 7;
significand = pow(10,(x - decade));
//Serial.println(x);
Serial.println(volts);
Serial.println(average);
//Serial.println(decade);
//Serial.println(significand);
if (volts >= 1.8 && volts <= 8.5) {
lcd.setCursor(4, 1);
// Print a message to the LCD.
lcd.print("e");
lcd.setCursor(0, 1);
lcd.print(significand);
lcd.setCursor(5, 1);
lcd.print(exponent);
error = 0;
if (state2 == 0) {
lcd.setCursor(7, 1);
lcd.print(" Torr");
}
else {
lcd.setCursor(7, 1);
lcd.print(" mBar");
}
}
else {
if (volts > 8.5) {
lcd.setCursor(0, 1);
lcd.print(" Overrange ");
error = 1;
}
if (volts < 1.8 && volts > 0.5) {
lcd.setCursor(0, 1);
lcd.print(" Underrange ");
error = 0;
}
if (volts < 0.5) {
lcd.setCursor(0, 1);
lcd.print("Sensor Error ");
error = 1;
}
}
if (volts < 6.5 && error == 0) pvenab = 1;
else pvenab = 0;
if (volts < 6.5 && error == 0) mvenab = 1;
else mvenab = 0;
readingo = digitalRead(PIN_D2); // Overridee Switch
if (readingo == HIGH && previouso == LOW && millis() - timeo > debounce) {
if (stateo == HIGH)
stateo = LOW;
else
stateo = HIGH;
timeo = millis();
}
previouso = readingo;
stateo = override;
reading3 = digitalRead(PIN_C7); // Main Valve Switch
if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce) {
if (state3 == HIGH)
state3 = LOW;
else
state3 = HIGH;
time3 = millis();
}
previous3 = reading3;
//if (mvenab == 0 && override == 1) overridden = 1;
if (override == HIGH || mvenab == 1) digitalWrite(PIN_F0, state3);
else {
digitalWrite(PIN_F0, LOW);
state3 = 0;
}
reading4 = digitalRead(PIN_C6); // Probe Valve Switch
if (reading4 == HIGH && previous4 == LOW && millis() - time4 > debounce) {
if (state4 == HIGH)
state4 = LOW;
else
state4 = HIGH;
time4 = millis();
}
previous4 = reading4;
if (pvenab == 1) digitalWrite(PIN_F1, state4);
else {
digitalWrite(PIN_F1, LOW);
state4 = 0;
}
if ( state3 == HIGH && override == LOW) {
lcd.setCursor(16,0);
lcd.print(byte(0x3D));
}
else {
if (state3 == HIGH && mvenab == 1) {
lcd.setCursor(16,0);
lcd.print(byte(0x88));
}
else {
lcd.setCursor(16,0);
lcd.print(byte(0x8D));
}
}
if (state4 == HIGH) {
lcd.setCursor(22,0);
lcd.print(byte(0x3D));
}
else {
lcd.setCursor(22,0);
lcd.print(byte(0x8D));
}
delay(50);
}