Ultrasonic Sensor and RTC

Folks:

I have a SR04 and RTC to display time, date and distance in sequentail order with some delay .
My code returns with incorrect distance , can you pls help in fixing it ?

my code as below:

/* Include the Wire,HCMAX7219 and SPI library */
#include <Wire.h>
#include <HCMAX7219.h>
#include "SPI.h"
#include <RTClib.h>
#include <NewPing.h>

#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 9 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // 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.

/* Create an instance of the library */
#define LOAD 10
// Timers with out dealy
unsigned long tmils = 0; // will store last time time was updated

const long tintrvl = 18000; // interval at which to All to change
RTC_DS1307 RTC;
HCMAX7219 HCMAX7219(LOAD);

void setup()
{
while (!Serial); // for Leonardo/Micro/Zero

Serial.begin(9600);
Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
}
RTC.adjust(DateTime(F(DATE), F(TIME)));
// RTC.adjust(DateTime(2015,10, 5, 9, 10, 0));

}

void loop()

{
unsigned long tcmils = millis();
if (tcmils - tmils >= tintrvl) {
tmils=tcmils;
}
long cgap= tcmils - tmils;

if (cgap <=6000 ){
ptime();
}
else if (cgap >6000 && cgap <=9000) {
pdate();
}

else {
pdist();
}
}
void ptime()
{
Wire.beginTransmission(0x68);
Wire.write(0); // location pointer
Wire.endTransmission();
Wire.requestFrom(0x68, 7); // send 7 bytes
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
// Serial Print date and time
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);

HCMAX7219.Clear();
prints(hour,8);
HCMAX7219.print7Seg("-",6);
prints(minute,5);
HCMAX7219.print7Seg("-",3);
prints(second,2);
HCMAX7219.Refresh();
delay(700);
}

void pdate()
{
Wire.beginTransmission(0x68);
Wire.write(0); // location pointer
Wire.endTransmission();
Wire.requestFrom(0x68, 7); // send 7 bytes
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
// Serial Print date and time
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);

HCMAX7219.Clear();
prints(monthDay,8);
HCMAX7219.print7Seg(".",6);
prints(month,5);
HCMAX7219.print7Seg(".",3);
prints(year,2);
HCMAX7219.Refresh();
delay(700);
}

void pdist()
{
delay(50); // 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 in cm and print result (0 = outside set distance range)
Serial.println("cm");

HCMAX7219.Clear();
HCMAX7219.print7Seg("Level:",8);
prints((uS / US_ROUNDTRIP_CM),2);
HCMAX7219.Refresh();
delay(700);
}

//Print data in Display
void prints(int data, int pos) {
if (data<=9){
HCMAX7219.print7Seg("0",pos);
HCMAX7219.print7Seg(data,--pos);
}
if (data>9){
HCMAX7219.print7Seg(data,pos);
}
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val) {
return ( (val/16*10) + (val%16) );
}

My initial guess would be a timer conflict - NewPing uses Timer2 - what do the other libraries use, would be the question...

Looks like only Newping only uses timers, <HCMAX7219.h> does not seem to use any timers. Thanks.

I just knocked off the 7219 Library and relevant codes , still it gets only incorrect data for distance.

I got that fixed . using class and 7219 Lib Turn on and off , I am able get what I want .

the code is as below :

/* Include the Wire,HCMAX7219 and SPI library */
#include <Wire.h>
#include <HCMAX7219.h>
#include <RTClib.h>
#include <NewPing.h>
#include <SPI.h>

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

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
RTC_DS1307 RTC; //RTC Instance
HCMAX7219 HCMAX7219(LOAD); //HC max instance

