อีกแบบสำหรับการใช้ขาร่วมกัน ที่เป็นทั้ง output และ input ให้กับขา Arduino ที่ใช้งานอยู่แล้วโดยเพิ่ม switch ที่ต้องการ โดยการต่อ pull down ผ่าน R 4.7K เข้ากับขาที่ขับสัญญาณ ไปยัง LCD (DB4-DB7)
ที่มา http://www.74hc.co.uk/lcd-and-button-sharing-io/
ข้างล่างนี้ผมนำ code นี้ไปเพิ่มใส่ กับ code ของเดิมที่ใช้วัดอุณหภูมิโดยมี Thermistor เป็น sensor และกำหนด Hysteresis =2
หรืออาจจะเพิ่ม function สำหรับตั้ง Hysteresis ก็ได้ และมี eeprom เก็บค่าที่เราตั้งอุณหภูมิด้วย
ที่มา http://www.74hc.co.uk/lcd-and-button-sharing-io/
Code Select
#include <LiquidCrystal.h>
// Pin Map, matches Arduino HelloWorld example
const byte lcdEnable = 11
const byte lcdRS = 12;
const byte db4 = 5;
const byte db5 = 4;
const byte db6 = 3;
const byte db7 = 2;
LiquidCrystal lcd(lcdRS, lcdEnable, db4, db5, db6, db7);
void setup(){
lcd.begin(16, 2);
lcd.print("74HC.co.uk");
setBus_Input();
}
void loop(){
if(digitalRead(db4) == LOW){
setBus_Output();
lcd.setCursor(0, 1);
lcd.print("DB4");
setBus_Input();
delay(500);
}
else if(digitalRead(db5) == LOW){
setBus_Output();
lcd.setCursor(0, 1);
lcd.print("DB5");
setBus_Input();
delay(500);
}
else if(digitalRead(db6) == LOW){
setBus_Output();
lcd.setCursor(0, 1);
lcd.print("DB6");
setBus_Input();
delay(500);
}
else if(digitalRead(db7) == LOW){
setBus_Output();
lcd.setCursor(0, 1);
lcd.print("DB7");
setBus_Input();
delay(500);
}
}
// A function to set the pins to input
void setBus_Input(){
pinMode(db4, INPUT_PULLUP);
pinMode(db5, INPUT_PULLUP);
pinMode(db6, INPUT_PULLUP);
pinMode(db7, INPUT_PULLUP);
}
// A function to set the pins to output
void setBus_Output(){
pinMode(db4, OUTPUT);
pinMode(db5, OUTPUT);
pinMode(db6, OUTPUT);
pinMode(db7, OUTPUT);
}
ข้างล่างนี้ผมนำ code นี้ไปเพิ่มใส่ กับ code ของเดิมที่ใช้วัดอุณหภูมิโดยมี Thermistor เป็น sensor และกำหนด Hysteresis =2
หรืออาจจะเพิ่ม function สำหรับตั้ง Hysteresis ก็ได้ และมี eeprom เก็บค่าที่เราตั้งอุณหภูมิด้วย
Code Select
/*
Arduino thermistor example software
Tutorial: http://www.hacktronics.com/Tutorials/arduino-thermistor-tutorial
Copyright (c) 2010 Mark McComb, hacktronics LLC
License: http://www.opensource.org/licenses/mit-license.php (Go crazy)
Modified: Pken 20/03/16
Resistor pull down using 4k7 or 10k work just fine.
*/
#include <LiquidCrystal.h>
#include <math.h>
#include <EEPROM.h>
float settemp; // make a variable called temp
int led =13;
//int sw_up = 7;
//int sw_dn = 6;
int tempPin = A5; // thermistor is at A5
int hys = 2; // Hysteresis = 2 C
/*
See tutorial for Thermistor connections
LCD Connections:
rs (LCD pin 4) to Arduino pin 12
LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
*/
const byte lcdEnable = 11;
const byte lcdRS = 12;
const byte db4 = 5;
const byte db5 = 4;
const byte db6 = 3;
const byte db7 = 2;
LiquidCrystal lcd(lcdRS, lcdEnable, db4, db5, db6, db7);
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(void) {
pinMode (led, OUTPUT);
lcd.begin(16, 2); // rows, columns. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
lcd.setCursor(0,0); // set cursor to column 0, row 0
EEPROM.read (1); // make the eeprom address 1
setBus_Input();
}
double Thermister(int RawADC) {
double Temp;
// See http://en.wikipedia.org/wiki/Thermistor for explanation of formula
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}
void printTemp(void) {
double fTemp;
double temp = Thermister(analogRead(tempPin)); // Read sensor
//lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp:");
lcd.setCursor(5,0);
lcd.print(temp);
lcd.print("C");
setBus_Input();
settemp = EEPROM.read(1);
delay (250);
if (digitalRead(db4)== LOW )
{
setBus_Output();
settemp += 1;
if( settemp > 45 )
settemp = 45;
delay(200);
EEPROM.write (1,settemp);
}
else{ // other wise do nothing
}
if (digitalRead (db5) == LOW) // if we detect a 1 on the other switch pin
{
setBus_Output();
settemp -= 1;
if( settemp < 10 )
settemp = 10;
delay(200);
EEPROM.write (1,settemp);
}
else {
}
if (temp > settemp) // if the temperature exceeds your chosen settemp
digitalWrite (led, HIGH); // turn on the led
{
if (temp < (settemp - hys)) // settemp - hysteresis
digitalWrite (led, LOW); // turn off the led
}
lcd.setCursor (0,1);
lcd.print ("Set: ");
lcd.setCursor (5,1);
lcd.print (settemp);
lcd.print ('C');
}
void loop(void) {
printTemp();
delay(200);
}
// A function to set the pins to input
void setBus_Input(){
pinMode(db4, INPUT_PULLUP);
pinMode(db5, INPUT_PULLUP);
pinMode(db6, INPUT_PULLUP);
pinMode(db7, INPUT_PULLUP);
}
// A function to set the pins to output
void setBus_Output(){
pinMode(db4, OUTPUT);
pinMode(db5, OUTPUT);
pinMode(db6, OUTPUT);
pinMode(db7, OUTPUT);
}