Reading 2 values from a HC module

Hello,

I am building a water tank level sensor based on: Wireless Water-Tank Level Meter with Alarm | Arduino Project Hub .

I simplified the project slightly and added a second sensor as I have 2 tanks. I managed to get the transmitter to send 2 values, one for each tank. to distinguish the 2 values I programmed the transmitter to send the value for Tank2 as negative (timed it by -1).

The code below is for the receiver. The result should be displayed on the LCD screen

TANK 1 - xx%

TANK 2 - xx%

For some reason I am getting the 2 numbers flashing on the TANK 1 line. One number is positive and the other negative.

I am using an arduino UNO R3 for the project. The code worked well with one sensor.

Can anyone help?

/

/* Reciever for water tank *//

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
SoftwareSerial HC12(7, 8);
LiquidCrystal  lcd{12, 11, 2, 3, 4, 5};

int data = 0;
int TANK1 = 0;
int TANK2 = 0;
int contrast = A4;
int backlight  = A5;
int ground = 13;

void setup() {
lcd.begin(16,2);
HC12.begin(9600);
pinMode(contrast,  OUTPUT);
pinMode(ground, OUTPUT);
pinMode(backlight, OUTPUT);

analogWrite(contrast,10);

digitalWrite(ground, 0);

// Boot-up  message //
lcd.setCursor(0,0);
lcd.print("  WELCOME     ");
lcd.setCursor(0,1);
lcd.print("     -Nir-");

}

void loop() {

digitalWrite(backlight,  1);
while(HC12.available ()){
lcd.clear();
data = HC12.read();
if (data < 0) {data = data * -1;
TANK2 = map(data, 20, 175, 100, 0);}
else {TANK1 = map(data, 20, 175, 100, 0);}
lcd.setCursor(0,0);
lcd.print("TANK 1-");
lcd.print(TANK1);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print("TANK 2-");
lcd.print(TANK2);
lcd.print("%");
}

delay(500);

}

What is the last character of the first line of your code as posted ?

That's not going to work for starters. You can't use PWM on A4 on an Uno R3.

Please tell me you're not using an Arduino output to power something more than a LED with a series resistor...

No, you definitely are not getting that.

Not with the code you posted. It doesn't even compile.

arduino-cli compile -b arduino:avr:uno --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:1:31: error: expected unqualified-id before '/' token
 /* Reciever for water tank */ /
                               ^
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:6:1: error: 'LiquidCrystal' does not name a type; did you mean 'LiquidCrystal_h'?
 LiquidCrystal lcd{ 12, 11, 2, 3, 4, 5 };
 ^~~~~~~~~~~~~
 LiquidCrystal_h
/home/me/Documents/sketchbook/Uno_R3/test/test.ino: In function 'void setup()':
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:16:4: error: 'lcd' was not declared in this scope
    lcd.begin(16, 2);
    ^~~
/home/me/Documents/sketchbook/Uno_R3/test/test.ino: In function 'void loop()':
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:37:7: error: 'lcd' was not declared in this scope
       lcd.clear();
       ^~~
Used library   Version Path
LiquidCrystal  1.0.7   /home/me/Documents/sketchbook/libraries/LiquidCrystal
SoftwareSerial 1.0     /home/me/.arduino15/packages/arduino/hardware/avr/1.8.3/libraries/SoftwareSerial
Used platform Version Path
arduino:avr   1.8.3   /home/me/.arduino15/packages/arduino/hardware/avr/1.8.3
Error during build: exit status 1
Compilation failed.

Are you playing games?

The unchanging pieces of these display commands are best done once in setup().

It would be helpful to post the sender code.

What testing have you done to confirm that the sender is indeed sending both positive and negative values according to the tank sensor sensor?

Remove the stray '/' to get the code to compile correctly

/* Reciever for water tank *//

Thanks,

As you probably guessed, I am not a very experienced programmer. I used the code from the watertank project as a basis for my project. I did think that it was strange the the ground was allocated a pin... especially since nothing is connected to that pin in the actual board.

The code compiled and it worked with one sensor (taking out the if / else statement and just referring to TANK1.

Think about what happens when data is greater than 175.

If this is someone else's code, I suggest chucking it.

Thanks,

Below is the sender code. I am using a Atmel Atmega328, a HC module and 2 sonic sensors.

I tested it first with the serial monitor and I got correct readings one positive and one negative. The receiver unit is receiving a negative and positive value as they are being displayed on the screen. The only thing is that the number for TANK2 which is the neagative one is still showing negative and it is displayed in the TANK 1 line. The 2 values are alternating.

//* Water tank sensor transmitter*//

#include <SoftwareSerial.h>
SoftwareSerial HC12(4, 5);

int POWER = 6;
long dist_1 = 0;
long dist_2a = 0;
long dist_2 = 0;
int TRIG1 = 2;
int ECHO1 = 3;
int TRIG2 = 8;
int ECHO2 = 7;

void setup() {
HC12.begin(9600);
Serial.begin(9600);
pinMode(TRIG1, OUTPUT);
digitalWrite(TRIG1, LOW);
pinMode(ECHO1, INPUT);
pinMode(POWER, OUTPUT);
digitalWrite(POWER , 1);
pinMode(TRIG2, OUTPUT);
digitalWrite(TRIG2, LOW);
pinMode(ECHO2, INPUT);
}

void loop() {

digitalWrite(TRIG1, LOW);
delayMicroseconds(5);
digitalWrite(TRIG1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG1, LOW);
dist_1 = (pulseIn(ECHO1, HIGH))*0.034/2;
HC12.write(dist_1);
delay(1000);

digitalWrite(TRIG2, LOW);
delayMicroseconds(5);
digitalWrite(TRIG2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG2, LOW);

dist_2 = (pulseIn(ECHO2, HIGH))*0.034/2;
dist_2a = dist_2 * -1;
HC12.write(dist_2a);
delay(1000);

}

It shouldn't be more than 175, as it would reach the tank's overflow. That's the maximum level for the tank. 20 is the minimum.

Then think about this one:

TANK1 = map(data, 20, 175, 100, 0);

It's one or the other. Whether you think it should or shouldn't be, it is.

Good luck.

It looks as if the if/else statement is not doing what I would like it to do.

The narrative is: if the number in data is negative then time it by -1 (to make it positive again) and use it to with the map function in TANK2. Otherwise use data with the map function in TANK1.

What is happening is that data is used in the map function in TANK1 for both the negative and positive number. The if statement is ignored.

The HC12.write() statement is only sending the lowest 8 bits of long value returned by pulseIn(). There is no sign information in the data you are sending with

dist_2 = (pulseIn(ECHO2, HIGH))*0.034/2;
dist_2a = dist_2 * -1;
HC12.write(dist_2a);

I think you will find easier going if you change the the structure of the program to send ascii data with .print() statements and follow the guidelines of the Serial Input Basics tutorial on how to read the multi digit numbers. You can then look for the char of the negative sign '-' or possibly change the transmission to have a tank indicator as well as the sign of the data.

Thanks,

I'll do some research and try a different approach. I might return with more questions.

Thanks for the help.

Nir