Created
March 16, 2018 20:10
-
-
Save marbaque/dc1861a4eb840b090764732bcbf5e6ab to your computer and use it in GitHub Desktop.
código para contar personas con sensores infrarrojos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
IR Breakbeam sensor | |
contador hecho por Alejandro y modificado para usar con IR | |
*/ | |
#define LEDPIN1 13 | |
#define LEDPIN2 12 | |
#define SENSORPIN1 4 | |
#define SENSORPIN2 5 | |
// variables will change: | |
int hitA = 0, lasthitA=0; // variable for reading the pushbutton status | |
int hitB = 0, lasthitB= 0; // variable for reading the pushbutton status | |
int primero = 0, segundo = 0; | |
void setup() { | |
// initialize the LED pin as an output: | |
pinMode(LEDPIN1, OUTPUT); | |
pinMode(LEDPIN2, OUTPUT); | |
// initialize the sensor pin as an input: | |
pinMode(SENSORPIN1, INPUT); | |
pinMode(SENSORPIN2, INPUT); | |
digitalWrite(SENSORPIN1, HIGH); // turn on the pullup | |
digitalWrite(SENSORPIN2, HIGH); // turn on the pullup | |
Serial.begin(9600); | |
} | |
void loop(){ | |
// read the state of the pushbutton value: | |
hitA = digitalRead(SENSORPIN1); | |
hitB = digitalRead(SENSORPIN2); | |
// check if the sensor hitA beam is broken | |
// if it is, the hitA is LOW: | |
if (hitA == LOW) { | |
// turn LED on: | |
//Serial.println("Broken 1"); | |
btnPressed(); | |
registrar(1); | |
verificar(); | |
} | |
else { | |
// turn LED off: | |
//digitalWrite(LEDPIN1, LOW); | |
} | |
if (hitA && !hitA) { | |
Serial.println("Unbroken 1"); | |
} | |
if (!hitA && hitA) { | |
Serial.println("Broken 1"); | |
} | |
lasthitA = hitA; | |
//sensor 2 | |
if (hitB == LOW) { | |
// turn LED on: | |
//digitalWrite(LEDPIN2, HIGH); | |
//Serial.println("Broken 2"); | |
btnPressed(); | |
registrar(2); | |
verificar(); | |
} | |
else { | |
// turn LED off: | |
//digitalWrite(LEDPIN2, LOW); | |
} | |
if (hitB && !hitB) { | |
Serial.println("Unbroken 2"); | |
} | |
if (!hitB && hitB) { | |
Serial.println("Broken 2"); | |
} | |
lasthitB = hitB; | |
delay(50); | |
} | |
void btnPressed() { | |
while (digitalRead(hitA) == LOW || digitalRead(hitB) == LOW) { | |
} | |
} | |
void registrar(int posicion) { | |
if (primero == 0) { | |
primero = posicion; | |
} | |
else { | |
segundo = posicion; | |
} | |
mostrarDatos(); | |
} | |
void mostrarDatos() { | |
Serial.print("PRIMERO: "); | |
Serial.println(primero); | |
Serial.print("SEGUNDO: "); | |
Serial.println(segundo); | |
} | |
void verificar() { | |
if (primero != 0 && segundo != 0) { | |
if (primero != segundo) { | |
Serial.println("INGRESO"); | |
reiniciar; | |
digitalWrite(LEDPIN1, HIGH); | |
delay(1000); | |
digitalWrite(LEDPIN1, LOW); | |
} | |
else { | |
Serial.println("REGRESO"); | |
reiniciar; | |
digitalWrite(LEDPIN2, HIGH); | |
delay(1000); | |
digitalWrite(LEDPIN2, LOW); | |
} | |
} | |
} | |
void reiniciar() { | |
primero = 0; | |
segundo = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment