Pages: [1] 2   Go Down
Author Topic: Ultrasonic Sensor and LCD Display code help needed !!  (Read 3625 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi... I was wondering if anyone could help me with the code when connecting an LCD and ultrasonic sensor...
I wired up the ultrasonic sensor on an arduino duemilanove just like the image in the link below.


http://arduino.cc/en/uploads/Tutorial/ping_bb.png

I then wired up a parralel LCD display screen just like in the link below..

http://www.google.co.uk/imgres?imgurl=http://arduino.cc/en/uploads/Tutorial/LCD_bb.png&imgrefurl=http://arduino.cc/en/Tutorial/LiquidCrystal&h=621&w=1012&sz=37&tbnid=VU4yq9GTv30ICM:&tbnh=74&tbnw=120&zoom=1&usg=__pYcaxlGGoM7mwJDCZRWC4goxsQs=&docid=xsJV83MIGq37lM&hl=en&sa=X&ei=sa5YUO-MK4e20QXw84HgAw&ved=0CCMQ9QEwAA&dur=303

After uploading the code for the lcd display, the lcd works perfectly ( but obviously the ultrasonic sensor isnt working) . In addition, when uploading the code for the ultrasonic sensor, the ultrasonic sensor works perfectly (on the serial monitor) but the lcd is obviously not working..


The problem i am having is that i do not know how to combine the two codes to make the reading of the ultrasonic sensor work on the LCD display . Can somebody please help me with this issue.. I have posted the seperate codes for the ultrasonic sensor and the lcd  below :


CODE FOR ULTRASONIC SENSOR :

/* Ping))) Sensor
   
   This sketch reads a PING))) ultrasonic rangefinder and returns the
    distance to the closest object in range. To do this, it sends a pulse
    to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
     
    The circuit:
     * +V connection of the PING))) attached to +5V
     * GND connection of the PING))) attached to ground
     * SIG connection of the PING))) attached to digital pin 7
 
   http://www.arduino.cc/en/Tutorial/Ping
   
    created 3 Nov 2008
    by David A. Mellis
    modified 30 Aug 2011
    by Tom Igoe
 
    This example code is in the public domain.
 
 */
 
// this constant won't change.  It's the pin number
 // of the sensor's output:
 const int pingPin = 7;
 
void setup() {
   // initialize serial communication:
   Serial.begin(9600);
 }
 
void loop()
 {
   // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
   long duration, inches, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
   // pulse whose duration is the time (in microseconds) from the sending
   // of the ping to the reception of its echo off of an object.
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);
 
  // convert the time into a distance
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   
  Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   
  delay(100);
 }
 
long microsecondsToInches(long microseconds)
 {
   // According to Parallax's datasheet for the PING))), there are
   // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
   // second).  This gives the distance travelled by the ping, outbound
   // and return, so we divide by 2 to get the distance of the obstacle.
   // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
   return microseconds / 74 / 2;
 }
 
long microsecondsToCentimeters(long microseconds)
 {
   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
   // The ping travels out and back, so to find the distance of the
   // object we take half of the distance travelled.
   return microseconds / 29 / 2;
 }




CODE FOR LCD  :

Code
 






