I am making a seismic sensor using a geophone and two LoRa modules. When the geophone reads vibration it sends the data from the sensor (geophone reading, GPS location and altitude, battery voltage and percentage, and time). I need both LoRa modules to send and receive data between the two. The sensor is functioning properly and I am trying to add a feature that allows the other LoRa module to control the level of sensitivity by entering a certain phrase in the serial monitor. When I added this feature,the sensor ignores my if statements on which level of sensitivity it should have. I know the fix is probably simple but it's been kicking my butt all day. Any help would be much appreciated.
Sensor Sketch
//Include the needed libraries for the ADS and the GPS module
#include <Wire.h>//I2C library
#include <Adafruit_ADS1X15.h>//ADS library
#include <heltec.h> //Heltec ESP32 LoRa board that is being used as the board and to transmit data
#include "TinyGPS++.h"//Gps module library
//Define the BAND frequency
#define BAND 915E6//set BAND to US which is 915E6 or 915MHz
//Declare your objects which are the GPS module and the ADS1115 in which we can read the geophone inputs
TinyGPSPlus gps;//This is the GPS object that will pretty much do all the grunt work with the NMEA data
Adafruit_ADS1115 ads;/* Use this for the 16-bit version */
//Declare the global variables
int value = 0;
float Voltage;
float Perc;
int16_t SensorRead;
float LatRead;
float LonRead;
float AltRead;
int HourRead;
int MinRead;
int SecRead;
int LEDPin = 12;
String rssi = "RSSI --";
String packSize = "--";
String packet ;
int id = 1;
String deviceType = "GRDSEN1";
String quote = "";
String command;
int readingMax;
int readingMin;
void setup(void)
{
Serial.begin(115200); // use the serial port
Serial2.begin(115200, SERIAL_8N1, 2, 17);
//serial_connection.begin(115200);//This opens up communications to the GPS
Serial.println("Hello!");
Serial.println("Single-ended readings from AIN0 with >3.0V comparator");
Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
Serial.println("Comparator Threshold: 1000 (3.000V)");
// ADS1015 ADS1115
// ------- -------
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
//ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
// ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
// ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
//ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
ads.begin();
ads.setGain(GAIN_FOUR);
if (!ads.begin()) {
Serial.println("Failed to initialize ADS.");
while (1);
}
// Setup 3V comparator on channel 0
ads.startComparator_SingleEnded(0, 1000);
pinMode(LEDPin, OUTPUT);
//WIFI Kit series V1 not support Vext control
Heltec.begin(false /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
}
void loop(void)
{
value = analogRead(A0);
Voltage = value * 5.0 / 1023;
Perc = map(Voltage, 3.6, 4.2, 0, 100);
LatRead = gps.location.lat();
LonRead = gps.location.lng();
AltRead = gps.altitude.feet();
HourRead = gps.time.hour();
MinRead = gps.time.minute();
SecRead = gps.time.second();
command = LoRa.readStringUntil('\n');
SensorRead = ads.getLastConversionResults();
String daTa = String(id) + "," + String(HourRead) + "," + String(MinRead) + "," + String(SecRead) + "," + String(LatRead, 6) + "," + String(LonRead, 6) + "," + String(AltRead) + "," + String(SensorRead) + "," + String(Voltage) + "," + String(Perc);
sendsensitivity();
Serial.print("GEOPHONE: "); Serial.println(SensorRead);
if (SensorRead > readingMax or SensorRead < readingMin )
{
flash();
LoRa.beginPacket();
LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
LoRa.print(daTa);
LoRa.endPacket();
Serial.print(daTa);
}
}
void sendsensitivity()
{
while (LoRa.available()) {
command.trim();
if (command.equals("level:1")) {
readingMax = 150;
readingMin = -150;
}
else if (command.equals("level:2")) {
readingMax = 180;
readingMin = -180;
}
else if (command.equals("level:3")) {
readingMax = 200;
readingMin = -200;
}
else if (command.equals("level:4")) {
readingMax = 500;
readingMin = -500;
}
else if (command.equals("level:5")) {
readingMax = 800;
readingMin = -800;
}
else {
readingMax = 200;
readingMin = -200;
}
}
}
void flash()
{
digitalWrite(LEDPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(LEDPin, LOW); // turn the LED off by making the voltage LOW
delay(100);
}
Hud Sketch
#include "heltec.h"
#define BAND 915E6 //you can set band here directly,e.g. 868E6,915E6
String command;
String rc = "";
void setup() {
//WIFI Kit series V1 not support Vext control
Heltec.begin(true /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
Serial.begin(115200);
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.println("Received packet '");
// read packet
while (LoRa.available()) {
rc = LoRa.readString();
}
}
sendsensitivity();
showData();
}
void sendsensitivity()
{
while (Serial.available()) {
command = Serial.readStringUntil('\n');
command.trim();
if (command.equals("level:1")) {
LoRa.beginPacket();
LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
LoRa.print("level:1");
LoRa.endPacket();
}
else if (command.equals("level:2")) {
LoRa.beginPacket();
LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
LoRa.print("level:2");
LoRa.endPacket();
}
else if (command.equals("level:3")) {
LoRa.beginPacket();
LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
LoRa.print("level:3");
LoRa.endPacket();
}
else if (command.equals("level:4")) {
LoRa.beginPacket();
LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
LoRa.print("level:4");
LoRa.endPacket();
}
else if (command.equals("level:5")) {
LoRa.beginPacket();
LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
LoRa.print("level:5");
LoRa.endPacket();
}
else {
Serial.println("bad command");
}
Serial.print("Command: ");
Serial.println(command);
}
}
void showData() {
Serial.print("Data: "); Serial.println(rc);
}