Hola, tengo un problema que me está volviendo un poco loco xD estoy realizando un contador con una barrera laser (LDR + Laser) y 4 displays 7 segmentos. El caso es que funciona bien pero necesito que cuando pase un objeto por el laser cuente solo 1 vez, y ahora si el objeto que pasa por el laser es grande cuenta muchas veces.
Este proyecto es para contar cajas mediante una rampa con la barrera laser al final de ella.
os adjunto el código:
const int LedA = 5;
const int LedB = 8;
const int LedC = 7;
const int LedD = 6;
// Arduino pins connected to the segment driver transistors
const int Led1 = 4;
const int Led2 = 3;
const int Led3 = 2;
const int Led4 = 1;
//LDR + Laser
const int laser = 9;
const int ldr = A0;
long count = 0;
int output = 0;
int remain = 0;
void setup()
{
// Let the Arduino know which pins go where
pinMode(LedA, OUTPUT);
pinMode(LedB, OUTPUT);
pinMode(LedC, OUTPUT);
pinMode(LedD, OUTPUT);
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Led4, OUTPUT);
pinMode(laser, OUTPUT);
digitalWrite(Led1, HIGH);
digitalWrite(Led2, HIGH);
digitalWrite(Led3, HIGH);
digitalWrite(Led4, HIGH);
digitalWrite(laser, HIGH);
}
void loop()
{
// The counter runs from 0-9999 so divide this by 10 as
// we only have three display segments
output = count;
// Display the first digit
// The order of these steps is important to prevent
// visible 'bleeding' on the display
Display(output / 1000); // Send first digit to the 4511
output = output % 1000; // Calculate next digit
digitalWrite(Led1, LOW); // Illuminate first segment
delay(2); // Wait
digitalWrite(Led1, HIGH); // Extinguish first segment
// Display the second digit
Display(output / 100);
output = output % 100;
digitalWrite(Led2, LOW);
delay(2);
digitalWrite(Led2, HIGH);
// Display the third digit
Display(output / 10);
output = output % 10;
digitalWrite(Led3, LOW);
delay(2);
digitalWrite(Led3, HIGH);
Display(output);
digitalWrite(Led4, LOW);
delay(2);
digitalWrite(Led4, HIGH);
if(analogRead(ldr) < 750){
Counter();
}
if (count > 9999)
{
count = 0;
}
}
void Counter()
{
count = count + 1;
}
// Function to display the digit 'n'
void Display(int n)
{
if ( n > 9 )
{
n = 9;
}
if ( n < 0 )
{
n = 0;
}
if ( n / 8 == 1 )
{
digitalWrite(LedD, HIGH);
}
else
{
digitalWrite(LedD, LOW);
}
n = n % 8;
if ( n / 4 == 1 )
{
digitalWrite(LedC, HIGH);
}
else
{
digitalWrite(LedC, LOW);
}
n = n % 4;
if ( n / 2 == 1 )
{
digitalWrite(LedB, HIGH);
}
else
{
digitalWrite(LedB, LOW);
}
n = n % 2;
if ( n == 1 )
{
digitalWrite(LedA, HIGH);
}
else
{
digitalWrite(LedA, LOW);
}
}
Saludos! y gracias