Python

#include <SPI.h>
#include <Ethernet.h>

#include <NewPing.h>

#define SONAR_NUM 2 // Number or sensors.
#define MAX_DISTANCE 400 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0;
float distancia =1000;
float distancia1=1000;
boolean e=false;
boolean y=false;
String var,IO;

EthernetClient client;

String idvariable_1 = "58ff93867be12b532d5053f9";
String token = "4oceb92f1sc6p66cf0svrgsoro";

NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(5, 2, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(7, 8, MAX_DISTANCE),
};

void setup()
{
Serial.begin(9600);
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;

}

void loop()
{
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
if (millis() >= pingTimer[i]) { // Is it this sensor's time to ping?
pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
currentSensor = i; // Sensor being accessed.
cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor.
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
}
}

delay(1000);

}

void echoCheck() { // If ping received, set the sensor distance to array.
if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
for (uint8_t i = 0; i < SONAR_NUM; i++) {
//Serial.print(i);
//Serial.print("=");

if(i==0) {
distancia= cm[i];
//Serial.print(distancia);
}
if (i==1){
distancia1= cm[i];
//Serial.print(distancia1);
}

}
//Serial.println();
ingreso_salida(distancia,distancia1);
}

void ingreso_salida(float distancia, float distancia1){
if(distancia <= 50.0)
{
e=true;
if(y=true)
{
//la persona esta saliendo
IO="out";
y=false;
e=false;
}
}

if(distancia1 <=50.0)
{
y=true;
if(e=true)
{
//la persona esta entrando
IO="in";
y=false;
e=false;
}
}

save_value(String(IO));
}

void save_value(String IO)
{
// if you get a connection, report back via serial:
int num_1= 0;
if (IO=="out"){

var = "Salida";
num_1 = var.length();
IO="";
}
else if (IO=="in"){
var = "Ingres";
num_1 = var.length();
IO="";
}
Serial.print(var);

}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License