Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Help merging two codes into one. on: October 20, 2012, 05:10:34 am
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!
2  Using Arduino / Programming Questions / Help merging two codes into one. on: October 19, 2012, 07:00:07 pm
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.
3  Using Arduino / Sensors / Re: PT100 sensor - help a beginner? on: October 18, 2012, 11:33:26 am
Well, I think that I'll just go and buy a DS18B20 http://www.lawicel-shop.se/prod/dig-therm-ds18b20-1wire_854351/Maxim--Dallas_55370/ENG/EUR and save myself a lot of frustration and time. Thanks for your answers guys!
4  Using Arduino / Sensors / PT100 sensor - help a beginner? on: October 17, 2012, 05:49:58 pm
Hi guys, I'm a beginner when it comes to understanding the Arduino. I'm currently using the Uno, and that's the only one I've used.

So, don't let me waste your time, and let's get in topic...
I just got the PT100 sensor (the one with 3 wires, one blue, red and white) and I'm going to make a thermometer, but well, I have a problem... I have no idea how to read the temperature, or the values at all. I get very strange values and it seems like it doesn't work at all, but it's just messing around with me.

I've looked around on the internet, for people using the PT100 with the Arduino and how they've done but it seems like nobody has. So I looked up a random page (http://www.yetanother.eu/arduino/articles/temp_basic.php) with a guy using an Arduino for reading temps (Note: with another sensor!) and I used his code (code in link, and I'll post it below) which didn't really work. When I uploaded the code (after modifying it just a little bit, because it was minor errors in it) the Arduino just started to give me strange values in the Serial Monitor (in the range of 70-90), and the values didn't change when I had the PT100 in my room (21c), or on the balcony (11c).

Strange code that I got off an website:
(note that the comment in the code says "Temperature sensor plugs into analog pin 0" and later on "int tempPin=0;")
Code:
/*
  TMP36 Temperature Sensor.
  Temperature sensor plugs into analog pin 0.
  5V supply.
*/
 
int tempPin=0;
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  float temp=(analogRead(pin)*5.0)/1024.0;
  temp=(temp-0.5)*100;
  Serial.println(temp);
  delay(1000);
 
}

I think that I've wired something wrong. I tried a lot of combinations (switching the wires between 0, A0, GND and 5+), but nothing seems to work. Could someone explain to me how and where to connect those wires and maybe give me a little code just to make it work? :I

I'd appreciate quick response, thanks guys! :)
Pages: [1]