2015年9月29日 星期二

DS1302_DS18x20_DHT11_2004LCD



溫度,濕度再加上時間,本想再加上SD卡,使它有Datalogger的功能,無耐與I2CLCD的libraries相衝吧?

#include "stdio.h"
#include "DS1302.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "dht.h"

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

LiquidCrystal_I2C lcd(0x3f,20,4);
dht DHT;
#define DHT11_PIN 3

namespace {

const int kCePin   = 5;  // Chip Enable
const int kIoPin   = 6;  // Input/Output
const int kSclkPin = 7;  // Serial Clock

DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}
void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
 }
}  // namespace

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  //Serial.println("Dallas Temperature IC Control Library");

  // Start up the library
  sensors.begin();

  lcd.init();
  lcd.backlight();
  //lcd.print("Room Temperature");
}

void loop(void)
{
  printTime();
  lcd.setCursor(0,0);
  Time t = rtc.time();
  char buf[50];
  snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d",
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);
  //Serial.println(buf);
  lcd.print(buf);
  //Serial.print("Requesting temperatures...");
  //sensors.requestTemperatures();
  //Serial.println("DONE");
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));
  Serial.print("Temperature for the device 2 (index 1) is: ");
  Serial.println(sensors.getTempCByIndex(1));
  lcd.setCursor(0,1);
  lcd.print("DS18S20 A: ");
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("C");
  lcd.setCursor(0,2);
  lcd.print("DS18B20 B: ");
  lcd.print(sensors.getTempCByIndex(1));
  lcd.print("C");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  delay(1000);
  Serial.println(DHT.humidity, 1);
  Serial.println(DHT.temperature, 1);
  lcd.setCursor(0,3);
  lcd.print("DHT11:  ");
  lcd.print(DHT.humidity, 0);
  lcd.print("%  ");
  // lcd.print("Temp: ");
  lcd.print(DHT.temperature,0);
  lcd.print("C");
  delay(1000);
}

ESP8266




2015年9月20日 星期日

SG90+Joystick module

#include "Servo.h"

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

BMP180+DS18x20+DCcduino_LCD2004A

DCcduino  這塊山寨版UNO剛好滿足我不喜歡用麵包板的特性,擴充各兩組5V及3.3V的接點
這次連接I2C LCD 及BMP180和DS18B20,沒有須增加電源的問題。


















#include "SFE_BMP180.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "OneWire.h"
#include "DallasTemperature.h"

SFE_BMP180 pressure;

#define ALTITUDE 1655.0

LiquidCrystal_I2C lcd(0x3f,20,4);

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{
  lcd.init();
  lcd.backlight();
  lcd.print("BMP180 Demo");

  Serial.begin(9600);
  Serial.println("REBOOT");

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {

    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }

sensors.begin();

}



void loop()
{
  char status;
  double T,P,p0,a;

  Serial.println();
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
  Serial.print(ALTITUDE*3.28084,0);
  Serial.println(" feet");

  status = pressure.startTemperature();
  if (status != 0)
  {
   
    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {

      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      Serial.print((9.0/5.0)*T+32.0,2);
      Serial.println(" deg F");
   
      status = pressure.startPressure(3);
      if (status != 0)
      {

        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
     
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*0.0295333727,2);
          Serial.println(" inHg");
     
          p0 = pressure.sealevel(P,ALTITUDE);
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0,2);
          Serial.print(" mb, ");
          Serial.print(p0*0.0295333727,2);
          Serial.println(" inHg");

          a = pressure.altitude(P,p0);
          Serial.print("computed altitude: ");
          Serial.print(a,0);
          Serial.print(" meters, ");
          Serial.print(a*3.28084,0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

 sensors.requestTemperatures();

  lcd.setCursor(0,1);
  lcd.print(T,2);
  lcd.print("C ");
  lcd.print(P,2);
  lcd.print("mb ");
  lcd.setCursor(0,2);
  lcd.print(a,0);
  lcd.print("M ");
  lcd.print(p0,2);
  lcd.print("mb ");
  lcd.setCursor(0,3);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.print("C ");
  lcd.print(sensors.getTempCByIndex(1));
  lcd.print("C ");

  Serial.print("Ds18s20");
  Serial.print(",");
   Serial.print(sensors.getTempCByIndex(0));
  Serial.print(",");
   Serial.print("Ds18b20");
  Serial.print(",");
  Serial.print(sensors.getTempCByIndex(1));
  Serial.println(",");

   
      // Serial.print("temperature: ");
     // Serial.print(T,2);
     // Serial.print(" deg C, ");
     // Serial.print((9.0/5.0)*T+32.0,2);
     // Serial.println(" deg F");

  delay(2000);
}

2015年9月19日 星期六

DHT11_LCD1602_Serial port

#include  "Wire.h"
#include  "LiquidCrystal_I2C.h"
#include  "dht.h"

LiquidCrystal_I2C lcd(0x27,16,2);
dht DHT;

#define DHT11_PIN 5

void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  //lcd.print("DHT11 Test Program ");

  Serial.begin(115200);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
  Serial.print("OK,\t");
  break;
    case DHTLIB_ERROR_CHECKSUM:
  Serial.print("Checksum error,\t");
  break;
    case DHTLIB_ERROR_TIMEOUT:
  Serial.print("Time out error,\t");
  break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default:
  Serial.print("Unknown error,\t");
  break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);
  lcd.setCursor(0,0);
  lcd.print("Humidity:");
  lcd.setCursor(13,0);
  lcd.print(DHT.humidity, 0);
  lcd.setCursor(15,0);
  lcd.print("%");
  lcd.setCursor(0,1);
  lcd.print("Temperature:");
  lcd.setCursor(13,1);
  lcd.print(DHT.temperature,0);
  lcd.setCursor(15,1);
  lcd.print("C");

  delay(2000);
}

2015年9月18日 星期五

BMP180 Demo

#include "SFE_BMP180.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"

SFE_BMP180 pressure;

#define ALTITUDE 1655.0

LiquidCrystal_I2C lcd(0x27,16,2);

void setup()
{
  lcd.init();
  lcd.backlight();
  lcd.print("BMP180 Demo");

  Serial.begin(9600);
  Serial.println("REBOOT");

  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {

    Serial.println("BMP180 init fail\n\n");
    while(1); // Pause forever.
  }
}

void loop()
{
  char status;
  double T,P,p0,a;

  Serial.println();
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
  Serial.print(ALTITUDE*3.28084,0);
  Serial.println(" feet");

  status = pressure.startTemperature();
  if (status != 0)
  {
   
    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {

      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      Serial.print((9.0/5.0)*T+32.0,2);
      Serial.println(" deg F");
   
      status = pressure.startPressure(3);
      if (status != 0)
      {

        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
     
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*0.0295333727,2);
          Serial.println(" inHg");
     
          p0 = pressure.sealevel(P,ALTITUDE);
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0,2);
          Serial.print(" mb, ");
          Serial.print(p0*0.0295333727,2);
          Serial.println(" inHg");

          a = pressure.altitude(P,p0);
          Serial.print("computed altitude: ");
          Serial.print(a,0);
          Serial.print(" meters, ");
          Serial.print(a*3.28084,0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

  lcd.setCursor(0,1);
  lcd.print(T,2);
  lcd.print("C ");
  lcd.print(P,2);
  lcd.print("mb ");
     // Serial.print("temperature: ");
     // Serial.print(T,2);
     // Serial.print(" deg C, ");
     // Serial.print((9.0/5.0)*T+32.0,2);
     // Serial.println(" deg F");

  delay(5000);
}

DHT11_LCD2004A_Serial prot

#include  "Wire.h"
#include "LiquidCrystal_I2C.h"
#include "dht.h"

LiquidCrystal_I2C lcd(0x3f,20,4);
dht DHT;

#define DHT11_PIN 5

void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  lcd.print("DHT11 Test Program ");

  Serial.begin(115200);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT11, \t");
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:
Serial.print("OK,\t");
break;
    case DHTLIB_ERROR_CHECKSUM:
Serial.print("Checksum error,\t");
break;
    case DHTLIB_ERROR_TIMEOUT:
Serial.print("Time out error,\t");
break;
    case DHTLIB_ERROR_CONNECT:
        Serial.print("Connect error,\t");
        break;
    case DHTLIB_ERROR_ACK_L:
        Serial.print("Ack Low error,\t");
        break;
    case DHTLIB_ERROR_ACK_H:
        Serial.print("Ack High error,\t");
        break;
    default:
Serial.print("Unknown error,\t");
break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);
  lcd.setCursor(0,2);
  lcd.print("Humidity:");
  lcd.setCursor(13,2);
  lcd.print(DHT.humidity, 1);
  lcd.setCursor(18,2);
  lcd.print("%");
  lcd.setCursor(0,3);
  lcd.print("Temperature:");
  lcd.setCursor(13,3);
  lcd.print(DHT.temperature, 1);
  lcd.setCursor(18,3);
  lcd.print("C");

  delay(1000);
}

2015年9月16日 星期三

LiquidCrystal_I2C 2004A

  • Arduino +5V --> Module VCC
  • Arduino GND --> Module GND
  • Arduino Analoog 4 --> Module SDA
  • Arduino Analoog 5 --> Module SCL

#include "Wire.h"
#include "LiquidCrystal_I2C.h"


LiquidCrystal_I2C lcd(0x3f,20,4);  // set the LCD address to 0x3f for a 20 chars and 4 line display

void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Hello Arduino");
  lcd.setCursor(0,1);
  lcd.print("DCcEle LCD Print");
  lcd.setCursor(0,2);
  lcd.print("2004A LiquidCrystal I2C");
  lcd.setCursor(0,3);
  lcd.print("Show!");    
}

void loop()
{
}