/*
   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
  * LCD R/W pin to ground
  * 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 22 Nov 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);
 
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);
 }

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi... I was wondering if anyone could help me with the code when connecting an LCD and ultrasonic sensor...
I wired up the ultrasonic sensor on an arduino duemilanove just like the image in the link below.


http://arduino.cc/en/uploads/Tutorial/ping_bb.png

I then wired up a parralel LCD display screen just like in the link below..

http://www.google.co.uk/imgres?imgurl=http://arduino.cc/en/uploads/Tutorial/LCD_bb.png&imgrefurl=http://arduino.cc/en/Tutorial/LiquidCrystal&h=621&w=1012&sz=37&tbnid=VU4yq9GTv30ICM:&tbnh=74&tbnw=120&zoom=1&usg=__pYcaxlGGoM7mwJDCZRWC4goxsQs=&docid=xsJV83MIGq37lM&hl=en&sa=X&ei=sa5YUO-MK4e20QXw84HgAw&ved=0CCMQ9QEwAA&dur=303

After uploading the code for the lcd display, the lcd works perfectly ( but obviously the ultrasonic sensor isnt working) . In addition, when uploading the code for the ultrasonic sensor, the ultrasonic sensor works perfectly (on the serial monitor) but the lcd is obviously not working..


The problem i am having is that i do not know how to combine the two codes to make the reading of the ultrasonic sensor work on the LCD display . Can somebody please help me with this issue.. I have posted the seperate codes for the ultrasonic sensor and the lcd  below :


CODE FOR ULTRASONIC SENSOR :

/* Ping))) Sensor
   
   This sketch reads a PING))) ultrasonic rangefinder and returns the
    distance to the closest object in range. To do this, it sends a pulse
    to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
     
    The circuit:
     * +V connection of the PING))) attached to +5V
     * GND connection of the PING))) attached to ground
     * SIG connection of the PING))) attached to digital pin 7
 
   http://www.arduino.cc/en/Tutorial/Ping
   
    created 3 Nov 2008
    by David A. Mellis
    modified 30 Aug 2011
    by Tom Igoe
 
    This example code is in the public domain.
 
 */
 
// this constant won't change.  It's the pin number
 // of the sensor's output:
 const int pingPin = 7;
 
void setup() {
   // initialize serial communication:
   Serial.begin(9600);
 }
 
void loop()
 {
   // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
   long duration, inches, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
   // pulse whose duration is the time (in microseconds) from the sending
   // of the ping to the reception of its echo off of an object.
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);
 
  // convert the time into a distance
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   
  Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   
  delay(100);
 }
 
long microsecondsToInches(long microseconds)
 {
   // According to Parallax's datasheet for the PING))), there are
   // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
   // second).  This gives the distance travelled by the ping, outbound
   // and return, so we divide by 2 to get the distance of the obstacle.
   // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
   return microseconds / 74 / 2;
 }
 
long microsecondsToCentimeters(long microseconds)
 {
   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
   // The ping travels out and back, so to find the distance of the
   // object we take half of the distance travelled.
   return microseconds / 29 / 2;
 }




CODE FOR LCD  :

Code
 






/*
   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
  * LCD R/W pin to ground
  * 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 22 Nov 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);
 
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);
 }

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi... I was wondering if anyone could help me with the code when connecting an LCD and ultrasonic sensor...
I wired up the ultrasonic sensor on an arduino duemilanove just like the image in the link below.


http://arduino.cc/en/uploads/Tutorial/ping_bb.png

I then wired up a parralel LCD display screen just like in the link below..

http://www.google.co.uk/imgres?imgurl=http://arduino.cc/en/uploads/Tutorial/LCD_bb.png&imgrefurl=http://arduino.cc/en/Tutorial/LiquidCrystal&h=621&w=1012&sz=37&tbnid=VU4yq9GTv30ICM:&tbnh=74&tbnw=120&zoom=1&usg=__pYcaxlGGoM7mwJDCZRWC4goxsQs=&docid=xsJV83MIGq37lM&hl=en&sa=X&ei=sa5YUO-MK4e20QXw84HgAw&ved=0CCMQ9QEwAA&dur=303

After uploading the code for the lcd display, the lcd works perfectly ( but obviously the ultrasonic sensor isnt working) . In addition, when uploading the code for the ultrasonic sensor, the ultrasonic sensor works perfectly (on the serial monitor) but the lcd is obviously not working..


The problem i am having is that i do not know how to combine the two codes to make the reading of the ultrasonic sensor work on the LCD display . Can somebody please help me with this issue.. I have posted the seperate codes for the ultrasonic sensor and the lcd  below :


CODE FOR ULTRASONIC SENSOR :

/* Ping))) Sensor
   
   This sketch reads a PING))) ultrasonic rangefinder and returns the
    distance to the closest object in range. To do this, it sends a pulse
    to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
     
    The circuit:
     * +V connection of the PING))) attached to +5V
     * GND connection of the PING))) attached to ground
     * SIG connection of the PING))) attached to digital pin 7
 
   http://www.arduino.cc/en/Tutorial/Ping
   
    created 3 Nov 2008
    by David A. Mellis
    modified 30 Aug 2011
    by Tom Igoe
 
    This example code is in the public domain.
 
 */
 
