Merging LCD Bargraph and ultrasonic sensor

Hey everyone!

It would be much appreciated if you can help me merge these two pieces of code to run the LCD display with this (HC-SR04) ultrasonic sensor.

LCD bargraph example


#include <LiquidCrystal.h>
#include <LcdBarGraph.h>

byte lcdNumCols = 16;
byte sensorPin = 0; //

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LcdBarGraph lbg(&lcd, lcdNumCols); /

void setup(){

lcd.begin(2, lcdNumCols);
lcd.clear();

delay(100);
}

void loop()
{

lbg.drawValue( analogRead(sensorPin), 1024);

delay(100);
}


ultrasonic sensor code


#include <NewPing.h>

#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 10 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 475 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
delay(2000); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}


cheers for the help if you can. if you cant dont worry.

i just want to display the sensor readings on a little LCD graph depending on how far away the reading is.

Have you tried to simply copy and paste the parts into there proper places. Setup with setup and loop with loop, everything else is at the top.

Hi, Merging two working sketches can be difficult. Best to stay really organized. See the http://ArduinoInfo.Info WIKI:

http://arduino-info.wikispaces.com/ArduinoSoftware and read down to the bottom:
http://arduino-info.wikispaces.com/CombiningArduinoSketches for some suggestions.

Anyone: Critique, suggestions, comments on this WIKI page would be appreciated.

Thanks for the replies. I will look into it. Simple copy and pasting is too hard.

ive tried my best at doing it and it just dosent work.

I just need it to display for every 0.1c it will add 1byte to the graph so say it was 30

lcd display


[[[[[[[[[[[[[[[[[[[[[[[[

Temp is 30*c


lcd display


[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[

Temp is 50*c


if anyone can help me do this id would be so much appreciated.

Kind regards

Lewis

Where it says temp i mean distance and *cm
so every 1cm is displays 1 byte on lcd display

EDIT, I was able to test it and it works now.

Ok, then use a FOR loop. You do know your lcd is only 2 by 16 right, so the max you can have is 32 bytes, unless you make it display a character every 2cm.
This should print a bar across both rows as temp increases or decreases.
Note: this is not tested, so if it does not compile, just check the closing brackets.

This goes in your loop(), at the end.

int count = 0; // DO NOT KEEP THIS HERE, set globally at the top
if(count < temp) { 
   for(count; count < temp; count++) 
   {
     if(count < 16)
     {
      lcd.setCursor(count, 0);
      lcd.print("]");
     }
    else {
      lcd.setCursor(count - 16, 1);
      lcd.print("]");
    }
   }
 }
else {
 for(count; count >= temp; count--) 
   {
     if(count < 16)
     {
      lcd.setCursor(count, 0);
      lcd.print(" "); // you want to clear what was put on
     }
    else 
     {
      lcd.setCursor(count - 16, 1);
      lcd.print(" "); // you want to clear what was put on
     }
  }
}

I made a minor edit

I was able to test the code I wrote earlier and I had the set cursor backwards, it should work like a charm now.