Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1. /*
  2.   Ping))) Sensor
  3.  
  4.   This sketch reads a PING))) ultrasonic
  5.   rangefinder and returns the distance to the
  6.   closest object in range. To do this, it sends a
  7.   pulse to the sensor to initiate a reading, then
  8.   listens for a pulse to return.  The length of
  9.   the returning pulse is proportional to the
  10.   distance of the object from the sensor.
  11.  
  12.   The circuit:
  13.    * +V connection of the PING))) attached to +5V
  14.    * GND connection attached to ground
  15.    * SIG connection attached to digital pin 7
  16.  
  17.   http://www.arduino.cc/en/Tutorial/Ping
  18.  
  19.   This example code is in the public domain.
  20. */
  21.  
  22. int inches = 0;
  23.  
  24. int cm = 0;
  25.  
  26. long readUltrasonicDistance(int pin)
  27. {
  28.   pinMode(pin, OUTPUT);  // Clear the trigger
  29.   digitalWrite(pin, LOW);
  30.   delayMicroseconds(2);
  31.   // Sets the pin on HIGH state for 10 micro seconds
  32.   digitalWrite(pin, HIGH);
  33.   delayMicroseconds(10);
  34.   digitalWrite(pin, LOW);
  35.   pinMode(pin, INPUT);
  36.   // Reads the pin, and returns the sound wave travel time in microseconds
  37.   return pulseIn(pin, HIGH);
  38. }
  39.  
  40. void setup()
  41. {
  42.   pinMode(7, INPUT);
  43.   Serial.begin(9600);
  44.  
  45. }
  46.  
  47. void loop()
  48. {
  49.   // measure the ping time in cm
  50.   cm = 0.01723 * readUltrasonicDistance(7);
  51.   // convert to inches by dividing by 2.54
  52.   inches = (cm / 2.54);
  53.   Serial.print(inches);
  54.   Serial.print("in, ");
  55.   Serial.print(cm);
  56.   Serial.println("cm");
  57.   delay(100); // Wait for 100 millisecond(s)
  58. }
  59. /*
  60.   LiquidCrystal Library - Hello World
  61.  
  62.  Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  63.  library works with all LCD displays that are compatible with the
  64.  Hitachi HD44780 driver. There are many of them out there, and you
  65.  can usually tell them by the 16-pin interface.
  66.  
  67.  This sketch prints "Hello World!" to the LCD
  68.  and shows the time.
  69.  
  70.   The circuit:
  71.  * LCD RS pin to digital pin 12
  72.  * LCD Enable pin to digital pin 11
  73.  * LCD D4 pin to digital pin 5
  74.  * LCD D5 pin to digital pin 4
  75.  * LCD D6 pin to digital pin 3
  76.  * LCD D7 pin to digital pin 2
  77.  * LCD R/W pin to ground
  78.  * LCD VSS pin to ground
  79.  * LCD VCC pin to 5V
  80.  * 10K resistor:
  81.  * ends to +5V and ground
  82.  * wiper to LCD VO pin (pin 3)
  83.  
  84.  Library originally added 18 Apr 2008
  85.  by David A. Mellis
  86.  library modified 5 Jul 2009
  87.  by Limor Fried (http://www.ladyada.net)
  88.  example added 9 Jul 2009
  89.  by Tom Igoe
  90.  modified 22 Nov 2010
  91.  by Tom Igoe
  92.  
  93.  This example code is in the public domain.
  94.  
  95.  http://www.arduino.cc/en/Tutorial/LiquidCrystal
  96.  */
  97.  
  98. // include the library code:
  99. #include <LiquidCrystal.h>
  100.  
  101. // initialize the library with the numbers of the interface pins
  102. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  103.  
  104. void setup() {
  105.   // set up the LCD's number of columns and rows:
  106.   lcd.begin(16, 2);
  107.   // Print a message to the LCD.
  108.   lcd.print("hello, world!");
  109. }
  110.  
  111. void loop() {
  112.   // set the cursor to column 0, line 1
  113.   // (note: line 1 is the second row, since counting begins with 0):
  114.   lcd.setCursor(0, 1);
  115.   // print the number of seconds since reset:
  116.   lcd.print(millis() / 1000);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement