Sonar: hc-sr40 OpenSegment Serial Display - 20mm (Blue) COM-11647 Display ISSUES

Hardware:
Arduino uno
Sonar: hc-sr40
OpenSegment Serial Display - 20mm (Blue) COM-11647

Issues: Cannot figure out how to print from right to left. Also IF statements aren’t displaying properly i would like nothing to display if inches is < 0 or < 157. Any Help would be appreciated.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8); //RX pin, TX pin

int pingPin = 13;
int inPin = 12;
 
void setup() {
  
  Serial.begin(9600);
  //Serial.println("OpenSegment Example Code");
  mySerial.begin(9600); //Talk to the Serial7Segment at 9600 bps
  mySerial.write(0x7A); // Brightness control command
  mySerial.write(75); // 0% is dimmest, 100% is brightest
  mySerial.write('v');
}
 
void loop(){
  
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): 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(inPin, INPUT);
  duration = pulseIn(inPin, HIGH);
 
  // convert the time into a human readable distance
  inches = 28-microsecondsToInches(duration);
  //cm = microsecondsToCentimeters(duration);
  
  // if distance is too large (probably not being measured properly)
  // will discard the result.
  if (inches >= 157)
  {
    mySerial.write(0x76);
    mySerial.println(0000);
  }
  else if (inches <= 0)
  {
    
    mySerial.write(0x76);
    mySerial.println(0000);
  }
 else
  mySerial.write(0x76);
  mySerial.write(0x80);
  mySerial.write(0x03);
  mySerial.print(inches);
 
 delay(2000);
  
}
 
long microsecondsToInches(long microseconds){
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  return microseconds  / 74 / 2;}
 
//long microsecondsToCentimeters(long microseconds){
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  //return microseconds / 29 / 2;}

djjester611:
Cannot figure out how to print from right to left. Also IF statements aren't displaying properly

Hi, I've never used that display, but I have some suggestions for you to try:

When you say "print from right to left" what do you mean? Did you mean right-justify the number displayed so that the units digit is always on the extreme right segment? If so, search the Arduino site for "sprintf()".

As for your IF statements, I assume what you see right now is that "0000" is displayed when the distance is too large. If so, just take out the "mySerial.println(0000);" I think, or maybe try "mySerial.println('----');". The code "mySerial.write(0x76);" will have cleared the display, if I read the instructions correctly.

Also, I'm not sure your code to set the brightness is correct. You could try "MySerial.write((byte) 75);". I can't figure out what the "v" is for.

Your code also has commands "mySerial.write(0x80);" and "mySerial.write(0x03);". What were these intended to do? The manual I read said they were commands to set the I2C address.

Paul

figured it out with the cycles demo sketch just modded it to fit what i was looking for. Although i cannot get it to default the upper action limits and lower action limits to print 0000’s or ----

#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8); //RX pin, TX pin

int pingPin = 13;
int inPin = 12;
 
void setup() {
  
  Serial.begin(9600);
  //Serial.println("OpenSegment Example Code");
  mySerial.begin(9600); //Talk to the Serial7Segment at 9600 bps
  mySerial.write(0x7A); // Brightness control command
  mySerial.write(75); // 0% is dimmest, 100% is brightest
  mySerial.write('v');
}
 
void loop(){
  
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): 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(inPin, INPUT);
  duration = pulseIn(inPin, HIGH);
 
  // convert the time into a human readable distance
  inches = 28-microsecondsToInches(duration);
  //cm = microsecondsToCentimeters(duration);
  
  // if distance is too large (probably not being measured properly)
  // will discard the result.
  if (inches > 157 && inches < 0)
  {
   // mySerial.write(0x76);
    mySerial.println('0000');
  }
  else 
{
  char inchesString[10]; //Used for sprintf
  sprintf(inchesString, "%4d", inches); //Convert deciSecond into a string that is right adjusted
  //sprintf(tempString, "%d", cycles); //Convert deciSecond into a string that is left adjusted (requires digit 1 command)
  //sprintf(tempString, "%04d", cycles); //Convert deciSecond into a string with leading zeros
  //sprintf(tempString, "%4X", cycles); //Count in HEX, right adjusted
  //int negativeCycles = cycles * -1;
  //sprintf(tempString, "%4d", negativeCycles); //Shows a negative sign infront of right adjusted number

  //Note: This method works well as long as your number is less than or equal to 4 digits.
  //14422 will cause the display to wrap (5 digits)
  //-5766 will cause the display to wrap (5 digits)
  //To fix this, send a 'v' character or look at how to control the digit placement
  //https://github.com/sparkfun/Serial7SegmentDisplay/wiki/Basic-Usage#wiki-cursor

  mySerial.print(inchesString); //Send serial string out the soft serial port to the S7S

  
  //mySerial.write(0x76);//clear display
 
 // mySerial.write(0x80);
 // mySerial.write(0x03);
  //mySerial.print(inches);
}
 delay(1000);
  
}
 
long microsecondsToInches(long microseconds){
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  return microseconds  / 74 / 2;}
 
//long microsecondsToCentimeters(long microseconds){
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  //return microseconds / 29 / 2;}