class distance
{
unsigned int dist;
long OnTime; // milliseconds of on-time
//unsigned long previousMillis; // will store last time LED was updated
public:
distance (long on)
{

OnTime = on;
//previousMillis = 0;
}

void update()
{
unsigned int dist = sonar.ping_cm(); // Send ping, get ping distance in CM.
HCMAX7219.Shutdown(MAX7219ON, 0);
HCMAX7219.Clear();
HCMAX7219.print7Seg("Level:", 8);
prints(dist, 3);
HCMAX7219.Refresh();
delay(4000);
HCMAX7219.Shutdown(MAX7219OFF, 0);
//}
}
//Print data in Display
void prints(int data, int pos) {
if (data <= 9) {
HCMAX7219.print7Seg("0", pos);
HCMAX7219.print7Seg(data, --pos);
}
if (data > 9) {
HCMAX7219.print7Seg(data, pos);
}

}
};

class pdate
{
long OnTime; // milliseconds of on-time
public:
pdate (long on)
{
OnTime = on;
}

void update()
{
Wire.beginTransmission(0x68);
Wire.write(0); // location pointer
Wire.endTransmission();
Wire.requestFrom(0x68, 7); // send 7 bytes
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
HCMAX7219.Clear();
prints(monthDay, 8);
HCMAX7219.print7Seg(".", 6);
prints(month, 5);
HCMAX7219.print7Seg(".", 3);
prints(year, 2);
HCMAX7219.Refresh();
HCMAX7219.Shutdown(MAX7219OFF, 0);

}
//Print data in Display
void prints(int data, int pos) {
if (data <= 9) {
HCMAX7219.print7Seg("0", pos);
HCMAX7219.print7Seg(data, --pos);
}
if (data > 9) {
HCMAX7219.print7Seg(data, pos);
}
}

};

class ptime
{
long OnTime; // milliseconds of on-time
unsigned long previousMillis2; // will store last time LED was updated
public:
ptime (long on)
{
OnTime = on;
}

void update()
{
unsigned long currentMillis2 = millis();
previousMillis2 = millis();
while (currentMillis2 - previousMillis2 <= OnTime)
{
currentMillis2 = millis();
Wire.beginTransmission(0x68);
Wire.write(0); // location pointer
Wire.endTransmission();
Wire.requestFrom(0x68, 7); // send 7 bytes
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());

HCMAX7219.Shutdown(MAX7219ON, 0);
HCMAX7219.Clear();
prints(hour, 8);
HCMAX7219.print7Seg("-", 6);
prints(minute, 5);
HCMAX7219.print7Seg("-", 3);
prints(second, 2);
HCMAX7219.Refresh();
HCMAX7219.Shutdown(MAX7219OFF, 0);
}

}
//Print data in Display
void prints(int data, int pos) {
if (data <= 9) {
HCMAX7219.print7Seg("0", pos);
HCMAX7219.print7Seg(data, --pos);
}
if (data > 9) {
HCMAX7219.print7Seg(data, pos);
}
}

};

distance sr04(5000);
pdate pdate(5000);
ptime ptime(15000);

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val) {
return ( (val / 16 * 10) + (val % 16) );
}

//Print data in Display
void prints(int data, int pos) {
if (data <= 9) {
HCMAX7219.print7Seg("0", pos);
HCMAX7219.print7Seg(data, --pos);
}
if (data > 9) {
HCMAX7219.print7Seg(data, pos);
}
}
void setup()
{
while (!Serial); // for Leonardo/Micro/Zero

Serial.begin(9600);
Wire.begin();
RTC.begin();

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
}
RTC.adjust(DateTime(F(DATE), F(TIME)));
// RTC.adjust(DateTime(2015,10, 5, 9, 10, 0));
}

void loop()

{
HCMAX7219.Shutdown(MAX7219OFF, 0);
sr04.update( );
//delay(3000);
HCMAX7219.Shutdown(MAX7219OFF, 0);
pdate.update();
HCMAX7219.Shutdown(MAX7219ON, 0);
delay(3000);
ptime.update();
HCMAX7219.Shutdown(MAX7219ON, 0);
delay(100);

}

Does your code REALLY have smiley faces in it? Read the stickies at the top of the forum - the ones that you were supposed to read BEFORE you blundered in here posting code incorrectly.