Pages: [1]   Go Down
Author Topic: Help merging two codes into one.  (Read 960 times)
0 Members and 1 Guest are viewing this topic.
Sweden
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Student with interests in music and technology.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
#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.
« Last Edit: October 19, 2012, 07:09:47 pm by HElephant » Logged

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:

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 This function reports the temperature.

Code:
// function to print the temperature for a device
void 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();
}
Logged


Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 I downloaded the Dallas temperature 372 beta zip.

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

Code:
#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)); 
}
Logged


Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html
Logged

Sweden
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Student with interests in music and technology.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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!
Logged

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:

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'll lock this thread, since I solved the problem!
That is consider bad manners here.
Logged

Pages: [1]   Go Up
Jump to: