Skip to content

Instantly share code, notes, and snippets.

@ljmf00
Last active September 16, 2017 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljmf00/6901439b7b1d5e1c3d025c2e3397ab28 to your computer and use it in GitHub Desktop.
Save ljmf00/6901439b7b1d5e1c3d025c2e3397ab28 to your computer and use it in GitHub Desktop.
#include <FastLED.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DDEBUG 1
#define DHT_OK 0
#define DHT_ERROR_CHECKSUM -1
#define DHT_ERROR_TIMEOUT -2
#define LED_PIN 11
#define NUM_LEDS 30
#define BRIGHTNESS 80
#define I2C_ADDRESS_LCD 0x3F
#define LED_TYPE WS2812B
#define COLOR_ORDER RGB
#define DHT11PIN 3
#define UPDATES_PER_SECOND 50
class dht11
{
public:
int read(int pin) {
// BUFFER TO RECEIVE
uint8_t bits[5];
uint8_t cnt = 7;
uint8_t idx = 0;
// EMPTY BUFFER
for (int i=0; i< 5; i++) bits[i] = 0;
// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delay(18);
digitalWrite(pin, HIGH);
delayMicroseconds(40);
pinMode(pin, INPUT);
// ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (int i=0; i<40; i++)
{
loopCnt = 10000;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
unsigned long t = micros();
loopCnt = 10000;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
idx++; // next byte!
}
else cnt--;
}
// WRITE TO RIGHT VARS
// as bits[1] and bits[3] are allways zero they are omitted in formulas.
humidity = bits[0];
temperature = bits[2];
uint8_t sum = bits[0] + bits[2];
if (bits[4] != sum) return DHT_ERROR_CHECKSUM;
return DHT_OK;
}
int humidity;
int temperature;
};
CRGB leds[NUM_LEDS]; // Set Led Strip
LiquidCrystal_I2C lcd(I2C_ADDRESS_LCD,2,1,0,4,5,6,7,3, POSITIVE); // Set the LCD I2C address
dht11 DHT11;
CRGBPalette16 currentPalette;
TBlendType currentBlending;
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
const uint8_t charBitmap[][8] = {
{ 0xc, 0x12, 0x12, 0xc, 0, 0, 0, 0 },
{ 0x6, 0x9, 0x9, 0x6, 0, 0, 0, 0 },
{ 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0, 0x0 },
{ 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0, 0x0 },
{ 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0x0 },
{ 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0x0 },
{ 0x0, 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0x0 },
{ 0x0, 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0x0 }
};
void setup() {
#ifdef DDEBUG
Serial.begin(115200);
#endif
int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));
lcd.begin(16,2);
for ( int i = 0; i < charBitmapSize; i++ )
{
lcd.createChar ( i, (uint8_t *)charBitmap[i] );
}
lcd.home();
lcd.setCursor ( 1, 0 );
lcd.print("ljmf00's Setup");
while(DHT11.read(DHT11PIN) != DHT_OK) {}
lcd.setCursor(0,1);
lcd.print("T:");
lcd.print((float)DHT11.temperature);
lcd.setCursor(8,1);
lcd.print("H:");
lcd.print((float)DHT11.humidity);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
StartFillLEDsFromPaletteColors();
}
double ct_m = 0;
double ch_m = 0;
byte i;
void loop() {
if(i!=10) {
if(DHT11.read(DHT11PIN) == DHT_OK) {
ct_m += (double)DHT11.temperature;
ch_m += (double)DHT11.humidity;
i++;
}
} else {
ct_m /= 10;
ch_m /= 10;
lcd.setCursor(0,1);
lcd.print("T:");
lcd.print(ct_m);
lcd.setCursor(8,1);
lcd.print("H:");
lcd.print(ch_m);
ct_m = 0;
ch_m = 0;
i = 0;
}
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
uint8_t brightness = 255;
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
colorIndex += 3;
}
}
void StartFillLEDsFromPaletteColors()
{
uint8_t brightness = 255;
uint8_t colorIndex = NUM_LEDS*3;
for( int i = NUM_LEDS; i >= 0; i--) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
colorIndex -= 3;
delay(50);
FastLED.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment