YMMV

* your mileage may vary

Sunday, 29 October 2017

Sparlfun 7-Segment Display & DHT11 Sensor

Code to interface Sparkfun 7-Segment display with DHT11.






/* Serial 7-Segment Display Example Code with DHT11
 *  cut and paste from a number of sources
*/
#include <SoftwareSerial.h>
#include <dht.h>
#include <Time.h>
#include <TimeLib.h>

#define DHT11_PIN 8
#define BUTTON_PIN_MIN 2
#define BUTTON_PIN_HOUR 3
#define BUTTON_PIN_SET 4
#define S7S_PIN_RX 7
#define S7S_PIN_TX 9

  SoftwareSerial s7s(S7S_PIN_RX, S7S_PIN_TX);
  dht DHT;
 
  int chk ;
  int tempString ;
  int humidString ;
 
  int buttonStateHour = 0;
  int buttonStateMin = 0;
  int buttonStateSet = 0;
  int newhour=0;
  int newmin = 0;
 
void setup()
{
  pinMode(BUTTON_PIN_SET, INPUT);
  pinMode(BUTTON_PIN_HOUR, INPUT);
  pinMode(BUTTON_PIN_MIN, INPUT);
  s7s.begin(9600);
//  Serial.begin(9600);  //debugging
  clearDisplay(); 
 
  s7s.print("-HI-");  // Displays -HI- on all digits
  setDecimals(0b111111);  // Turn on all decimals, colon, apos
  // Flash brightness values at the beginning
  setBrightness(0);  // Lowest brightness
  delay(1500);
  setBrightness(127);  // Medium brightness
  delay(1500);
  setBrightness(255);  // High brightness
  delay(1500);

  // Clear the display before jumping into loop
  clearDisplay();
  setBrightness(127);  // Medium brightness
}

void checkSetTime() {
// check if the set time button is high 
  buttonStateSet = digitalRead(BUTTON_PIN_SET);
  while(buttonStateSet == HIGH){
// Read button and update the Hours   
    buttonStateHour = digitalRead(BUTTON_PIN_HOUR);
      if (buttonStateHour == HIGH) {
        newhour=newhour+1;
           clearDisplay();   
        if (newhour > 23)
          newhour = 0;
        setTime(newhour,newmin,second(), day(), month(), year());
          printTime();
          delay(500);   
      }
// Read buttotn and update the minutes     
      buttonStateMin = digitalRead(BUTTON_PIN_MIN);
      if (buttonStateMin == HIGH) {
          newmin=newmin+1;
          clearDisplay();
                if (newmin > 59)
          newhour = 0;
          setTime(newhour,newmin,second(), day(), month(), year());
          printTime();
          delay(500);
     }
    buttonStateSet = digitalRead(BUTTON_PIN_SET);
  }
}

void loop()
{
  chk = DHT.read11(DHT11_PIN);
  tempString = DHT.temperature;
  humidString = DHT.humidity;

// Display the Time
  clearDisplay();
  checkSetTime();
  printTime();
  delay(4000);

// Display the Temperature
  clearDisplay();
  s7s.print(tempString);
  s7s.print(" c");
  setDecimals(0b100000);
  delay(1500);

// Display the Humidity
  clearDisplay();
  s7s.print(humidString);
  s7s.print(" -");
  setDecimals(0b101000);
  delay(1500); 
}

// Send the clear display command (0x76)
//  This will clear the display and reset the cursor
void clearDisplay()
{
  s7s.write(0x76);  // Clear display command
}

// Set the displays brightness. Should receive byte with the value
//  to set the brightness to
//  dimmest------------->brightest
//     0--------127--------255
void setBrightness(byte value)
{
  s7s.write(0x7A);  // Set brightness command byte
  s7s.write(value);  // brightness data byte
}

// Turn on any, none, or all of the decimals.
//  The six lowest bits in the decimals parameter sets a decimal
//  (or colon, or apostrophe) on or off. A 1 indicates on, 0 off.
//  [MSB] (X)(X)(Apos)(Colon)(Digit 4)(Digit 3)(Digit2)(Digit1)
void setDecimals(byte decimals)
{
  s7s.write(0x77);
  s7s.write(decimals);
}
void printTime()
{
  if (hour() < 10)
    s7s.print(" ");
    s7s.print(hour());
//    Serial.println(hour());
  if (minute() < 10)
    s7s.print(0);
    s7s.print(minute());
//    Serial.println(minute());
    setDecimals(0b10000);
}

No comments:

Post a Comment