Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15598

Button to change float?

$
0
0

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??

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 15598

Latest Images

Trending Articles



Latest Images