// this constant won't change.  It's the pin number
 // of the sensor's output:
 const int pingPin = 7;
 
void setup() {
   // initialize serial communication:
   Serial.begin(9600);
 }
 
void loop()
 {
   // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
   long duration, inches, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
   // pulse whose duration is the time (in microseconds) from the sending
   // of the ping to the reception of its echo off of an object.
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);
 
  // convert the time into a distance
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   
  Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   
  delay(100);
 }
 
long microsecondsToInches(long microseconds)
 {
   // According to Parallax's datasheet for the PING))), there are
   // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
   // second).  This gives the distance travelled by the ping, outbound
   // and return, so we divide by 2 to get the distance of the obstacle.
   // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
   return microseconds / 74 / 2;
 }
 
long microsecondsToCentimeters(long microseconds)
 {
   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
   // The ping travels out and back, so to find the distance of the
   // object we take half of the distance travelled.
   return microseconds / 29 / 2;
 }




CODE FOR LCD  :

Code
 






/*
   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
  * LCD R/W pin to ground
  * 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 22 Nov 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);
 
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);
 }

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi... I was wondering if anyone could help me with the code when connecting an LCD and ultrasonic sensor...
I wired up the ultrasonic sensor on an arduino duemilanove just like the image in the link below.


http://arduino.cc/en/uploads/Tutorial/ping_bb.png

I then wired up a parralel LCD display screen just like in the link below..

http://www.google.co.uk/imgres?imgurl=http://arduino.cc/en/uploads/Tutorial/LCD_bb.png&imgrefurl=http://arduino.cc/en/Tutorial/LiquidCrystal&h=621&w=1012&sz=37&tbnid=VU4yq9GTv30ICM:&tbnh=74&tbnw=120&zoom=1&usg=__pYcaxlGGoM7mwJDCZRWC4goxsQs=&docid=xsJV83MIGq37lM&hl=en&sa=X&ei=sa5YUO-MK4e20QXw84HgAw&ved=0CCMQ9QEwAA&dur=303

After uploading the code for the lcd display, the lcd works perfectly ( but obviously the ultrasonic sensor isnt working) . In addition, when uploading the code for the ultrasonic sensor, the ultrasonic sensor works perfectly (on the serial monitor) but the lcd is obviously not working..


The problem i am having is that i do not know how to combine the two codes to make the reading of the ultrasonic sensor work on the LCD display . Can somebody please help me with this issue.. I have posted the seperate codes for the ultrasonic sensor and the lcd  below :


CODE FOR ULTRASONIC SENSOR :

/* Ping))) Sensor
   
   This sketch reads a PING))) ultrasonic rangefinder and returns the
    distance to the closest object in range. To do this, it sends a pulse
    to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
     
    The circuit:
     * +V connection of the PING))) attached to +5V
     * GND connection of the PING))) attached to ground
     * SIG connection of the PING))) attached to digital pin 7
 
   http://www.arduino.cc/en/Tutorial/Ping
   
    created 3 Nov 2008
    by David A. Mellis
    modified 30 Aug 2011
    by Tom Igoe
 
    This example code is in the public domain.
 
 */
 
// this constant won't change.  It's the pin number
 // of the sensor's output:
 const int pingPin = 7;
 
void setup() {
   // initialize serial communication:
   Serial.begin(9600);
 }
 
void loop()
 {
   // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
   long duration, inches, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
   // pulse whose duration is the time (in microseconds) from the sending
   // of the ping to the reception of its echo off of an object.
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);
 
  // convert the time into a distance
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   
  Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   
  delay(100);
 }
 
long microsecondsToInches(long microseconds)
 {
   // According to Parallax's datasheet for the PING))), there are
   // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
   // second).  This gives the distance travelled by the ping, outbound
   // and return, so we divide by 2 to get the distance of the obstacle.
   // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
   return microseconds / 74 / 2;
 }
 
