/*
Air Pressure Measurement usning MPX5010 or MPX5100 Pressure Sensor
Arduino Mega 2560
Board Arduino Nano
Prozessor: ATMEGA 323 P (Old Bootloader)
Port: /dev/ttyusb0
*/
// include the library code:
#include <LiquidCrystal.h>
float SensorOffset = 5.0;
float SensorOffset_L = 5.0;
int temp = 1;
int scale = 800;
boolean scale_blocked = false;
// const float SensorOffset = 38.0; //const
// const float SensorOffset_L = 37.5;
const int numReadings_right = 25; // alt 15
const int numReadings_left = 25 ; // alt 15
const int buttonPin_up = 6; // the number of the pushbutton_up pin
const int buttonPin_down = 7; // the number of the pushbutton_down pin
const int buttonPin_left = 8; //
const int buttonPin_right = 9; //
float readings[numReadings_right]; // the readings from the analog input input A0
int readIndex = 0; // the index of the current reading
float readings_L[numReadings_left]; // the readings from the analog input A1
int readIndex_L = 0; // the index of the current reading
bool has_changed = true;
int del = 25;
float total = 0.0; // the running total_R
float average = 0.0; // the average_R
float total_L = 0.0; // the running total_L
float average_L = 0.0; // the average_L
int inputPin = A0;
int inputPin_L = A1;
int inputPin_Volt = A2;
int buttonState_up = 0; // variable for reading the pushbutton "up" status
int buttonState_down = 0; // variable for reading the pushbutton "down" status
int buttonState_left = 0; // variable for reading the pushbutton "left" status
int buttonState_right = 0; // variable for reading the pushbutton "right" status
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
/*
void calibrate ()
{
float temp;
do
{
temp = (analogRead(inputPin)-SensorOffset)/100.0;
if (temp > 0.0) SensorOffset = SensorOffset - 0.1;
if (temp < 0.0) SensorOffset = SensorOffset + 0.1;
}while (temp != 0.0);
do
{
temp = (analogRead(inputPin)-SensorOffset_L)/100.0;
if (temp > 0.0) SensorOffset_L = SensorOffset_L - 0.1;
if (temp < 0.0) SensorOffset_L = SensorOffset_L + 0.1;
}while (temp != 0.0);
}
*/
// the setup routine runs once when you press reset:
void setup() {
//set pinMode for all Pins that are used as Button input:
pinMode(buttonPin_up, INPUT_PULLUP);
pinMode(buttonPin_down, INPUT_PULLUP);
pinMode(buttonPin_left, INPUT_PULLUP);
pinMode(buttonPin_right, INPUT_PULLUP);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Serial.println("Board Arduino Nano");
Serial.println("Prozessor: ATMEGA 323 P (Old Bootloader)");
Serial.println("Port: /dev/ttyusb0 ");
// special Char up and down
Serial.println("Char_down");
byte down[8] = { 0x04, 0x04, 0x04, 0x04, 0x04, 0x15, 0x0E, 0x04};
lcd.createChar(6, down);
Serial.println("Char_up");
byte up[8] = { 0x04, 0x0E, 0x15, 0x04, 0x04, 0x04, 0x04, 0x04};
lcd.createChar(7, up);
delay(100);
// set up the LCD's number of columns and rows:
lcd.begin(20, 4);
lcd.clear();
Serial.println("LCD 20 x 4 init = ok");
Serial.println("button Pins set to INPUT_PULLUP");
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings_right; thisReading ++) {
readings[thisReading] = 0;
}
for (int thisReading_L = 0; thisReading_L < numReadings_left; thisReading_L ++) {
readings_L[thisReading_L] = 0;
}
Serial.println("moving average buffer initialized");
// Greeting text
Serial.println("Init Text -> Start");
lcd.setCursor(0, 0);
lcd.write(" Synchron - Tester");
lcd.setCursor(0, 1);
lcd.write("warte etwas........ ");
// including calibrating to zero
// calibrate();
temp = 1;
while(temp != 0)
{
temp = (analogRead(inputPin)-SensorOffset);
if (temp > 0){ SensorOffset = SensorOffset + 1;}
else if (temp < 0){ SensorOffset = SensorOffset - 1;}
else if (temp == 0) {
temp=1;
break;
}
Serial.print(SensorOffset);
Serial.print(" ");
Serial.println(temp);
}
lcd.setCursor(2, 3);
lcd.print(SensorOffset,1);
while(temp != 0)
{
temp = (analogRead(inputPin_L)-SensorOffset_L);
if (temp > 0) {SensorOffset_L = SensorOffset_L + 1;}
else if (temp < 0) {SensorOffset_L = SensorOffset_L - 1;}
if (temp == 0){
break;
}
Serial.print(SensorOffset_L);
Serial.print(" ");
Serial.println(temp);
}
lcd.setCursor(8, 3);
lcd.print(SensorOffset_L,1);
delay(1500);
lcd.setCursor(0, 0);
lcd.write("1 Motor aus, ");
lcd.setCursor(0, 1);
lcd.write("2 beide Vergaser ");
lcd.setCursor(0, 2);
lcd.write(" anschliessen ");
lcd.setCursor(0, 3);
lcd.write("3 Motor start -> ");
delay(40);
lcd.clear();
lcd.setCursor(5, 0);
lcd.write("mBar");
lcd.setCursor(5, 1);
lcd.write("mBar");
lcd.setCursor(15, 0);
lcd.write("delay");
}
void LCD_progress_bar (int row, int var, int minVal, int maxVal)
{
int block = map(var, minVal, maxVal, 0, 19); // 16 Block represent the current LCD space (modify the map setting to fit your LCD)
int line = map(var, minVal, maxVal, 0, 95); // 80 Line represent the theoretical lines that should be printed
int bar = (line-(block*5)); // Bar represent the actual lines that will be printed
/* LCD Progress Bar Characters, create your custom bars */
byte bar1[8] = { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10};
byte bar2[8] = { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18};
byte bar3[8] = { 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C};
byte bar4[8] = { 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E};
byte bar5[8] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F};
lcd.createChar(1, bar1);
lcd.createChar(2, bar2);
lcd.createChar(3, bar3);
lcd.createChar(4, bar4);
lcd.createChar(5, bar5);
for (int x = 0; x < block; x++) // Print all the filled blocks
{
lcd.setCursor (x, row);
lcd.write (1023);
}
lcd.setCursor (block, row); // Set the cursor at the current block and print the numbers of line needed
if (bar != 0) lcd.write (bar);
if (block == 0 && line == 0) lcd.write (1022); // 1022 Unless there is nothing to print, in this case show blank
for (int x = 19; x > block; x--) // 16 Print all the blank blocks
{
lcd.setCursor (x, row);
lcd.write (1022);
}
}
// the loop routine runs over and over again forever:
void loop() {
// subtract the last reading:
total = total - readings[readIndex];
total_L = total_L - readings_L[readIndex_L];
// read the input on analog pin 0:
readings[readIndex] = (analogRead(inputPin)-SensorOffset)/100.0; //Do maths for calibration
readings_L[readIndex_L] = (analogRead(inputPin_L)-SensorOffset_L)/100.0; //Do maths for calibration
// add the reading to the total:
total = total + readings[readIndex];
total_L = total_L + readings_L[readIndex_L];
// advance to the next position in the array:
readIndex = readIndex + 1;
readIndex_L = readIndex_L + 1;
// if we're at the end of the array...
if (readIndex >= numReadings_right) {
// ...wrap around to the beginning:
readIndex = 0;
}
if (readIndex_L >= numReadings_left) {
// ...wrap around to the beginning:
readIndex_L = 0;
}
// calculate the average right and left:
average = total / numReadings_right;
average_L = total_L / numReadings_left;
// send it to the computer as ASCII digits
// print out the value you read:
//Serial.println("Air Pressure: ");
//Serial.println(average,2);
//Serial.println(" kPa");
//Serial.print(buttonState_up);
//Serial.println(buttonState_down );
lcd.setCursor(0, 0);
average_L = fabs(average_L);
lcd.print(average_L,2);
lcd.setCursor(0, 1);
average = fabs(average);
lcd.print(average,2);
if (scale_blocked == false){
if ((average > 4.2) or (average_L > 4.2)){
scale = scale + 200;
scale_blocked = true;
lcd.setCursor(13,1);
lcd.print("<");
}
}
// else if ((average > 5) and (average_L > 5)){
// scale = scale - 100;
//}
/* ************** Pfeile UP / DOWN ************************************
**********************************************************************/
if (average_L > average){
lcd.setCursor(10,0);
lcd.write(6);
lcd.setCursor(10,1);
lcd.print(" ");
}
else if (average > average_L){
lcd.setCursor(10,1);
lcd.write(7);
lcd.setCursor(10,0);
lcd.print(" ");
}
/* ************** Write delay *****************************************
**********************************************************************/
if (has_changed = true) {
lcd.setCursor(15, 1);
//lcd.print(numReadings);
lcd.print(del);
has_changed = false;
}
delay(del); // delay in between reads for stability
/* ************** Write progress bar **********************************
**********************************************************************/
LCD_progress_bar (2, average_L * 200, 0, scale); //1024, 768, 512, 256, 128 Scale kann hier verändert werden
LCD_progress_bar (3, average * 200, 0, scale); //800
// read the states of the pushbutton value:
buttonState_up = digitalRead(buttonPin_up);
if (buttonState_up == LOW) {
del ++;
has_changed = true;
}
buttonState_down = digitalRead(buttonPin_down);
if (buttonState_down == LOW && del >= 5) {
del --;
has_changed = true;
}
buttonState_left= digitalRead(buttonPin_left);
if (buttonState_left == LOW && del >= 15) {
del ++;
has_changed = true;
// numReadings_left ++; // smooth
// numReadings_right ++;
}
buttonState_right= digitalRead(buttonPin_right);
if (buttonState_right == LOW) {
int sensorValue = analogRead(inputPin_Volt);
float voltage = sensorValue * (5.0 / 1023.0);
lcd.setCursor(15, 2);
lcd.write("Akku");
lcd.setCursor(15, 3);
lcd.print(voltage,2);
lcd.write("V");
delay(1000);
}
}