Adding a start/stop button to control if sensors are sending data or not

I am designing a product for an A-level Design project and I wanted to try use an Arduino to measure the temperature of water and detect if water is present. Im not sure if I have put this is the right category but i'm not sure what it would go in to.

I'd like add 2 buttons into this circuit I am using , one to start/stop the water sensor , and another to start/stop the temperature sensor + LCD. I want to do this to save power on the Arduino as it will be portable and I want to conserve energy from this and I want the water sensor to not be triggered constantly when it's in water. I would like to be able to press the button to stop the function, leave it and then press it again when I need to start it again.

I'd also like to implement an LED that will turn on when water is present like how the buzzer does

this is my current code :


#include <DallasTemperature.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS); 
LiquidCrystal_I2C lcd(0x27, 16, 2); 
 
DallasTemperature sensors(&oneWire);

const int waterSens = A0; //define water sensor 
int waterVal; // define the water sensor value 
 
void setup(void)
{

  pinMode(11, OUTPUT);
  pinMode(waterSens, INPUT);
  Serial.begin(9600);

  sensors.begin();
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
}
void loop(void)
{ 

  sensors.requestTemperatures(); 
 

  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temperature:");
  lcd.setCursor(0,1);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.setCursor(7,1);
  lcd.print("C");
  lcd.setCursor(9,1);
  lcd.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
  lcd.setCursor(15,1);
  lcd.print("F");
  
     waterVal = analogRead(waterSens) ;

   Serial.println(waterVal) ;

   if (waterVal <=480) {
     noTone(11);

   }
else{
  tone(11,500);
 }
  
  delay(1000);
}

I'm not sure what code I need and I cant find anything online to help me include it in my project.

Write a do-nothing function that tests your button. If the button is (as you want it) then stay in the do-nothing function, otherwise, leave the do-nothing function. Here is one I wrote:

void simulate()
{
  if (SIMULATOR)
  {
    pinMode(A0, INPUT);
    while (!analogRead(A0)); // While A0 is HIGH, do not release from this loop
  }
}

Try using this code.

#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 2

#define WATER_BUTTON_PIN 3        //water button is connected through pin 3
#define TEMPERATURE_BUTTON_PIN 4  //temperature button is connected through pin 4

OneWire oneWire(ONE_WIRE_BUS);
LiquidCrystal_I2C lcd(0x27, 16, 2);

DallasTemperature sensors(&oneWire);

const int waterSens = A0;  //define water sensor
int waterVal;              // define the water sensor value


int waterButtonState;          //defining the water button state
int tempButtonState;           //defining the temperature button state
bool waterSensorIsOff = true;  //creating a flag if the sensor is off. default value is true.
bool tempSensorIsOff = true;

void setup(void) {

  pinMode(11, OUTPUT);
  pinMode(waterSens, INPUT);
  pinMode(WATER_BUTTON_PIN, INPUT_PULLUP);
  pinMode(TEMPERATURE_BUTTON_PIN, INPUT_PULLUP);

  Serial.begin(9600);

  sensors.begin();
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
}
void loop(void) {

  sensors.requestTemperatures();

  waterButtonState = digitalRead(WATER_BUTTON_PIN);
  tempButtonState = digitalRead(TEMPERATURE_BUTTON_PIN);

  //check if the button is pressed
  if (waterButtonState) {
    //now check if the sensor is off then...
    if (waterSensorIsOff) {
      waterVal = analogRead(waterSens);  //do the analogRead Function
      waterSensorIsOff = false;
      Serial.println(waterVal);
      if (waterVal <= 480) {
        noTone(11);
      } else {
        tone(11, 500);
      }
    } else {
      waterSensorIsOff = true;
      Serial.println("WATER SENSOR SUCCESSFULLY OFF!");
    }
  }
  if (waterButtonState) {
    //now check if the sensor is off then...
    if (waterSensorIsOff) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Temperature:");
      lcd.setCursor(0, 1);
      lcd.print(sensors.getTempCByIndex(0));
      lcd.setCursor(7, 1);
      lcd.print("C");
      lcd.setCursor(9, 1);
      lcd.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
      lcd.setCursor(15, 1);
      lcd.print("F");      
      tempSensorIsOff = false;
    } else {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("TEMPERATURE SENSOR");
      lcd.setCursor(0, 1);
      lcd.print("IS OFF");
      tempSensorIsOff = true;
    }
  }







  delay(1000);
}

