Hi,
I am a begginer in Arduino programming and I am working on a project that will serve to control air permeability. Namely, I have 2 solenoid valves, BMP280 sensor, 2 buttons, 2 relays, voltage regulator 12v - 5v for valves, TFT display.
My idea was to program using the switch case and if functions.
An air compressor with a constant pressure of 1 bar is connected at the inlet to the first valve.
The system should work in such a way that by clicking on the first button, the first valve opens and lets air towards the sensor until it reaches a value of 0.15bar, and this is a sign that the first valve is closing. Thus, there is 0.15 bar of air in the system connected to the tested body and at that moment the red light on the display is on.
By pressing the first button again, the test begins in a period of 10sec.
If the pressure does not drop (no holes or air leaks), the green light on the display comes on.
By clicking on the second button, the system releases the air, so that the second solenoid valve opens and closes in a period of 1s.
So one test is completed, the next body is connected and the test is performed again. Also, the goal is to have a test option at 0.3bar and 0.5bar. So I thought it was necessary to use a switch case.
For starters, I would like to achieve a functional test at 0.15.
On the enclosed program code, I managed to display the read pressure on the display, and by increasing it by a certain value, the red light goes out and the green light comes on, but it keeps that shape.
I don't know how to adjust the loop reading within case 1, to print the sensor value once it reaches a higher value and drops again.
Also the second button, I am not sure how to implement inside the cases where the state of the first button changes.
I hope you understand the idea, any help is welcome.
Thank you
P.S. the library has BLUE and RED mixed up so don't get confused.
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
//Define PIN-Wiring
#define BMP_SCK 6
#define BMP_MISO 2
#define BMP_MOSI 5
#define BMP_CS 4
#include <Adafruit_Sensor.h>
#include<stdlib.h>
#define RED 0xFBE0
#define GREEN 0x07E0
#define clk 13 // Don't change
#define din 11 // Don't change
#define cs 10
#define dc 9
#define rst 8 // you can also connect this to the Arduino reset
#include <Adafruit_GFX_AS.h> // Core graphics library
#include <Adafruit_ST7735_AS.h> // Hardware-specific library
Adafruit_ST7735_AS tft = Adafruit_ST7735_AS(cs, dc, rst); // Create the tft object
Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
#define INTERVAL 3000 // Update interval for the display 3000 = 3 seconds
unsigned long startTime = 0;
float t = -15.0;
const int relayEnable = 12;
#define buttonPin1 7
#define buttonPin2 3
int state1 = 0; //Integer to hold current state
int state2 = 0;
int old1 = 0;
int old2 = 0;
int buttonPoll1 = 0;
int buttonPoll2 = 0;
void setup(void) {
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}
tft.initR();
tft.setRotation(1);
tft.fillScreen(ST7735_BLACK);
startTime = millis() - INTERVAL;
pinMode(relayEnable, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
long pfix = bmp.readPressure();
}
void loop() {
int BtnRead1 = digitalRead(buttonPin1);
Serial.println(BtnRead1);
delay(50);
int BtnRead2 = digitalRead(buttonPin2);
Serial.println(BtnRead2);
delay(50);
buttonPoll1 = digitalRead(buttonPin1);
buttonPoll1 = digitalRead(buttonPin1);
if (buttonPoll1 != old1) {
if (buttonPoll1 == HIGH) {
state1++;
}
else {
delay(50);
}
}
old1 = buttonPoll1;
if (buttonPoll2 != old2) {
if (buttonPoll2 == HIGH) {
state2++;
}
else {
delay(50);
}
}
old2 = buttonPoll2;
switch (state1) {
case 0: {
tft.fillRect(5, 75, 80, 50, ST7735_BLUE); //prvi pravokutnik je crven
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
int xpos = 5;
int ypos = 7;
tft.drawString("Tlak", xpos, ypos, 2);
long p = bmp.readPressure();
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
char buf[20];
dtostrf((float)p / 100.0, 8, 3, buf);
xpos = 130;
ypos += 25;
tft.drawRightString(buf, xpos, ypos, 4);
tft.drawString(" mb", xpos, ypos, 4);
//digitalWrite(led, HIGH);
tft.fillRect(5, 75, 80, 50, ST7735_BLUE);
state1 = 0;
}
break;
case 1: {
long p1 = bmp.readPressure();
delay (300);
long p2 = bmp.readPressure();
if ((p1 + 100) < p2) {
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
int xpos = 5;
int ypos = 7;
tft.drawString("Tlak", xpos, ypos, 2);
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
char buf[20];
dtostrf((float)p2 / 100.0, 9, 3, buf);
xpos = 130;
ypos += 25;
tft.drawRightString(buf, xpos, ypos, 4);
tft.drawString(" mb", xpos, ypos, 4);
tft.fillRect(5, 75, 80, 50, ST7735_BLACK);
tft.fillRect(85, 75, 80, 50, ST7735_BLUE);
//digitalWrite(led, HIGH);
}
else if (p1 + 100 >= p2) {
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
int xpos = 5;
int ypos = 7;
tft.drawString("Tlak", xpos, ypos, 2);
//long p2 = bmp.readPressure();
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
char buf[20];
dtostrf((float)p2 / 100.0, 9, 3, buf);
xpos = 130;
ypos += 25;
tft.drawRightString(buf, xpos, ypos, 4);
tft.drawString(" mb", xpos, ypos, 4);
tft.fillRect(5, 75, 80, 50, ST7735_BLACK); //prvi pravokutnik postaje crn
tft.fillRect(85, 75, 80, 50, ST7735_GREEN); //drugi pravokutnik postaj zelen
//digitalWrite(led, LOW);
state1 = 1;
}
}
break;
case 2: {
if (state2 != 0) {
long p = bmp.readPressure();
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
int xpos = 5;
int ypos = 7;
tft.drawString("Tlak", xpos, ypos, 2);
tft.setTextColor(ST7735_WHITE, ST7735_BLACK);
char buf[20];
dtostrf((float)p / 100.0, 8, 3, buf);
xpos = 130;
ypos += 25;
tft.drawRightString(buf, xpos, ypos, 4);
tft.drawString(" mb", xpos, ypos, 4);
//digitalWrite(led, HIGH); tu ces ubaciti otvaranje izlaznog ventila.
tft.fillRect(85, 75, 80, 50, ST7735_BLACK);
tft.fillRect(75, 75, 80, 50, ST7735_GREEN);
state1 = 0;
}
}
break;
}
}