DMD with an sr04 ping sensor

The Dot Matrix Display (DMD) is a 32x16 array of high-brightness LEDs for visually striking effects. [Product Page]
coromonadalix
Posts:26
Joined:Wed Apr 09, 2014 9:03 pm
Re: DMD with an sr04 ping sensor

Post by coromonadalix » Wed Apr 16, 2014 2:36 am

completly stuck to how i can add the strings mentioned ...

all my other sketchs failed, and did not compile at all.


I am trying to find a few examples who could show me that,

the one who seem promising: was some one who tried to talk to the DMD via serial terminal ...

help ?

angusgr
Freetronics Staff
Freetronics Staff
Posts:853
Joined:Tue Apr 09, 2013 11:19 pm
Location:Melbourne, Australia
Contact:

Re: DMD with an sr04 ping sensor

Post by angusgr » Wed Apr 16, 2014 4:53 am

Hi coromonadalix,

Sorry it's still not behaving for you.

I haven't tested this code, but it should be similar to this:

Code: Select all

String output(inches);  // Build the output in these two lines
output += " po, " + cm + " cm";
char buf[32];
output.toCharArray(buf, 32); // Copy it to a temporary buffer
dmd.drawString( 0,0, buf, strlen(buf), GRAPHICS_NORMAL ); // Display the buffer
There are some updates coming to the DMD library soon that will make this kind of a thing a bit simpler to achieve.

coromonadalix
Posts:26
Joined:Wed Apr 09, 2014 9:03 pm

Re: DMD with an sr04 ping sensor

Post by coromonadalix » Wed Apr 16, 2014 11:10 am

thanks ill try that ... ill give you some feed back.

It would be nice to have some kind of interface between an arduino and the dmd display, like an spi interface who could mimic an graphical lcd library ... something like that, to ease the communication ??? since we have powerful arduinos like a teensy 3.1 who came out not long ago ?

coromonadalix
Posts:26
Joined:Wed Apr 09, 2014 9:03 pm

Re: DMD with an sr04 ping sensor

Post by coromonadalix » Wed Apr 16, 2014 1:38 pm

woah almost there i have changed po for in (inches) just to be in english.


I have both cm and po displayed, but only the po values are showings, but the value blink at 1 hz ??? goes up to 4 digits if i move to fast the ping sensor.

The terminal windows is still working.

I had to coment out the line // output += "po," + cm + "cm"; it was giving an error : invalid operands of types "const char" and 'const char [4]' to binary 'operator +' ??? never saw that error.

But if i use this : output += "po"; i have ex: "po 46po" or ex: "po 1148po" displaying but blinking at about 1hz

If i use : output += "po";
output += "cm";

I have as display ex: "po 1146pocm " displayed

i think the drawstring with the duffer seems only picking the inches values

very funny to see loll


but i ended to commented out this line : //output += " po, " + cm + " cm"; jsu to have an blinking inches values ill try to understand if i can how to catch the cm values.

thanks again for your help


i whant to use this project in my garage to tell me how my car is near the wall, and park a second one behind it, to close my garage door, i have a 2 place "inline car" garage, i saw a laser pointer doing a cross hatch on the hood, but i dont whant to mess with a laser and have reflections in my eyes. At least for now ???


EDIT : ok the 1hz flashing is gone by removing the delay 1000 after the 2 drawstring in and cm sections.
my actual code :

#include <SPI.h>
#include <DMD.h>
#include <TimerOne.h>
#include "SystemFont5x7.h"
#include "Arial_black_16.h"

const int triggerPin = 2; // was pin 12 defore the DMD
const int echoPin = 3; // was pin13 before the DMD

#define DISPLAYS_ACROSS 2 // i whant to display it on 16x64 pixels
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);

void ScanDMD()
{
dmd.scanDisplayBySPI();
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// *** THIS NEEDS TO BE CHECKED FOR THE HC-SR04 ***
return microseconds / 29 / 2;
}


