I'm a casual user of arduino and i'm looking for some easy to understand code for a pretty simple problem that comes up allot, using delay() with libraries that use the timer functions. I'm using an ESP board to grab data from remote sensors inside a vacuum chamber, and then work the relay for the pump. The code below gets the psi from the other esp... but i'm wondering if there is a good way to turn on the pump to bring the pressure down to 8 psi, then keep it there for 5 minutes. The relevant code:
if (currentMillis - previousMillis < interval) {
if ((psi >= 8)) && (psi <= 9 {
digitalWrite(relayPin, HIGH);
Serial.println("hello from psi");
} else {
digitalWrite(relayPin, LOW);
}
} else {
pressurized = false;
digitalWrite(relayPin, LOW);
Serial.println("gello");
previousMillis = currentMillis;
x = 1;
}
I would've solved it by just putting in a delay(5000) then test for the psi again. Any suggestions?
/*
* Functions: grabs data for co2, humidity, temperature, and pressure and stores it to the SD card
* grabs pressure reading data and turns on/off a relay
* sets a lower bound for the pressure, turns on pump, operates pump till the desired pressure is achieved, and keeps the pressure low for a specified amount of time.
* ticker one: SD card... grabs data every 5 minutes except when in pressurized mode.
* ticker two: sets the interval for pulling pressure, once an hour!
* millis() sets how long the pressure remains low, 5 minutes.
*/
#include <Ticker.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <SD.h>
// Set WiFi credentials
#define WIFI_SSID "TheOtherESP"
#define WIFI_PASS "flashmeifyoucan"
//SD
File myFile;
String datalabel1 = "Pressure";
String datalabel2 = "CO2";
String datalabel3 = "Humidity";
String datalabel4 = "Temperature";
bool label = true;
const int chipSelect = D8;
// UDP
WiFiUDP UDP;
IPAddress remote_IP(192,168,4,1);
#define UDP_PORT 4210
char packet[255];
//drops the millis to zero on execution of the pressureDrop function, then back to one upon dataDrop
int x = 0;
// pins used
const int relayPin = D4;
// Ticker
Ticker flipper;
bool pressurized = false;
unsigned long currentMillis = millis();
unsigned long previousMillis = 0;
long interval = 10000 + 15000;
//////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(D2, OUTPUT);
Serial.begin(9600);
while (!Serial) {
;
}
//SD card
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
while (1);}
Serial.println("initialization done.");
// Begin WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
WiFi.mode(WIFI_STA);
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
// Loop continuously while WiFi is not connected
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
// Begin UDP port
UDP.begin(UDP_PORT);
Serial.print("Opening UDP port ");
Serial.println(UDP_PORT);
// Ticker
flipper.attach(300, dataDrop);
flipper.attach(15, pressureDrop);
}
//////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
currentMillis = millis();
digitalWrite(D2, LOW);
if (x == 2) {
String myString;
int psi;
int packetSize;
pressurized = true;
UDP.beginPacket(IPAddress(192,168,4,1), 4210);
UDP.write("pressure");
UDP.endPacket();
packetSize = UDP.parsePacket();
myString = String(packet);
psi = (myString.toInt())*.0145;
Serial.println(psi);
Serial.println(currentMillis - previousMillis);
if (currentMillis - previousMillis < interval) {
if ((psi >= 8)) && (psi <= 9 {
digitalWrite(relayPin, HIGH);
Serial.println("hello from psi");
} else {
digitalWrite(relayPin, LOW);
}
} else {
pressurized = false;
digitalWrite(relayPin, LOW);
Serial.println("gello");
previousMillis = currentMillis;
x = 1;
}
}
// If packet received...
int packetSize = UDP.parsePacket();
if (packetSize) {
Serial.print("Received packet! Size: ");
Serial.println(packetSize);
int len = UDP.read(packet, 255);
if (len > 0)
{
packet[len] = '\0';
}
Serial.print("Packet received: ");
Serial.println(packet);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
void dataDrop() {
UDP.beginPacket(IPAddress(192,168,4,1), 4210);
UDP.write("Loser");
UDP.endPacket();
int packetSize = UDP.parsePacket();
myFile = SD.open("data.csv", FILE_WRITE);
if (pressurized == false) {
x = 0;
if (myFile) {
while (label) {
myFile.print(datalabel1);
myFile.print(",");
myFile.print(datalabel2);
myFile.print(",");
myFile.print(datalabel3);
myFile.print(",");
myFile.println(datalabel4);
label=false;
}
myFile.println(packet);
myFile.close();
} else {
Serial.println("error opening test.txt");
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
void pressureDrop() {
currentMillis = previousMillis;
x = 2;
}