Go Down

Topic: Help combining these 2 sketches (Read 871 times) previous topic - next topic

ac1234

How I can combine these 2 codes in 1?

first code:

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 3

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

DeviceAddress insideThermometer = { 0x28, 0x43, 0xC2, 0x03, 0x04, 0x00, 0x00, 0x85 };

void setup(void)
{
  Serial.begin(9600);
 
  sensors.begin();
 
  sensors.setResolution(insideThermometer, 10);
 
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{
  delay(5000);
  Serial.print("Here we go!!!...\n\r");
  sensors.requestTemperatures();
 
  Serial.print("The temperature is: \n\r");
  printTemperature(insideThermometer);
  Serial.print("\n\r");
  Serial.print("We got it!!!!\n\r");
  Serial.print("\n\r");
  Serial.print("\n\r");
  Serial.print("\n\r");
}


second code:

const byte ledPin =  13;    // LED pin
const byte motionPin = 2;   // motion detector input pin
byte senseMotion = 0;       // variable to hold current state of motion detector

void setup() {
  // set the digital pin directions
  pinMode(ledPin, OUTPUT);     
  pinMode(motionPin, INPUT);
}

void loop()
{
    // Now watch for burglers
    senseMotion = digitalRead(motionPin);
    if (senseMotion == HIGH) {    // burgler found!
      digitalWrite(ledPin, HIGH);
    } else {                      // no burgler, yet...
      digitalWrite(ledPin, LOW);
    }
}

PaulS

Quote
How I can combine these 2 codes in 1?

Fire up the IDE and start typing.

What do you want the combined sketch to do? Take a temperature reading every time there is motion?

Seems like a strange pair of sketches to be combining.

CrossRoads

Copy/paste the pre-setup stuff into the pre-setup section,
Copy/past the void setup stuff into void setup
Copy/past the void loop stuff into void loop.

Let it compile, fix any errors. This code looks pretty benign, no overlapping pins/names/etc that would cause problems.

Code: [Select]


#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 3

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

DeviceAddress insideThermometer = { 0x28, 0x43, 0xC2, 0x03, 0x04, 0x00, 0x00, 0x85 };

const byte ledPin =  13;    // LED pin
const byte motionPin = 2;   // motion detector input pin
byte senseMotion = 0;       // variable to hold current state of motion detector

void setup(void)
{
  Serial.begin(9600);
 
  sensors.begin();
 
  sensors.setResolution(insideThermometer, 10);

  // set the digital pin directions
  pinMode(ledPin, OUTPUT);     
  pinMode(motionPin, INPUT);

 
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{
  delay(5000);
  Serial.print("Here we go!!!...\n\r");
  sensors.requestTemperatures();
 
  Serial.print("The temperature is: \n\r");
  printTemperature(insideThermometer);
  Serial.print("\n\r");
  Serial.print("We got it!!!!\n\r");
  Serial.print("\n\r");
  Serial.print("\n\r");
  Serial.print("\n\r");

    // Now watch for burglers
    senseMotion = digitalRead(motionPin);
    if (senseMotion == HIGH) {    // burgler found!
      digitalWrite(ledPin, HIGH);
    } else {                      // no burgler, yet...
      digitalWrite(ledPin, LOW);
    }

}

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

ac1234

Thanks for the help, I use the code from @Crossroads and works fine.

@PaulS : Yes, thats what im trying to do, but im having issues with the motion sensor.


PaulS

Quote
Yes, thats what im trying to do, but im having issues with the motion sensor.

Help me understand how measuring the temperature whenever there is motion is a useful thing to do. I mean, there must be some context in which this is useful, but I'm not seeing it.

Generally, people want to log temperatures at known intervals, not at random intervals.

CrossRoads

He's getting the temp around every 5 seconds
and then checking for motion. Seems pretty straightforward.

Of course, if the motion happens during the 5 second delay it is missed.

Better would be to use millis() and see if 5 seconds elapsed, and the rest of the time polling the motion sensor. See Blink without Delay in the Learning section, or the  Playground, whereever it is.

PIRs that I've seen sit high when inactive and then pull your input pin low to indicate they've  been triggered.  I think I''d turn on the LED & hold it longer to make sure it was seen. Maybe print something out too.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up