Button to change float?

Im lost, my code runs fine but i want to be able to change the dew point with the push of a button.

ive tried eveything i can think of but im lost.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

#define DHTPIN 11  // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11  // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)


// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

//DHT dht(DHTPIN, DHTTYPE, 30);

int intakefan = 2;  //  Intake fan for Humid box and dry box
int humidv = 3;     //  Valve for humud intake
int dryv = 4;       // dry box valve
int exfan = 5;      // exhaust fan
int exv = 6;        // exhaust valve
int returnfan = 7;  // fan to bring air from inside box
int returnv = 8;    //  return valve, air from inside box
//int lock = 9;       // Electric lock
const int buttonPin = 10;  // button to reset cycle
int fridge = 13;           // relay to fridge 240v

int DHTpower = 12;  // constant 5v to DHT11

int buttonState = 0;  // variable for reading the pushbutton

float lowDry, highDry;
float lowStore, highStore;
//bool cycle;

LiquidCrystal_I2C lcd(0x27, 16, 2);  // Set the LCD address to 0x27 for a 16x2 display


double dewPoint(double celsius, double humidity) {
  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO))) - 1);
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1);
  RHS += log10(1013.246);

  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;

  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP / 0.61078);  // temp var
  return (241.88 * T) / (17.558 - T);
}

void setup() {
  pinMode(intakefan, OUTPUT);
  pinMode(fridge, OUTPUT);
  pinMode(humidv, OUTPUT);
  pinMode(dryv, OUTPUT);
  pinMode(exfan, OUTPUT);
  pinMode(exv, OUTPUT);
  pinMode(returnfan, OUTPUT);
  pinMode(returnv, OUTPUT);
  pinMode(DHTpower, OUTPUT);
  pinMode(buttonPin, INPUT);

  digitalWrite(intakefan, LOW);
  digitalWrite(humidv, LOW);
  digitalWrite(dryv, LOW);
  digitalWrite(exfan, LOW);
  digitalWrite(exv, LOW);
  digitalWrite(returnfan, LOW);
  digitalWrite(returnv, LOW);
  digitalWrite(DHTpower, HIGH);
  digitalWrite(fridge, LOW);

  Serial.begin(9600);
  lcd.init();       // Initialize the LCD
  lcd.backlight();  // Turn on the backlight
  lcd.clear();      // Clear the LCD screen
  lcd.setCursor(3, 0);
  lcd.print("The Green");
  lcd.setCursor(4, 1);
  lcd.print("Ninja");
  delay(2000);  // Display the startup message for 2 seconds
  lcd.clear();
  dht.begin();

  lowStore = 10.5;
  highStore = 11.5;  // aim for 11

  lowDry = 11.5;  // Dry aim for 12
  highDry = 12.5;
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  float currentDew = dewPoint(t, h);  // Dew Point for setting pins high


  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    lcd.clear();
    lcd.print("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);
  float hiDegC = dht.convertFtoC(hi);

  if (f < 19) {
    digitalWrite(fridge, LOW);
  }

  if (f > 21) {
    digitalWrite(fridge, HIGH);
  }


  if (currentDew > lowDry && (currentDew < highDry)) {  // Level to aim for dew point
    dewPointOk(t, h);
  }
  if (currentDew >= highDry) {  //   High dew point, send to dry box
    dewPointHigh(t, h);
  }
  if (currentDew <= lowDry) {  // Dew point low, send to humid box
    dewPointLow(t, h);
  }
}


void dewPointOk(float t, float h) {
  digitalWrite(intakefan, LOW);
  digitalWrite(humidv, LOW);
  digitalWrite(dryv, LOW);
  digitalWrite(exfan, LOW);
  digitalWrite(exv, LOW);
  digitalWrite(returnfan, LOW);
  digitalWrite(returnv, LOW);
  lcd.print("Dew point OK");
  lcd.setCursor(0, 1);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Temperature");
  lcd.setCursor(0, 1);
  lcd.print(dht.readTemperature());
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Humidity");
  lcd.setCursor(0, 1);
  lcd.print(dht.readHumidity());
  lcd.setCursor(6, 1);
  lcd.print("%");
  delay(2000);
}

void dewPointHigh(float t, float h) {
  digitalWrite(humidv, LOW);
  digitalWrite(dryv, HIGH);
  digitalWrite(exv, HIGH);
  digitalWrite(returnv, HIGH);
  lcd.clear();
  lcd.print("Dew Point High");
  lcd.setCursor(0, 1);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Temperature");
  lcd.setCursor(0, 1);
  lcd.print(dht.readTemperature());
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Dry Box On");
  lcd.setCursor(0, 1);
  lcd.print("Fans On");
  digitalWrite(intakefan, HIGH);
  digitalWrite(exfan, HIGH);
  digitalWrite(returnfan, HIGH);
  delay(2000);
  lcd.clear();
  lcd.print("Humidity");
  lcd.setCursor(0, 1);
  lcd.print(dht.readHumidity());
  lcd.setCursor(6, 1);
  lcd.print("%");
  delay(2000);
}

void dewPointLow(float t, float h) {
  digitalWrite(humidv, HIGH);
  digitalWrite(dryv, LOW);
  digitalWrite(exv, HIGH);
  digitalWrite(returnfan, LOW);
  digitalWrite(returnv, LOW);
  lcd.print("Dew point LOW");
  lcd.setCursor(0, 1);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  digitalWrite(intakefan, HIGH);
  digitalWrite(exfan, HIGH);
  lcd.clear();
  lcd.print("Humid Box On");
  lcd.setCursor(0, 1);
  lcd.print("Fans On");
  delay(2000);
  lcd.clear();
  lcd.print("Dew Point Low");
  lcd.setCursor(0, 1);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Temperature");
  lcd.setCursor(0, 1);
  lcd.print(dht.readTemperature());
  delay(2000);
  lcd.clear();
  lcd.print("Humidity");
  lcd.setCursor(0, 1);
  lcd.print(dht.readHumidity());
  lcd.setCursor(6, 1);
  lcd.print("%");
  lcd.clear();
}

I want to be able to change this;


  if (currentDew > lowDry && (currentDew < highDry)) {  // Level to aim for dew point
    dewPointOk(t, h);
  }
  if (currentDew >= highDry) {  //   High dew point, send to dry box
    dewPointHigh(t, h);
  }
  if (currentDew <= lowDry) {  // Dew point low, send to humid box
    dewPointLow(t, h);
  }
}

with the push of a button be able to swap the lowDry and highDry to lowCure and highCure.

Can i do this??

Use another set of variables lowValue and highValue in your comparisons, and change these values when you press the button, for example

// at the top of the sketch
float lowValue = lowDry;
float highValue = highDry;

...

// switch between dry and cure values when the button is pressed
if ( buttonPressed() )
{
	static bool toggle = true;
	toggle = !toggle;
	
	if ( toggle == true )
	{
		lowValue = lowDry;
		highValue = highDry;
	}
	else
	{
		lowValue = lowCure;
		highValue = highCure;
	}
}

...

if (currentDew > lowValue && (currentDew < highValue )) {  // Level to aim for dew point
  dewPointOk(t, h);
}

But first you have to rewrite your entire code to make it non blocking, all those delay() will make the code irresponsive to your action on the button, unless you use an interrupt but I don't recommend it.

2 Likes
  • you would have to hold the button down until you're long functions with delays (e.g. dewPointHigh()) complete and execution within loop() continues which includes checking the buttons

  • i posted code in the other thread on how to update the display each iteration of loop() without blocking

  • instead of have set-points for lowDry and highDry, why not just a set-point for target dew-point and test for +/- 0.5 ... also shown in what i posted in the other thread

it's seems common for OPs with poorly structured code to think that just a few simple changes can make it work when it's require far more changes

1 Like

Thats what i was looking for, thank you

Could I just use a latching switch to keep it closed?

I will check out your thread.

Skill level is the main reason for just having one set point. The code just kept getting more complex. Once ive got the button sorted im going to screw the lid shut on this box.

yes, using a toggle switch sounds like a good option

What am i doing wrong?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

#define DHTPIN 11  // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11  // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)


// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

//DHT dht(DHTPIN, DHTTYPE, 30);

int intakefan = 2;  //  Intake fan for Humid box and dry box
int humidv = 3;     //  Valve for humud intake
int dryv = 4;       // dry box valve
int exfan = 5;      // exhaust fan
int exv = 6;        // exhaust valve
int returnfan = 7;  // fan to bring air from inside box
int returnv = 8;    //  return valve, air from inside box
//int lock = 9;       // Electric lock
const int buttonPin = 10;  // button to reset cycle
int fridge = 13;           // relay to fridge 240v

int DHTpower = 12;  // constant 5v to DHT11

int buttonState = 0;  // variable for reading the pushbutton

float lowDry, highDry;
float lowCure, highCure;
//bool cycle;

LiquidCrystal_I2C lcd(0x27, 16, 2);  // Set the LCD address to 0x27 for a 16x2 display


double dewPoint(double celsius, double humidity) {
  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO))) - 1);
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1);
  RHS += log10(1013.246);

  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;

  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP / 0.61078);  // temp var
  return (241.88 * T) / (17.558 - T);
}

