instinctivepinoy
instinctivepinoy

Reputation: 9

How to get a input that continuously gives data to output a single digit integer value

To give a short summary of my project, it’s a smart parking system where I let a parking user know whether a parking lot is vacant or not. I’m implementing an XBee network containing 1 coordinator and 2 routers. I have two sensors, 1 sensor at the exit and 1 at the entrance. Those two sensors are the routers and whatever data they gather is being transmitted to the coordinator (output). The two routers have the same code which is:

INPUT CODE (TRANSMITTING):

#include<SoftwareSerial.h>
#define Sensor 8 

void setup() {

pinMode(Sensor,INPUT);
Serial.begin(9600);

}

void loop()
{

bool Detection = digitalRead(Sensor);
int carexit=1;

if(Detection == HIGH)
  {
  Serial.println("Car Exit");

  Serial.write(carexit);

  }
if(Detection == LOW)
  {
  Serial.println("Clear");
  }
}

It’s a very simple code where it detects a car coming in or out. Since the two routers are the same we’ll use the sensor for the cars exiting. When I open the serial monitor, the word “Clear” will keep on being outputted continuously nonstop until detection is found which will show an output of “Car Exit” for about 3 seconds and then return to “Clear” continuously. This data is being transmitted to the coordinator. What I’m trying to do is take that continuous data and have the serial monitor at the coordinator input a single integer value. For example, at the entrance, the sensor will sense the car and transmit the data to the coordinator where it'll increment. The result will show something like this if there’s only 1 vacant slot available:

Vacant slots: 1

and when the car exits the router at the exit, it will transmit a code to the coordinator decrementing it:

Vacant slots: 0

So, the input(router) would be transmitting continuous data while the output(transmitter) will detect it and just register it for a single-digit value. The output code(receiving) by the way is shown below:

OUTPUT CODE(RECEIVING):

#include "SoftwareSerial.h"

// RX: Arduino pin 2, XBee pin DOUT.  TX:  Arduino pin 3, XBee pin DIN


void setup()
{

  Serial.begin(9600);
}
void loop()
{


  if (Serial.available() > 0)
  {
   
    Serial.write(Serial.read());

  }
}

The output code is pretty simple as well. Let me know if there’s any possible way to implement what I’m trying to do and if there are any other details I left out, let me know as well.

Upvotes: 0

Views: 140

Answers (1)

Micha&#235;l Roy
Micha&#235;l Roy

Reputation: 6461

This is a very common problem, best solved at the source, in your sensor transmit code.

//...
bool PreviousDetection = false;
void loop()
{

    bool Detection = digitalRead(Sensor);

    // do not do anything when state hasn't changed.
    if (Detection != PreviousDetection)
    {
        if (Detection)
            Serial.println("Car Exit");
        else
            Serial.println("Clear");
    }
    PreviousDetection = Detection;
}

You may want to add some debouncing to reduce the risk of false readings.

//...
// Debounce thresholds the number depends on the frequency of readings,
// speeed of cars, sensitivity and placement of your sensor...
// Dirt, sun exposure, etc... may influence readings in the long term.
const int LowDebounceThreshold = 3;
const int HighDebounceThreshold = 3;

bool PreviousDetection = false;
int DebounceCounter = 0;
bool DebouncedPreviousDetection = false;

void loop()
{

    bool Detection = digitalRead(Sensor);
    // 
    if (Detection == PreviousDetection)
        ++DebounceCounter; // this will rollover, but will not affect 
                           // DebouncedDetection in any meaningfull way.
    else
        DebounceCounter = 0;

    PreviousDetection = Detection;

    bool DebouncedDetection = PreviousDebouncedDetection;

    if (Detection && DebounceCounter >= HighDebounceThreshold)
        DebouncedDetection = true;
    else if (!Detection && DebounceCounter >= LowDebounceThreshold)
        DebouncedDetection = false;

    if (DebouncedDetection != PreviousDebouncedDetection)
    {
        PreviousDebouncedDetection = DebouncedDetection;
        if (DebouncedDetection) 
            Serial.println("Car Exit");
        else
            Serial.println("Clear");
    }
}

Upvotes: 0

Related Questions