Hello,
I’ve been looking online for awhile now, and I just can’t seem to figure out how to set a function for the ‘select’ button on the 2x16 LCD Shield (connected to an Arduino Uno). I have tried a number of codes from similar projects, altering them to suit mine, but it doesn’t do what I tell it to do. *Note I have also attempted doing one myself.
My end goal is for the user to press ‘select’ (on the LCD) which will allow the ultrasonic sensor (US) to measure the distance from its placement on the wall (e.g 40cm, above the bathtub’s top edge) to the bottom of the empty bathtub. The code will then take away that 40cm + another 10cm (as an overflow barrier) equalling 50cm in total, revealing the maximum depth (for example 60cm- which equals the distance from the bottom of the bathtub to 10cm from the top). The arduino now knowing the distance, will minus the height/depth the user sets for their water height (e.g 35cm) from the maximum depth measurement (60cm).
The simple calculation would be something like this 60cm (maximum depth) - 35cm (distance set by user) = 25cm (which is the gap, between the water’s surface/level (35cm) and the 10cm mark before the top).
The code below shows some of my work, however I’d need some help altering the code to fit want I stated above.
Code:
// include the library code:
#include <LiquidCrystal.h>
#define echoPin 1
#define trigPin 2
#define solenoidPin 3
#define buttonsPin A0
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 9, d4 = 4 , d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(3, OUTPUT); // connected to S terminal of Relay
pinMode(A0, INPUT);
}
void loop() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor (1, 1); // put your setup code here, to run once:
lcd.setCursor (0, 0);
// Print a message to the LCD.
delay (250);
lcd.setCursor (0, 0);
lcd.print("Maximum Distance:");
lcd.setCursor(1, 1);
lcd.print("(Hold Select)");
delay (2000);
lcd.clear ();
lcd.setCursor(1, 0);
lcd.print("Wait till the");
lcd.setCursor (1, 1);
lcd.print("screen clears");
delay (1200);
int x;
x = analogRead (0);
lcd.setCursor(10, 1);
if (x < 800) {
lcd.clear ();
delay (10000);
Serial.println(x, DEC);
}
// Ultrasonic sensor function
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;
}
or this code which is used to set a minimum height (50) before the solenoid/relay shuts off.
#define echoPin 1
#define trigPin 2
#define solenoidPin 3
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(3, OUTPUT); // connected to S terminal of Relay
}
void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;
if (distance >= 550 || distance <= 50){
Serial.print("Distance = ");
Serial.println("Off");
digitalWrite(solenoidPin, LOW); // turn relay off
}
else {
Serial.print("Solenoid On Distance = ");
Serial.print(distance);
Serial.println(" cm");;
digitalWrite(solenoidPin, HIGH); // turn relay on
}
delay(500);