void setup()
{
// initialize serial communication for the terminal windows.
Serial.begin(9600);

Timer1.initialize( 5000 );
Timer1.attachInterrupt( ScanDMD );
dmd.clearScreen( true );
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The device is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(5);
digitalWrite(triggerPin, LOW);

// The echo pin is used to read the signal from the device: 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(echoPin, INPUT);
duration = pulseIn(echoPin, 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(1000);


dmd.clearScreen( true );

dmd.selectFont(System5x7);
dmd.drawString( 1,0, "in", 2, GRAPHICS_NORMAL );
dmd.drawString( 1,8, "cm", 2, GRAPHICS_NORMAL );

// delay(1000); // if on will cause an 1 hz blinking value dispalyed

String output(inches); // Build the output in these two lines
//output += "po";
//output += " po, " + cm + " cm";
char buf[32];
output.toCharArray(buf, 32); // Copy it to a temporary buffer
dmd.drawString (15,0, buf, strlen(buf), GRAPHICS_NORMAL ); // Display the buffer
}

coromonadalix
Posts:26
Joined:Wed Apr 09, 2014 9:03 pm

Re: DMD with an sr04 ping sensor

Post by coromonadalix » Wed Apr 16, 2014 1:56 pm

if i add thoses lines and comment out 1 of the 2 strings sections, i can switch between the cm or the inches displayed.

if i dont do that, the arduino ide is complaining of strings output redeclaration ...

Again i am poor knowledged in arduino strings ...

//String output(inches); // Build the output in these two lines
//output += "po";
//output += " po, " + cm + " cm";
//char buf[32];
//output.toCharArray(buf, 32); // Copy it to a temporary buffer
//dmd.drawString (15,0, buf, strlen(buf), GRAPHICS_NORMAL ); // Display the buffer

String output(cm); // Build the output in these two lines
//output += "po";
//output += " po, " + cm + " cm";
char buf[32];
output.toCharArray(buf, 32); // Copy it to a temporary buffer
dmd.drawString (15,8, buf, strlen(buf), GRAPHICS_NORMAL ); // Display the buffer

coromonadalix
Posts:26
Joined:Wed Apr 09, 2014 9:03 pm

Re: DMD with an sr04 ping sensor

Post by coromonadalix » Fri Apr 18, 2014 1:14 pm

once again im stuck with how to add the two strings together, at least it work at 90% of what i need.

User avatar
stryker
Posts:282
Joined:Sat Jan 14, 2012 2:44 pm

Re: DMD with an sr04 ping sensor

Post by stryker » Fri Apr 18, 2014 1:56 pm

coromonadalix wrote:once again im stuck with how to add the two strings together, at least it work at 90% of what i need.
What two strings are you trying to add together?

Variables of the String object type can be added together with the operator +. There's an explanation of the various methods here.

Cheers ! Geoff

coromonadalix
Posts:26
Joined:Wed Apr 09, 2014 9:03 pm

Re: DMD with an sr04 ping sensor

Post by coromonadalix » Fri Apr 18, 2014 5:38 pm

hi Geoff

I am trying to add those 2 sections : But the ide dont compile and say redeclaration of the "StringOutput" i was trying to understand this line : output += " po, " + cm + " cm";

Thanks for the tips.

String output(inches);
//output += " po, " + cm + " cm";
char buf[32];
output.toCharArray(buf, 32);
dmd.drawString (15,0, buf, strlen(buf), GRAPHICS_NORMAL );

String output(cm); // Build the output in these two lines
//output += " po, " + cm + " cm";
char buf[32];
output.toCharArray(buf, 32);
dmd.drawString (15,8, buf, strlen(buf), GRAPHICS_NORMAL );

Actually i use the cm section for more precision than inch, but for the fun of it, since the actual code is displaying the cm and inche on the dmd and showing only the cm values, i am trying to finish the missing inch values.

I am also trying to understand how the buffer works, i dont catch how we are able to call cm or inch, but not the two values ??? since we allow enough size and length .

my values for inch and/or cm are 1 digit to 4 digits ...

User avatar
stryker
Posts:282
Joined:Sat Jan 14, 2012 2:44 pm

Re: DMD with an sr04 ping sensor

Post by stryker » Sat Apr 19, 2014 10:15 am

Hi

The error "redeclaration of 'String output' " is because you have these two lines in your loop()

Code: Select all

String output(inches);
// and later
String output(cm);
They both declare a variable of type String, of the name output and that's not allowed within the same variable scope (ie the function loop()).

If you want to eliminate the error, change the 2nd declaration to something like this

Code: Select all

output =String(cm);
You will also get an error due to the buffer being delcared twice too they way you have this.

However, you don't need it. A single block of your code is already outputting the inches then cm values. I did find that your concatenation of values into output caused errors if done in one step, so I broke it up and it's all fine. Don't be afraid to throw in lots of Serial.print()s to show what's going on.

Code: Select all

  String output(inches);
  output += " in, ";
  output += cm;
  output += " cm"; 
  char buf[32];
  output.toCharArray(buf, 32);
  dmd.drawString (0,0, buf, strlen(buf), GRAPHICS_NORMAL );
  Serial.println(output);
results in (for me, with no Ping sensor)

Code: Select all

0 in, 0 cm
0 in, 0 cm
0 in, 0 cm
0 in, 0 cm
0 in, 0 cm
0 in, 0 cm
0 in, 0 cm
0 in, 0 cm
And so on...

So you know what's going on, let me step you through this. Firstly a new variable of type String is declared, and also initialised as containing the string representation of the value of variable 'inches' (in my case, always 0 but in yours the most recent distance measured in inches

Code: Select all

  String output(inches);
Then in the next 3 lines, we concatenate to the end of that String, two additional string constants " in, " and " cm" as well as the contents of the variable 'cm'

Code: Select all

  output += " in, ";
  output += cm;
  output += " cm"; 
In my case without a ping sensor attached, cm always contains 0's however in your case you'll have a String containing everything you wanted to now. Next a new variable array, length 32, of type char is declared

Code: Select all

char buf[32];
And we use the function toCharArray which is part of the inbuilt functionality of the StringObject type to fill the buf[] array with the contents of output

Code: Select all

output.toCharArray(buf, 32);
That's really all there is to it.

Now I didn't test this with a DMD attached, so that's your next challenge. I don't expect that all that will fit across your two DMD array...

Here's the entire code for you again (I have moved a few things, but nothing that was breaking the code)

Code: Select all

#include <SPI.h> 
#include <DMD.h> 
#include <TimerOne.h> 
#include "SystemFont5x7.h"
#include "Arial_black_16.h"

const int triggerPin = 2; // was pin 12 defore the DMD
const int echoPin = 3; // was pin13 before the DMD

#define DISPLAYS_ACROSS 2 // i whant to display it on 16x64 pixels
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);