void setup() {
  pinMode(intakefan, OUTPUT);
  pinMode(fridge, OUTPUT);
  pinMode(humidv, OUTPUT);
  pinMode(dryv, OUTPUT);
  pinMode(exfan, OUTPUT);
  pinMode(exv, OUTPUT);
  pinMode(returnfan, OUTPUT);
  pinMode(returnv, OUTPUT);
  pinMode(DHTpower, OUTPUT);
  pinMode(buttonPin, INPUT);

  digitalWrite(intakefan, LOW);
  digitalWrite(humidv, LOW);
  digitalWrite(dryv, LOW);
  digitalWrite(exfan, LOW);
  digitalWrite(exv, LOW);
  digitalWrite(returnfan, LOW);
  digitalWrite(returnv, LOW);
  digitalWrite(DHTpower, HIGH);
  digitalWrite(fridge, LOW);

  Serial.begin(9600);
  lcd.init();       // Initialize the LCD
  lcd.backlight();  // Turn on the backlight
  lcd.clear();      // Clear the LCD screen
  lcd.setCursor(3, 0);
  lcd.print("The Green");
  lcd.setCursor(4, 1);
  lcd.print("Ninja");
  delay(2000);  // Display the startup message for 2 seconds
  lcd.clear();
  dht.begin();

  lowCure = 10.5;
  highCure = 11.5;  // aim for 11

  lowDry = 11.5;  // Dry aim for 12
  highDry = 12.5;
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  float currentDew = dewPoint(t, h);  // Dew Point for setting pins high


  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    lcd.clear();
    lcd.print("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);
  float hiDegC = dht.convertFtoC(hi);

  if (buttonPin == LOW); {
    static bool toggle = true;
    toggle = !toggle;

    if (toggle == true) {
      lowCure = lowDry;
      highCure = highDry;
      lcd.clear();
      lcd.print("Cure");
      delay(2000);
    } else {
      lowDry = lowCure;
      highDry = highCure;
      lcd.clear();
      lcd.print("Cure");
      delay(2000);
    }
  }


  if (f < 19) {
    digitalWrite(fridge, LOW);
  }

  if (f > 21) {
    digitalWrite(fridge, HIGH);
  }


  if (currentDew > lowDry && (currentDew < highDry)) {  // Level to aim for dew point
    dewPointOk(t, h);
  }
  if (currentDew >= highDry) {  //   High dew point, send to dry box
    dewPointHigh(t, h);
  }
  if (currentDew <= lowDry) {  // Dew point low, send to humid box
    dewPointLow(t, h);
  }
}


void dewPointOk(float t, float h) {
  digitalWrite(intakefan, LOW);
  digitalWrite(humidv, LOW);
  digitalWrite(dryv, LOW);
  digitalWrite(exfan, LOW);
  digitalWrite(exv, LOW);
  digitalWrite(returnfan, LOW);
  digitalWrite(returnv, LOW);
  lcd.print("Dew point OK");
  lcd.setCursor(0, 1);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Temperature");
  lcd.setCursor(0, 1);
  lcd.print(dht.readTemperature());
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Humidity");
  lcd.setCursor(0, 1);
  lcd.print(dht.readHumidity());
  lcd.setCursor(6, 1);
  lcd.print("%");
  delay(2000);
}

void dewPointHigh(float t, float h) {
  digitalWrite(humidv, LOW);
  digitalWrite(dryv, HIGH);
  digitalWrite(exv, HIGH);
  digitalWrite(returnv, HIGH);
  lcd.clear();
  lcd.print("Dew Point High");
  lcd.setCursor(0, 1);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Temperature");
  lcd.setCursor(0, 1);
  lcd.print(dht.readTemperature());
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Dry Box On");
  lcd.setCursor(0, 1);
  lcd.print("Fans On");
  digitalWrite(intakefan, HIGH);
  digitalWrite(exfan, HIGH);
  digitalWrite(returnfan, HIGH);
  delay(2000);
  lcd.clear();
  lcd.print("Humidity");
  lcd.setCursor(0, 1);
  lcd.print(dht.readHumidity());
  lcd.setCursor(6, 1);
  lcd.print("%");
  delay(2000);
}

void dewPointLow(float t, float h) {
  digitalWrite(humidv, HIGH);
  digitalWrite(dryv, LOW);
  digitalWrite(exv, HIGH);
  digitalWrite(returnfan, LOW);
  digitalWrite(returnv, LOW);
  lcd.print("Dew point LOW");
  lcd.setCursor(0, 1);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  digitalWrite(intakefan, HIGH);
  digitalWrite(exfan, HIGH);
  lcd.clear();
  lcd.print("Humid Box On");
  lcd.setCursor(0, 1);
  lcd.print("Fans On");
  delay(2000);
  lcd.clear();
  lcd.print("Dew Point Low");
  lcd.setCursor(0, 1);
  lcd.print(dewPoint(t, h));
  lcd.setCursor(6, 1);
  lcd.print("c");
  delay(2000);
  lcd.clear();
  lcd.print("Temperature");
  lcd.setCursor(0, 1);
  lcd.print(dht.readTemperature());
  delay(2000);
  lcd.clear();
  lcd.print("Humidity");
  lcd.setCursor(0, 1);
  lcd.print(dht.readHumidity());
  lcd.setCursor(6, 1);
  lcd.print("%");
  lcd.clear();
}

Cant seem to make the button do anything?

shouldn't this be setting low/highDry to low/highStore?

Shouldn't you be doing a digitalRead(buttonPin)?
Shouldn't you get rid of the ; in that line?

Maybe, ive had so many changes im lost, might need to look at it later.

Yea i think you are right, been looking at this too long now.

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