Hello everyone, I'm having a slight problem with a setup I'm working with.
I'm trying to make a ultrasonic measure tape using a US-020 ultrasonic sensor and displaying it on a 4-digit 7-seg display driven by a MAX7219, I'm using the NewPing and LedControl libraries.
My problem is that when I use the NewPing sample the measurement is correct but when I add the code the display the measurement on the 7-seg the measurement is incorrect, by a factor of 1 meter and going up & down all the time (the same happens in the serial monitor and the display and the result is the same even if I remove the serialPrint command, if it matters) .
Has anyone had a similar problem?
Thanks
Oscar
#include <NewPing.h>
#include <LedControl.h>
#define TRIGGER_PIN 36 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 37 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 600 // Maximum distance we want to ping for (in centimeters).
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
LedControl lc = LedControl(5, 6, 7, 0);
void setup() {
lc.shutdown(0, false); // turns on display
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void printNumber(float num)
{
int ones;
int tens;
int hundreds;
int thousands;
int v=(int)num;
ones=v%10;
v=v/10;
tens=v%10;
v=v/10;
hundreds=v%10;
v=v/10;
thousands=v%10;
v=v/10;
//Now print the number digit by digit
lc.setDigit(0,0,(byte)thousands,false);
lc.setDigit(0,1,(byte)hundreds,false);
lc.setDigit(0,2,(byte)tens,false);
lc.setDigit(0,3,(byte)ones,false);
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
sonar.ping_median(5);
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");
printNumber(uS / US_ROUNDTRIP_CM);
}
You are saying that you get the correct answer on the serial monitor when you are not using the LCD ?
And then, when you add the LCD, the answer is then wrong in both places ?
Your code is rather flakey in its use of integer and float data, but that maybe isn't your problem. The calculation of the argument to the printNumber() function is not necessarily a float. There may be an implicit conversion but you had better verify that is the case.
Yes, that's what happens, the code I used to display the data on the 7-seg display is what I could find from another similar project, as I'm not familiar with data conversion yet.
But I am able to display other numbers correctly, for example display the analogue read of a potentiometer, using the same code ( same code, commented out the NewPing library parts).
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
//#include <NewPing.h>
#include <LedControl.h>
/*#define TRIGGER_PIN 36 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 37 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 600 // 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.
LedControl lc = LedControl(5, 6, 7, 0);
int POT = A2;
int POT_reading = 0;
int Volt = 0;
void setup() {
lc.shutdown(0, false); // turns on display
lc.setIntensity(0, 15); // 15 = brightest
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void printNumber(float num)
{
int ones;
int tens;
int hundreds;
int thousands;
int v=(int)num;
ones=v%10;
v=v/10;
tens=v%10;
v=v/10;
hundreds=v%10;
v=v/10;
thousands=v%10;
v=v/10;
//Now print the number digit by digit
lc.setDigit(0,0,(byte)thousands,false);
lc.setDigit(0,1,(byte)hundreds,true);
lc.setDigit(0,2,(byte)tens,false);
lc.setDigit(0,3,(byte)ones,false);
}
void loop() {
/*delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
sonar.ping_median(5);
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");
printNumber(uS / US_ROUNDTRIP_CM);*/
POT_reading = analogRead(POT);
Volt = POT_reading*0.48;
printNumber(Volt);
Serial.print(Volt);
Serial.print("Volt");
}
I get the same problem, I can see the data perfectly but as soon as I implement the displaying of the digits, my values become erratic and wrong. I tried removing the serial communication thinking maybe there are too many serial communications (MAX7219 uses Serial) but I still get funky values
I attached my code:
#include <Ultrasonic.h>
#include "LedControl.h"
LedControl lc=LedControl(12,11,10,4);
#define TRIGGER_PIN 7
#define ECHO_PIN 6
int ledPin = 9;
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
void setup()
{
//Serial.begin(9600);
lc.shutdown(0,false);
/* Set the brightness to a medium values*/
lc.setIntensity(0,8);
/* and clear the display */
lc.clearDisplay(0);
}
void loop()
{
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
/* Serial.print("MS: ");
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);*/
delay(50);
//casting the float to int
int intCmSec = (int) cmMsec;
//writing to display
lc.setDigit(0,0,intCmSec/100,false);
lc.setDigit(0,1,(intCmSec%100)/10,false);
lc.setDigit(0,2,(intCmSec%100)%10,false);
}
I noticed that when I uploaded another sketch the measurements were still off although that sketch didn't use the 4 digit 7 segment display, but the display was still lit from the previous sketch. So I figured it was a power issue... Then I got a 5V regulator (LM7805) and used that with a 9V to give the circuit better current and now it works like a charm hope this helps!