Go Down

### Topic: Help merging two codes into one. (Read 1 time)previous topic - next topic

#### HElephant

##### Oct 20, 2012, 02:00 amLast Edit: Oct 20, 2012, 02:09 am by HElephant Reason: 1
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:
Code: [Select]
`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:
Code: [Select]
`#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 foundDeviceAddress tempDeviceAddress; // We'll use this variable to store a found device addressvoid 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 devicevoid 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 addressvoid printAddress(DeviceAddress deviceAddress){  for (uint8_t i = 0; i < 8; i++)  {    if (deviceAddress[i] < 16) Serial.print("0");    Serial.print(deviceAddress[i], HEX);  }}`

EDIT: I use the DS18X20 temperature sensor if that's somehow relevant to this. Also, everything should be wired correctly.

//HElephant.
I'm a newb using the Arduino Uno.

|| CPU: AMD Athlon II X4 651 || Motherboard: Gigabyte GA-A75-D3H || RAM: Corsair VENGEANCE 8 GB || GPU:

#1
##### Oct 20, 2012, 04:51 am

This function reports the temperature.

Code: [Select]
`// function to print the temperature for a devicevoid printTemperature(DeviceAddress deviceAddress){  // method 1 - slower  //Serial.print("Temp C: ");  //Serial.print(sensors.getTempC(deviceAddress));      //this report temp C  //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);     //this variable is temperature in C  Serial.println();  Serial.println();}`
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

#2
##### Oct 20, 2012, 05:07 am

Inside of the library it had a sketch called "simple", here it is. I think it should help.

Code: [Select]
`#include <OneWire.h>#include <DallasTemperature.h>// Data wire is plugged into port 2 on the Arduino#define ONE_WIRE_BUS 2// 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);void setup(void){  // start serial port  Serial.begin(9600);  Serial.println("Dallas Temperature IC Control Library Demo");  // Start up the library  sensors.begin();}void loop(void){   // call sensors.requestTemperatures() to issue a global temperature   // request to all devices on the bus  Serial.print("Requesting temperatures...");  sensors.requestTemperatures(); // Send the command to get temperatures  Serial.println("DONE");    Serial.print("Temperature for the device 1 (index 0) is: ");  Serial.println(sensors.getTempCByIndex(0));  }`
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

#3

#### HElephant

#4
##### Oct 20, 2012, 12:10 pm
Cyclegadget, thank you so much! I tried using that code earlier, but couldn't get it working! Now everything works great, and feel free to use my code:
Code: [Select]
`#include <OneWire.h>#include <DallasTemperature.h>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// 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);void setup(void){  // start serial port  Serial.begin(9600);    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);    Serial.println("Dallas Temperature IC Control Library Demo");  // Start up the library  sensors.begin();}void loop(void){   // call sensors.requestTemperatures() to issue a global temperature   // request to all devices on the bus  Serial.print("Requesting temperatures...");  sensors.requestTemperatures(); // Send the command to get temperatures  Serial.println("DONE");    int T = (sensors.getTempCByIndex(0));     Serial.print("Temperature for the device 1 (index 0) is: ");  Serial.println(sensors.getTempCByIndex(0));        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(500);    }`

The variable "ledinv" in not yet used, because I haven't worked on getting negative numbers working. I'll lock this thread, since I solved the problem!
I'm a newb using the Arduino Uno.

|| CPU: AMD Athlon II X4 651 || Motherboard: Gigabyte GA-A75-D3H || RAM: Corsair VENGEANCE 8 GB || GPU:

#### Grumpy_Mike

#5
##### Oct 20, 2012, 05:12 pm
Quote
I'll lock this thread, since I solved the problem!

That is consider bad manners here.

Go Up

Please enter a valid email to subscribe