I have tested the code and the temperature sensor works turning it off and on , but the temperature doesn't constantly change when its turned on and only updates when it's been turned off and on again, is there a way to make it update constantly when its turned on as it was doing it with the old code.

The water sensor just doesn't work at all and I have no way of telling if it's on or off. Would it be possible to add an LED that is on when the sensor is off , and make that same LED go on when water is present.

Im Sorry in my last feedback, it seems that i have an error because i did;nt actually compile it in my IDE but can you please try this code now, and tell if it is working or not.
Here is the code.

#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 2

#define WATER_BUTTON_PIN 3        //water button is connected through pin 3
#define TEMPERATURE_BUTTON_PIN 4  //temperature button is connected through pin 4

OneWire oneWire(ONE_WIRE_BUS);
LiquidCrystal_I2C lcd(0x27, 16, 2);

DallasTemperature sensors(&oneWire);

const int waterSens = A0;  //define water sensor
int waterVal;              // define the water sensor value


int waterButtonState;          //defining the water button state
int tempButtonState;           //defining the temperature button state
bool waterSensorIsOn = true;  //creating a flag if the sensor is off. default value is true.
bool tempSensorIsOn = true;

void setup(void) {

  pinMode(11, OUTPUT);
  pinMode(waterSens, INPUT);
  pinMode(WATER_BUTTON_PIN, INPUT_PULLUP);
  pinMode(TEMPERATURE_BUTTON_PIN, INPUT_PULLUP);

  Serial.begin(9600);

  sensors.begin();
  Serial.begin(9600);
  lcd.begin();
  lcd.backlight();
}
void loop(void) {

  sensors.requestTemperatures();

  waterButtonState = digitalRead(WATER_BUTTON_PIN);                     //check whenever the button is pushed or not
  tempButtonState = digitalRead(TEMPERATURE_BUTTON_PIN);                //check whenever the button is pushed or not

  //check if the button is pressed
  if (waterButtonState) {
    waterSensorIsOn = !waterSensorIsOn;  //basically it inverts to true or false
  }
  if (tempButtonState) {
    tempSensorIsOn = !tempSensorIsOn;  //basically it inverts to true or false
  }
  //now check if the water sensor is off then...
  if (waterSensorIsOn) {
    waterVal = analogRead(waterSens);  //do the analogRead Function
    Serial.println(waterVal);
    if (waterVal <= 480) {
      noTone(11);
    } else {
      tone(11, 500);
    }
  } else {
    Serial.println("WATER SENSOR SUCCESSFULLY OFF!");
  }
  if (tempSensorIsOn) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temperature:");
    lcd.setCursor(0, 1);
    lcd.print(sensors.getTempCByIndex(0));
    lcd.setCursor(7, 1);
    lcd.print("C");
    lcd.setCursor(9, 1);
    lcd.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
    lcd.setCursor(15, 1);
    lcd.print("F");
  } else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("TEMPERATURE SENSOR");
    lcd.setCursor(0, 1);
    lcd.print("IS OFF");
  }

  delay(1000);          //you may consider removing this delay function if you want a precise reading of your button
}
1 Like

The temperature sensor works perfectly now thank you.

Would it be possible to press the button once for the water sensor and it stop the buzzer going off , and then to press it again when I want it to without having to hold it down?

Which water sensor? Are you trying to sense only if water is present or not, or water depth?

its a water level sensor ,

the " Robodo SEN18 Water Level Sensor " is the closest match I can find but I'm using it to just detect water presence.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.