will someone look at this sketch and tell me where i messed up

 /* YourDuino.com Example Software Sketch
 20 character 4 line I2C Display
 Backpack Interface labelled "LCM1602 IIC  A0 A1 A2"
 terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <LiquidCrystal_I2C.h>
#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

/*-----( Declare Constants )-----*/
/*-----( Declare objects )-----*/
// set the LCD address to 0x20 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
#include <NewPing.h>

#define SONAR_NUM     3 // Number or sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 1000 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(53, 52, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(51, 50, MAX_DISTANCE),
  NewPing(49, 48, MAX_DISTANCE),

/*-----( Declare Variables )-----*/

void setup()   /*----( SETUP: RUNS ONCE )----*/
  Serial.begin(9600);  // Used to type in characters
 while (!Serial) ; // wait until Arduino Serial Monitor opens
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
     Serial.println("RTC has set the system time");  
  lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines and turn on backlight

// ------- Quick 3 blinks of backlight  -------------
  for(int i = 0; i< 3; i++)
  lcd.backlight(); // finish with backlight on  
//-------- Write characters on the display ----------------
// NOTE: Cursor Position: CHAR, LINE) start at 0  
  lcd.setCursor(3,0); //Start at character 4 on line 0
  lcd.print("Hello, world!");
  lcd.print("From YourDuino");
  lcd.print("20 by 4 Line Display");

  lcd.setCursor(0,0); //Start at character 0 on line 0
  lcd.print("Start Serial Monitor");
  lcd.print("Type chars 2 display");   
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;

}/*--(end setup )---*/

void loop() { 
  if (timeStatus() == timeSet) {
  } else {
    Serial.println("The time has not been set.  Please run the Time");
    Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(" ");
  Serial.print(" ");
  Serial.print(" ");

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  if(digits < 10)
   for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
  // The rest of your code would go here.

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print("cm ");

Not to steal Paul S. 's thunder but...

Your code is supposed to do something. It would really help if you shared what that something was.

Your code is obviously doing something else, or you wouldn't be posting it here. It would really help if you shared that as well.

Nobody here is going to go through all of that code and try to figure out what you want it to do and try to find the mistake hidden in it. Tell us what is going wrong and someone can probably help you track down the relevant bit of code.

You somehow didn't get the code inside the code tags. But you did at least try. Edit your first post (the Modify option) and put the end tag [/code] at the end of the code.


sorry about it not posting correctly i had this working correctly and my computer messed up and lost the working sketch it is supposed to write to the serial monitor the time and ping sensors readings but now the way i have done it when the ping reading is supposed to print it starts off with the hour then inserts the ping reading then the rest of the minutes and date i know i just made a mistake in the order of things but am a newb at this thanks for any help James

You messed up by not reading the first post in the thread titled

Read this before posting a programming question ...


Once you have the code up IN TAGS it would also be good if you added comments showing WHAT YOU CHANGED as well as a link to the original which did work for you or not?

Thanks for fixing the code tag problem. I think the problem is that your printDigits function not only prints the digits for one integer, it also then checks the ping status, during which it can call oneSensorCycle which prints the results of one cycle. I think you need to take the code which checks the ping status and probably put it in the loop after you call digitalClockDisplay(). But you'll have to check your code to make sure that it is the logical/correct place to put it.


thank you for pointing me in the right direction it is working the way i want it to now ! thanks james