#include #define TEMP_PIN_LEFT 7 // RC-1 #define TEMP_PIN_RIGHT 8 // RC-2 #define VOLT_PIN_LEFT 6 // INPUT 1 #define VOLT_PIN_RIGHT 5 // INPUT 2 #define FAN_PIN 13 LiquidCrystal lcd(2, 3, 4, 9, 10, 11, 12); void OneWireReset(int Pin);//See Note 2 void OneWireOutByte(int Pin, byte d); byte OneWireInByte(int Pin); int NumBytes = 0 ; int incomingByte = 0; int Datain = 0; int outputValue = 0; // value output to the PWM (analog out) void setup() { digitalWrite(TEMP_PIN_RIGHT, LOW); pinMode(TEMP_PIN_RIGHT, INPUT); // sets the digital pin as input (logic 1) digitalWrite(TEMP_PIN_LEFT, LOW); pinMode(TEMP_PIN_LEFT, INPUT); // sets the digital pin as input (logic 1) Serial.begin(9600); while (Serial.available()>0){Serial.read();} ; // Empties the Buffer analogWrite(VOLT_PIN_LEFT,0); analogWrite(VOLT_PIN_RIGHT,0); digitalWrite(FAN_PIN, LOW); //9600 to match the data rate being used by the //serial monitor on my system, which is set to //the Arduino default. (Sample code published //by nuelectronics used a faster baud rate.) lcd.begin(20,4); // (col,row) lcd.clear(); lcd.setCursor(1, 1); // (col,row) lcd.print("- LEFT -"); lcd.setCursor(11, 1); lcd.print("- RIGHT -"); lcd.setCursor(4, 3); lcd.print("**"); lcd.setCursor(14, 3); lcd.print("**"); delay(100); } void loop(){ int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract,FanAction; String TWhole, TFract, ToTransfer; //********** while (Serial.available()<=6) ; incomingByte = Serial.read(); FanAction=incomingByte-48; // READ THE FIRST BYTE if (FanAction==2) { lcd.setCursor(4, 3); lcd.print("** "); lcd.setCursor(14, 3); lcd.print("** "); while (Serial.available()>0){Serial.read();} ; // Empties the Buffer } else { // READ BYTES 2 -> 4 Datain = 0; incomingByte = Serial.read(); Datain+=(incomingByte-48)*100; incomingByte = Serial.read(); Datain+=(incomingByte-48)*10; incomingByte = Serial.read(); Datain+=(incomingByte-48); if (FanAction<2) analogWrite(VOLT_PIN_LEFT, Datain); // READ BYTES 5 -> 7 Datain = 0; incomingByte = Serial.read(); Datain+=(incomingByte-48)*100; incomingByte = Serial.read(); Datain+=(incomingByte-48)*10; incomingByte = Serial.read(); Datain+=(incomingByte-48); if (FanAction<2) analogWrite(VOLT_PIN_RIGHT, Datain); // READ THE LEFT SENSOR OneWireReset(TEMP_PIN_LEFT); OneWireOutByte(TEMP_PIN_LEFT, 0xcc); OneWireOutByte(TEMP_PIN_LEFT, 0x44); // perform temperature conversion, strong pullup for one sec OneWireReset(TEMP_PIN_LEFT); OneWireOutByte(TEMP_PIN_LEFT, 0xcc); OneWireOutByte(TEMP_PIN_LEFT, 0xbe); LowByte = OneWireInByte(TEMP_PIN_LEFT); HighByte = OneWireInByte(TEMP_PIN_LEFT); TReading = (HighByte << 8) + LowByte; SignBit = TReading & 0x8000; // test most sig bit if (SignBit) // negative { TReading = (TReading ^ 0xffff) + 1; // 2's comp } 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; Fract = Fract / 10; TWhole=String(Whole); if (Whole<100) { TWhole='0'+ TWhole; } TFract=String(Fract); ToTransfer=TWhole+TFract; ToTransfer+='*'; lcd.setCursor(14, 3); lcd.print(Whole); // 7 -> LEFT TEMP lcd.print("."); lcd.print(Fract); // READ THE RIGHT SENSOR OneWireReset(TEMP_PIN_RIGHT); OneWireOutByte(TEMP_PIN_RIGHT, 0xcc); OneWireOutByte(TEMP_PIN_RIGHT, 0x44); // perform temperature conversion, strong pullup for one sec OneWireReset(TEMP_PIN_RIGHT); OneWireOutByte(TEMP_PIN_RIGHT, 0xcc); OneWireOutByte(TEMP_PIN_RIGHT, 0xbe); LowByte = OneWireInByte(TEMP_PIN_RIGHT); HighByte = OneWireInByte(TEMP_PIN_RIGHT); TReading = (HighByte << 8) + LowByte; SignBit = TReading & 0x8000; // test most sig bit if (SignBit) // negative { TReading = (TReading ^ 0xffff) + 1; // 2's comp } 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; Fract = Fract / 10; TWhole=String(Whole); if (Whole<100) { TWhole='0'+ TWhole; } TFract=String(Fract); ToTransfer+=TWhole; ToTransfer+=TFract; Serial.print(ToTransfer); lcd.setCursor(4, 3); lcd.print(Whole); // 8-> RIGHT TEMP lcd.print("."); lcd.print(Fract); if (FanAction==1) { digitalWrite(FAN_PIN,HIGH); } else { digitalWrite(FAN_PIN,LOW); } } } // ******************** // * HERE END OF LOOP * // ******************** void OneWireReset(int Pin) // reset. Should improve to act as a presence pulse { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); // bring low for 500 us delayMicroseconds(500); pinMode(Pin, INPUT); delayMicroseconds(500); } void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first). { byte n; for(n=8; n!=0; n--) { if ((d & 0x01) == 1) // test least sig bit { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(5); pinMode(Pin, INPUT); delayMicroseconds(60); } else { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(60); pinMode(Pin, INPUT); } d=d>>1; // now the next bit is in the least sig bit position. } } byte OneWireInByte(int Pin) // read byte, least sig byte first { byte d, n, b; for (n=0; n<8; n++) { digitalWrite(Pin, LOW); pinMode(Pin, OUTPUT); delayMicroseconds(5); pinMode(Pin, INPUT); delayMicroseconds(5); b = digitalRead(Pin); delayMicroseconds(50); d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position } return(d); }