void ScanDMD() { 
  dmd.scanDisplayBySPI(); 
}

long microsecondsToInches(long microseconds) {
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // *** THIS NEEDS TO BE CHECKED FOR THE HC-SR04 ***
  return microseconds / 29 / 2;
} 


void setup() {
  // initialize serial communication for the terminal windows.
  Serial.begin(9600);

  // setup DMD refreshing
  Timer1.initialize( 5000 ); 
  Timer1.attachInterrupt( ScanDMD ); 
  // initialise DMD as blank, all LEDs off
  dmd.clearScreen( true ); 
  // choose a font to use
  dmd.selectFont(System5x7);

  // set up pins for ultrasonic sensor
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The device is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(triggerPin, LOW);

  // The echo pin is used to read the signal from the device: 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.
  duration = pulseIn(echoPin, 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();

  dmd.clearScreen( true );

  String output(inches);
  output += " in, ";
  output += cm;
  output += " cm"; 
  char buf[32];
  output.toCharArray(buf, 32);
  dmd.drawString (0,0, buf, strlen(buf), GRAPHICS_NORMAL );
  Serial.println(output);

  delay(1000); // if on will cause an 1 hz blinking value dispalyed
}
Hopefully this gets you within striking distance of what it is you wanted to achieve. All the best! Geoff

coromonadalix
Posts:26
Joined:Wed Apr 09, 2014 9:03 pm

Re: DMD with an sr04 ping sensor

Post by coromonadalix » Tue Apr 22, 2014 1:43 pm

hello thanks for the code,

It is working only if i use a minimun of 3 dmd wide display, it is outputting on 1 line only .... since the digits are 1 to 4 digits in length (each values). It begins at 2 dmd wide and goes up to 3 dmd wide if i move suddenly the sensor while taking a measurement.

where getting near

Im trying to figure how to push 2 strings on 2 different sections of the dmd.

I am learning the arduino string documentations, the +=, concatenations ....

again im trying to understand the "buffer" because it seem to call all the cm and inches values ... but how to separate them and send them to their respective places.

the dmd.drawString needs to be ascociated to a string ex: cm
and the other dmd.drawString to the inche values ...

Yet i havent found an example for this, but i am searching thru this forum for any hints.


Maybe I could use bigger font like an 14x 10 font (twice the size of a 5x7) ???
and a least 4 dmd wide.

thanks


by the way you were talking of a new DMD library, do you have an idea when it will come out ?

Post Reply