Press Button as a Switch

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
stevenblanc
 
Posts: 20
Joined: Fri Nov 29, 2013 3:48 pm

Press Button as a Switch

Post by stevenblanc »

Hi folks,

First post here. Just got a starter kit yesterday and a DHT22 and I'm now fiddling around with the tutorials and trying to make them my own.

I followed the wifi weather station tutorial but I'm sending the output to an LCD instead of to a server (I think I will tackle that whole webserver setup next). I got that working just fine, but I decided I would like to have a button which when pressed would switch the temperature printout from Celsius (default) to Farenheit.

I went back to the drawing board and got a push button to act as a switch, turning two LEDs on and off which works well, but, when I try to incorporate the same technique into the weather station it only seems to work while I hold the pushbutton down. Immediately reverting to Celsius on release.

I've been fighting the code for two hours now, just hoping a fresh set of eyes can pick up the issue. I'd really appreciate the help.

Here's the code:

Code: Select all

#include <LiquidCrystal.h>
#include "DHT.h"
LiquidCrystal lcd(12,11,5,4,3,2);

// DHT22 sensor pins
#define DHTPIN 7		// DHT 
#define DHTTYPE DHT22

// Create DHT instance
DHT dht(DHTPIN, DHTTYPE);

int CSS = LOW;         // Current Switch State
int PSS = LOW;         // Previous Switch State
String tempUnit = "C"; // Temperature Unit

long time = 0;
long debounce = 200;

void setup(){  
  pinMode(6,INPUT);

  // Initialize DHT sensor
  dht.begin();
  lcd.begin(16,2);
}

void loop() {
  CSS = digitalRead(6);

  // Measure the humidity & temperature
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // Swap temperature units on button press
  if(CSS != PSS && millis() - time > debounce ) {
    if(tempUnit == "C") {
      tempUnit = "F";
      t = dht.readTemperature();
      t = t * 9 / 5 + 32;          // Adjust calculation for Farenheit
    }
    else {
      tempUnit = "C";
      t = dht.readTemperature();
    }
    time = millis();
  }

  // Transform to String
  String temperature = String((int) t);
  String humidity = String((int) h);

  // Print data to lcd
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print(" ");
  lcd.print(tempUnit);

  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  lcd.print(humidity);
  lcd.print("%");

  PSS = CSS;
}
Thanks in advance! Merry Christmas!

Cheers,


Steven

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Press Button as a Switch

Post by adafruit_support_bill »

This line should be inside your debounce conditional logic.

Code: Select all

  PSS = CSS;

User avatar
stevenblanc
 
Posts: 20
Joined: Fri Nov 29, 2013 3:48 pm

Re: Press Button as a Switch

Post by stevenblanc »

Hey thanks for the reply.

I suppose that reduces the number of times it triggers.

I moved it as you suggested:

Code: Select all

  // Swap temperature units on button press
  if(CSS != PSS && millis() - time > debounce ) {
    if(tempUnit == "C") {
      tempUnit = "F";
      t = dht.readTemperature();
      t = t * 9 / 5 + 32;          // Adjust calculation for Farenheit
    }
    else {
      tempUnit = "C";
      t = dht.readTemperature();
    }
    time = millis();
 [b]   PSS = CSS;[/b]
  }
Is that what you meant?

Doesn't make any difference to the function as far as I can see. And logically the only change I can identify is that it will only process the line of code after a change, rather than at the end of every loop.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Press Button as a Switch

Post by adafruit_support_bill »

And logically the only change I can identify is that it will only process the line of code after a change, rather than at the end of every loop.
It may not be the only issue, but logically, you don't want to count every "bounce" as a state change.

User avatar
stevenblanc
 
Posts: 20
Joined: Fri Nov 29, 2013 3:48 pm

Re: Press Button as a Switch

Post by stevenblanc »

Fair enough. I suppose in larger projects, efficiency can be critical, so I might as well practice good habits.

Does anything else seem to stand out to account for the issue I'm facing?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Press Button as a Switch

Post by adafruit_support_bill »

Code: Select all

  // Swap temperature units on button press
  if(CSS != PSS && millis() - time > debounce ) 
  {
The code does not match the comment. That code will swap the temperature units on every (debounced) state change.
You need to further qualify it to only change on the pressed state.

User avatar
stevenblanc
 
Posts: 20
Joined: Fri Nov 29, 2013 3:48 pm

Re: Press Button as a Switch

Post by stevenblanc »

Ah.... so it is working effectively... but it's changing once when it goes from low to high and then swaps back from high to low.

I was thinking of it as one press... but its actually two steps. Perfect.

I'll just confirm when I sort it out!

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Press Button as a Switch

Post by adafruit_support_bill »

I went back to look at the sample code from arduino on this, and theirs does not seem to need this qualifier to work:

Code: Select all

reading == HIGH && previous == LOW
is not the same as:

Code: Select all

if(CSS != PSS
The first case is only true on a transition to HIGH. The second one is true on any transition.

User avatar
stevenblanc
 
Posts: 20
Joined: Fri Nov 29, 2013 3:48 pm

Re: Press Button as a Switch

Post by stevenblanc »

Got it! Final Code:

Code: Select all

#include <LiquidCrystal.h>
#include "DHT.h"
LiquidCrystal lcd(12,11,5,4,3,2);

// DHT22 sensor pins
#define DHTPIN 7		// DHT 
#define DHTTYPE DHT22

// Create DHT instance
DHT dht(DHTPIN, DHTTYPE);

int CSS = LOW;         // Current Switch State
int PSS = LOW;         // Previous Switch State
String tempUnit = "C"; // Temperature Unit

long time = 0;
long debounce = 200;

void setup(){  
  pinMode(6,INPUT);

  // Initialize DHT sensor
  dht.begin();
  lcd.begin(16,2);

  Serial.begin(9600);
}

void loop() {
  CSS = digitalRead(6);

  // Measure the humidity & temperature
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // Swap temperature units on button release
  if(CSS == HIGH && PSS == LOW && millis() - time > debounce ) {
    if(tempUnit == "C") {
      tempUnit = "F";
    }
    else {
      tempUnit = "C";
    }
    time = millis();
    //PSS = CSS;
  }

  // Adjust calculation for Farenheit
  if(tempUnit == "F") {
    t = t * 9 / 5 + 32;
  }
  
  // Transform to String
  String temperature = String((int) t);
  String humidity = String((int) h);

  //Print data to serial
  Serial.print("Temp: ");
  Serial.print(temperature);
  Serial.print(tempUnit);
  Serial.print("  ");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print("%");
  Serial.print('\n');

  // Print data to lcd
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print(" ");
  lcd.print(tempUnit);

  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  lcd.print(humidity);
  lcd.print("%");

  PSS = CSS;
}
Thanks for the help Bill!

Cheers!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”