long microsecondsToCentimeters(long microseconds)
 {
   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
   // The ping travels out and back, so to find the distance of the
   // object we take half of the distance travelled.
   return microseconds / 29 / 2;
 }




CODE FOR LCD  :

Code
 






/*
   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
  * LCD R/W pin to ground
  * 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 22 Nov 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);
 
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);
 }

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi... I was wondering if anyone could help me with the code when connecting an LCD and ultrasonic sensor...
I wired up the ultrasonic sensor on an arduino duemilanove just like the image in the link below.


http://arduino.cc/en/uploads/Tutorial/ping_bb.png

I then wired up a parralel LCD display screen just like in the link below..

http://www.google.co.uk/imgres?imgurl=http://arduino.cc/en/uploads/Tutorial/LCD_bb.png&imgrefurl=http://arduino.cc/en/Tutorial/LiquidCrystal&h=621&w=1012&sz=37&tbnid=VU4yq9GTv30ICM:&tbnh=74&tbnw=120&zoom=1&usg=__pYcaxlGGoM7mwJDCZRWC4goxsQs=&docid=xsJV83MIGq37lM&hl=en&sa=X&ei=sa5YUO-MK4e20QXw84HgAw&ved=0CCMQ9QEwAA&dur=303

After uploading the code for the lcd display, the lcd works perfectly ( but obviously the ultrasonic sensor isnt working) . In addition, when uploading the code for the ultrasonic sensor, the ultrasonic sensor works perfectly (on the serial monitor) but the lcd is obviously not working..


The problem i am having is that i do not know how to combine the two codes to make the reading of the ultrasonic sensor work on the LCD display . Can somebody please help me with this issue.. I have posted the seperate codes for the ultrasonic sensor and the lcd  below :


CODE FOR ULTRASONIC SENSOR :

/* Ping))) Sensor
   
   This sketch reads a PING))) ultrasonic rangefinder and returns the
    distance to the closest object in range. To do this, it sends a pulse
    to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
     
    The circuit:
     * +V connection of the PING))) attached to +5V
     * GND connection of the PING))) attached to ground
     * SIG connection of the PING))) attached to digital pin 7
 
   http://www.arduino.cc/en/Tutorial/Ping
   
    created 3 Nov 2008
    by David A. Mellis
    modified 30 Aug 2011
    by Tom Igoe
 
    This example code is in the public domain.
 
 */
 
// this constant won't change.  It's the pin number
 // of the sensor's output:
 const int pingPin = 7;
 
void setup() {
   // initialize serial communication:
   Serial.begin(9600);
 }
 
void loop()
 {
   // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
   long duration, inches, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
   // pulse whose duration is the time (in microseconds) from the sending
   // of the ping to the reception of its echo off of an object.
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);
 
  // convert the time into a distance
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   
  Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   
  delay(100);
 }
 
long microsecondsToInches(long microseconds)
 {
   // According to Parallax's datasheet for the PING))), there are
   // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
   // second).  This gives the distance travelled by the ping, outbound
   // and return, so we divide by 2 to get the distance of the obstacle.
   // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
   return microseconds / 74 / 2;
 }
 
long microsecondsToCentimeters(long microseconds)
 {
   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
   // The ping travels out and back, so to find the distance of the
   // object we take half of the distance travelled.
   return microseconds / 29 / 2;
 }




CODE FOR LCD  :

Code
 






