Pages: [1]   Go Down
Author Topic: Help combining these 2 sketches  (Read 663 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 443
Posts: 23835
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

}

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 443
Posts: 23835
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: