Hi guys, I'm making a thermometer that will display the temperature in binary (in celcius) with 8 LED's. I have no idea how to merge my code with one that I've found on the internet (a code that actually works, but only writes to serial monitor) and I need your help, quite fast, preferably. I have no idea which value represents the degrees in the code I've found, but "T" is the variable for temperature in my code.
Minus (-) degrees are not important at the moment, but it wouldn't be wrong if someone could do that in some way. However, I've assigned ledinv (pinout 5) for turning on when it goes negative.
My own code:
int led1=6;
int led2=7;
int led4=8;
int led8=9;
int led16=10;
int led32=11;
int led64=12;
int led128=13;
int ledinv=5;
void setup() {
// put your setup code here, to run once:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led16, OUTPUT);
pinMode(led32, OUTPUT);
pinMode(led64, OUTPUT);
pinMode(led128, OUTPUT);
pinMode(ledinv, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int T=76;
if (T >= 64) {
digitalWrite(led64, HIGH);
T=T-64;}
else{
digitalWrite(led64, LOW);}
if (T >= 32) {
digitalWrite(led32, HIGH);
T=T-32;}
else{
digitalWrite(led32, LOW);}
if (T >= 16) {
digitalWrite(led16, HIGH);
T=T-16;}
else{
digitalWrite(led16, LOW);}
if (T >= 8) {
digitalWrite(led8, HIGH);
T=T-8;}
else{
digitalWrite(led8, LOW);}
if (T >= 4) {
digitalWrite(led4, HIGH);
T=T-4;}
else{
digitalWrite(led4, LOW);}
if (T >= 2) {
digitalWrite(led2, HIGH);
T=T-2;}
else{
digitalWrite(led2, LOW);}
if (T >= 1) {
digitalWrite(led1, HIGH);}
else{
digitalWrite(led1, LOW);}
delay(2000);
}
The code I want to implement my own code into:
#include <OneWire.h>
#include <DallasTemperature.h>
int tempPin=0;
int led1=6;
int led2=7;
int led4=8;
int led8=9;
int led16=10;
int led32=11;
int led64=12;
int led128=13;
int ledinv=5;
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
void setup(void) {
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led16, OUTPUT);
pinMode(led32, OUTPUT);
pinMode(led64, OUTPUT);
pinMode(led128, OUTPUT);
pinMode(ledinv, OUTPUT);
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)) {
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION,DEC);
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}
else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
// method 1 - slower
//Serial.print("Temp C: ");
//Serial.print(sensors.getTempC(deviceAddress));
//Serial.print(" Temp F: ");
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
// method 2 - faster
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.println();
Serial.println();
}
void loop(void) {
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
delay(2000);
Serial.print("Requesting temperature(s)...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
//else ghost device! Check your power requirements and cabling
}
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
Please help, and ask if I weren't clear about something.
EDIT: I use the DS18X20 temperature sensor if that's somehow relevant to this. Also, everything should be wired correctly.
//HElephant.