/*
   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
  * LCD R/W pin to ground
  * 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 22 Nov 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);
 
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);
 }

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46215
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't just mash two sketches together. You need to decide what the new sketch is supposed to do. It is quite simple to take the 2nd sketch you posted, and modify it to define the ping pin, and to read the ping sensor, and to print the distance value on the LCD, if that is what the resulting sketch is supposed to do.
Logged

Ontario
Offline Offline
God Member
*****
Karma: 20
Posts: 835
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A working sketch will have one setup() and one loop(), so you would want to combine the contents of these functions.  The remainder of the sketches you can pretty well combine by just pasting that stuff together into a new sketch.  For example:

combining:

Code:
void setup() {
   // initialize serial communication:
   Serial.begin(9600);
 }

and

Code:
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!");
 }

you might come up with

Code:
void setup() {
   // initialize serial communication:
   Serial.begin(9600);
   // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
   // Print a message to the LCD.
   lcd.print("hello, world!");
 }
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok.. how would you go about doing that... i do not know much about electronics.. i study economics but have to get the work
done for someone in my group who doesnt turn up... hes supposed to be the engineer..

can you please help me with modifying the second sketch.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what do i write to define the ping pin and read the sensor to the lcd screen
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46215
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can you please help me with modifying the second sketch.
I could, but I think that it is important that you review both sketches to understand what they are doing.

The second sketch currently does something. Compare what you see happening with the code, and make sure that you understand what bits of code make that happen.

The first sketch has a block of code that gets data from the ping sensor, and prints some data to the serial port. Make sure you understand which bits of code are getting the data, and which bits are printing it to the serial port.

When you understand both sketches, you will see that it is easy to copy the code to get data from the ping sensor from one sketch to the other. Sending data to the LCD instead of the serial port is trivial
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could you please help me further to combine the two codes.. it seem easy but im trying to cover for an engineer who slacks in our project .
I study economics.. so im totally useless and we are running out of time on the project... All i want is for the lcd to display the distance results which would usually show up
on the arduino environment... It would really be appreciated..

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but i know nothing about electronics.. !! i study economics for crying out loud  lol...

please help mate.. it would be really appreciated.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46215
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but i know nothing about electronics.
This isn't about electronics. It's about coding. I'll tell you what, though. You go through both sketches you posted, and add comments that describe what is happening. I'll review the comments, to see whether they are accurate.

After that, if you still can't figure out what to cut-and-paste to create a single sketch, I'll do it.
Logged

Inland Empire, California, US
Offline Offline
Full Member
***
Karma: 4
Posts: 242
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Every time you upload a new sketch the uploading process wipes the old sketch from memory. So if you want to use both sketches you have to combine them by copying and pasting lines of code into the appropriate sections of the new singular sketch. I would suggest you start with figuring out all the libraries and variables (from both sketches) that first must defined and copy those into one sketch.  Then do the same for the void setup() sections and finally void loop() sections. The two functions in the ultrasonic sketch found after the main loop must also be included at the bottom. On a side note, posting code using the code tag ( # ) makes it easier for others to read. For example:
Blink:
Code:
int led = 13;

void setup() {               
  pinMode(led, OUTPUT);     
}

void loop() {
  digitalWrite(led, HIGH);   
  delay(1000);               
  digitalWrite(led, LOW);   
  delay(1000);               
}

Serial:
Code:
void setup() {
  Serial.begin(9600);
  }

void loop() {
   Serial.println("Hello World");
  delay(1000);        // delay in between reads for stability
}

A Serial /Blink Combination:
Code:
int led = 13;

void setup() {               
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  Serial.println("Hello World"); 
}

void loop() {
  digitalWrite(led, HIGH);
   Serial.println("LED is ON"); 
  delay(1000);               
  digitalWrite(led, LOW); 
   Serial.println("LED is OFF");
  delay(1000);               
}
« Last Edit: September 18, 2012, 02:10:53 pm by seanz2003 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Built one a few days ago, this works for me:

Code:
#include <LiquidCrystal.h>
#define trigPin 8; //Define the Trigger pin of the Ultrasonic
#define echoPin 9; //Define the Echo pin of the Ultrasonic

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Define LCD pins

void setup() {
  lcd.begin(16, 2); //Setup LCD
  pinMode(trigPin, OUTPUT); //Setup Trigger and Echo
  pinMode(echoPin, INPUT);
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH); //Write High for 1ms
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); //Time pulse in
  distance = (duration/2) / 29.1; //Convert time to Cm
  if (distance >= 200 || distance <= 0){
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Out of Range");
  }
  else {
    lcd.clear();
    lcd.setCursor(2,0); //Print to screen
    lcd.print(distance);
    lcd.setCursor(5,0);
    lcd.print(" Cm");
  }
  delay(300);
}

Hope this helps,
Owen
Logged

Pages: [1] 2   Go Up
Jump to: