Hello.. My project has two parts:
Part 1: A pin gets high or low depending on certain conditions
Part 2: LCD display and DS18B20 as a digital thermometer
I have successfully run theses two codes separately; but how to combine them together so that i can ran them together in my Arduino Mega 2560
The codes are attached below:
Sketch for Part 1:
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
const int transistorPin = 40; // pin that the LED is attached to
const int dtmfPin1 = A1;// pin that the Q4 of MT8870 is attached to
const int dtmfPin2 = A2;
const int dtmfPin3 = A3;
const int dtmfPin4 = A4;
int value1 = 0;
int value2 = 0;
int value3 = 0;
int value4 = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(transistorPin, OUTPUT);
//initialize the DTMF output pin as input
pinMode(dtmfPin1, INPUT);
pinMode(dtmfPin2, INPUT);
pinMode(dtmfPin3, INPUT);
pinMode(dtmfPin4, INPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
value1 = digitalRead(dtmfPin1);
value2 = digitalRead(dtmfPin2);
value3 = digitalRead(dtmfPin3);
value4 = digitalRead(dtmfPin4);
if (value1==HIGH&&value2==HIGH&&value3==HIGH&&value4==LOW)// 7
{
// if the analog value is high enough, turn on the LED:
if (analogValue < 150 ) {
digitalWrite(transistorPin, HIGH);
}
else if (analogValue > 450 ){
digitalWrite(transistorPin,LOW);
}
}
else {
if (value1==LOW&&value2==LOW&&value3==LOW&&value4==HIGH)//8
{
digitalWrite(transistorPin, HIGH);
}
else if (value1==HIGH&&value2==LOW&&value3==LOW&&value4==HIGH)//9
{
digitalWrite(transistorPin,LOW);
}
}
// print the analog value:
Serial.println(analogValue, DEC);
}
Code for part 2:
// LCD Thermostat
// www.hacktronics.com
#include <OneWire.h>
#include <LiquidCrystal.h>
// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 13; // pin 13 will control the backlight
OneWire ds(8); // ds18b20 pin #2 (middle pin) to Arduino pin 8
byte i;
byte present = 0;
byte data[12];
byte addr[8];
int HighByte, LowByte, SignBit, Whole, Fract, TReading, Tc_100, FWhole;
void setup(void) {
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
lcd.begin(2,16); // rows, columns. use 2,16 for a 2x16 LCD, etc.
lcd.clear(); // start with a blank screen
lcd.setCursor(0,0); // set cursor to column 0, row 0
if ( !ds.search(addr)) {
lcd.clear(); lcd.print("No more addrs");
delay(1000);
ds.reset_search();
return;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
lcd.clear(); lcd.print("CRC not valid!");
delay(1000);
return;
}
}
void getTemp() {
int foo, bar;
ds.reset();
ds.select(addr);
ds.write(0x44,1);
present = ds.reset();
ds.select(addr);
ds.write(0xBE);
for ( i = 0; i < 9; i++) {
data[i] = ds.read();
}
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) {
TReading = -TReading;
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
if (Fract > 49) {
if (SignBit) {
--Whole;
} else {
++Whole;
}
}
if (SignBit) {
bar = -1;
} else {
bar = 1;
}
foo = ((Whole * bar) * 18); // celsius to fahrenheit conversion section
FWhole = (((Whole * bar) * 18) / 10) + 32;
if ((foo % 10) > 4) { // round up if needed
++FWhole;
}
}
void printTemp(void) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp is: ");
lcd.setCursor(0,1);
if (SignBit) {
lcd.print("-");
}
lcd.print(Whole);
lcd.print(" C / ");
lcd.print(FWhole);
lcd.print(" F");
}
void loop(void) {
getTemp();
printTemp();
delay(1000);
}