Skip to content
  • IR distance sensor usage?

    Hardware proximity infrared distance
    4
    0 Votes
    4 Posts
    3k Views
    hetbeestH
    I did some quick coding, combining it with a temperature sensor. It did work for a couple of hours, after that my 9V battery was drained so far that I didn't get any data send from the sensors anymore. Quite likely the IR sensor uses a lot of power while it keeps transmitting IR. Anyhow, my quick and dirty code is: // Example sketch showing how to send in OneWire temperature readings #include <MySensor.h> #include <SPI.h> #include <DallasTemperature.h> #include <OneWire.h> #include <DistanceGP2Y0A21YK.h> #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 #define CHILD_ID_DISTANCE 25 DistanceGP2Y0A21YK Dist; unsigned long SLEEP_TIME = 500; // Sleep time between reads (in milliseconds) OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); MySensor gw; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; boolean receivedConfig = false; boolean metric = true; int distance; int lastDistance=0; int loopCounter=0; int loopsBetweenTemp = 60; // Initialize temperature message MyMessage msg(0,V_TEMP); MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE); void setup() { // Startup OneWire sensors.begin(); Dist.begin(0); // Startup and initialize MySensors library. Set callback for incoming messages. gw.begin(); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("Temperature and Distance Sensor", "1.0"); gw.present(CHILD_ID_DISTANCE,S_DISTANCE); // Fetch the number of attached temperature sensors numSensors = sensors.getDeviceCount(); // Present all sensors to controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { gw.present(i, S_TEMP); } } void loop() { // Process incoming messages (like config from server) gw.process(); distance = Dist.getDistanceCentimeter(); if (distance != lastDistance) { lastDistance = distance; gw.send(msgdist.set(distance)); } if (loopCounter == 0) { // Fetch temperatures from Dallas sensors sensors.requestTemperatures(); // Read temperatures and send them to controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { // Fetch and round temperature to one decimal float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; // Only send data if temperature has changed and no error if (lastTemperature[i] != temperature && temperature != -127.00) { // Send in the new temperature gw.send(msg.setSensor(i).set(temperature,1)); lastTemperature[i]=temperature; } } } if (++loopCounter >= loopsBetweenTemp) loopCounter = 0; gw.sleep(SLEEP_TIME); }
  • 2 Votes
    6 Posts
    3k Views
    hekH
    @destos Wow!
  • 0 Votes
    3 Posts
    2k Views
    P
    @doctor64 said: PIR sensors Not anyone, a sensor that generates a signal when I come close to the car, I rather it not be IR since, AFAIK, they need to be in specific positions to work, unlike NFC or Bluetooth that cover areas around the sensor. Basically I'm trying to create a smart switch that goes on or of based on whether I'm close (in the range of 10-20 feet) to it or not.

10

Online

11.7k

Users

11.2k

Topics

113.1k

Posts