Alarma visual (Led con ultrasonido)





int cm = 0;
int led = 13;

long readUltrasonicDistance(int pin)
{
  pinMode(pin, OUTPUT);  // Clear the trigger
  digitalWrite(pin, LOW);
  delayMicroseconds(2);
  // Sets the pin on HIGH state for 10 micro seconds
  digitalWrite(pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pin, LOW);
  pinMode(pin, INPUT);
  // Reads the pin, and returns the sound wave travel time in microseconds
  return pulseIn(pin, HIGH);
}

void setup()
{
  pinMode(7, INPUT);
  pinMode (led, OUTPUT);

}

void loop()
{
  // measure the ping time in cm
  cm = 0.01723 * readUltrasonicDistance(7);
 
  if (cm < 70)
{
  digitalWrite(led, HIGH);
  delay(1000);
  digitalWrite(led, LOW);
  delay(1000);
}
else
{
  digitalWrite(led, LOW);
}
 
}

Comentarios

Entradas populares de este blog

Sistema de turnos con led de 7 segmentos y pulsadores

Teclado matricial 4x4 con LCD