Buscar este blog

domingo, 15 de diciembre de 2013

Código Arduino - Sensor de distancia



Código Arduino

Este sería el código de la pantalla LCD
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 8 Feb 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
3
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
El codigo del sensor de distancia seria el siguiente:
/*Este programa utiliza los ultrasonidos para determinar a qué
distancia se encuentra un objeto situado dentro de su campo de
acción. El Sensor emite un pulso ultrasónico, escucha la onda
rebotada.
Calcula la distancia en función del tiempo que tarda
en regresar la onda.
El Circuito:
* +Vcc conectado a +5V
* GND conectado a ground
* TRIG conectado a digital pin 7
* ECHO conectado a digital pin 8
*/
const int pingEmisor = 7;
const int pingReceptor = 8;
void setup() {
pinMode(pingEmisor, OUTPUT);
pinMode(pingReceptor, INPUT);
Serial.begin(9600); // Inicia la comunicación con el puerto de serie:
}
void loop()
{
// Establece las variables para la duración del pulso, pulgada y
centímetros.
// Da el resultado de la distancia en centímetros(cm):
long duration, cm;
// El sensor es disparado con un pulso HIGH de 2 o mas microsegundos.
// Da dos pequeños pulsos LOW para asegurarse de que el pulso HIGH es
limpio:
digitalWrite(pingEmisor, LOW);
delayMicroseconds(2);
digitalWrite(pingEmisor, HIGH);
delayMicroseconds(5);
4
digitalWrite(pingEmisor, LOW);
//Para leer el "eco" o pulso rebotado utilizamos el digital pin 8.
//El tiempo (en microsegundos) que tarda entre emitir el pulso
//y recibir el eco nos dará la medida.
duration = pulseIn(pingReceptor, HIGH);
// Aqui convertimos el tiempo en distancia.
// La velocidad del sonido es 340 m/s o 29 microsegundos centimetro.
// Como la onda ve y vuelve, tenemos que tomar la mitad de este
valor,
// para que solo considere la onda rebotada.
cm = duration / 29 /2;
//Se imprime por pantalla el resultado.
Serial.print(cm);
Serial.print("cm\n");
delay(100);
}
/*
long microsecondsToCentimeters(long microseconds)
{
// La velocidad del sonido es 340 m/s o 29 microsegundos centimetro.
// Como la onda ve y vuelve, tenemos que tomar la mitad de este
valor,
// para que solo considere la onda rebotada.
return microseconds / 29 / 2;
}*/
Una vez ensamblados los dos códigos quedaría así:
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* +Vcc conectado a +5V
* GND conectado a ground
* TRIG conectado a digital pin 7
* ECHO conectado a digital pin 8
Library originally added 18 Apr 2008
5
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 8 Feb 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int pingEmisor = 7;
const int pingReceptor = 8;
void setup() {
pinMode(pingEmisor, OUTPUT);
pinMode(pingReceptor, INPUT);
Serial.begin(9600); // Inicia la comunicación con el puerto de
serie:
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Distancia:");
}
void loop() {
// Establece las variables para la duración del pulso, pulgada y
centímetros.
// Da el resultado de la distancia en centímetros(cm):
long duration, cm;
// El sensor es disparado con un pulso HIGH de 2 o mas
microsegundos.
// Da dos pequeños pulsos LOW para asegurarse de que el pulso
HIGH es limpio:
digitalWrite(pingEmisor, LOW);
delayMicroseconds(2);
digitalWrite(pingEmisor, HIGH);
delayMicroseconds(5);
digitalWrite(pingEmisor, LOW);
//Para leer el "eco" o pulso rebotado utilizamos el digital pin
8.
//El tiempo (en microsegundos) que tarda entre emitir el pulso
//y recibir el eco nos dará la medida.
duration = pulseIn(pingReceptor, HIGH);
// Aqui convertimos el tiempo en distancia.
// La velocidad del sonido es 340 m/s o 29 microsegundos
centimetro.
// Como la onda ve y vuelve, tenemos que tomar la mitad de este
valor,
6
// para que solo considere la onda rebotada.
cm = duration / 29 /2;
//Se imprime por pantalla el resultado.
delay(100);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with
0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(cm);
lcd.print (" cm ");
delay(1000);
Serial.println(cm);
}

No hay comentarios:

Publicar un comentario