This is a continuation of my thread from here:
I tried connecting with the circuit in that thread, 220ohm resistors in the TX and RX lines before connecting together. I could not communicate at all with the ultrasonic sensor. (Deleted the code inadvertently so I can't post it). I also tried using NewSoftSerial. While the sensor indicated communication, the data I got back was bogus.
Here is the code I used using SoftwareSerial, which works fine (modified from an example program provided with sensor). It gets the software version from the sensor in the setup, then loops over and over, doing 100 ranges and averaging them. If the distance is less than 10, onboard LED is illuminated.
#include <SoftwareSerial.h>
#define txrxPin 10 // Defines a pin to be used as both rx and tx for the SRF01
#define srfAddress 0x01 // Address of the SFR01
#define getSoft 0x5D // Byte to tell SRF01 we wish to read software version
#define getRange 0x53 // Byte used to get range from SRF01 in inches
#define getStatus 0x5F // Byte used to get the status of the transducer
SoftwareSerial UltrasonicBus(txrxPin, txrxPin);
void setup(){
pinMode(13,OUTPUT);
Serial.begin(19200);
UltrasonicBus.begin(9700);
delay(200); // Waits some time to make sure that SRF01 is powered up
Serial.println("SRF01 Test");
SRF01_Cmd(srfAddress, getSoft); // Calls a function to get the SRF01 software version
int softVer = UltrasonicBus.read(); // Read software version from SRF01
Serial.print("V:");
Serial.println(softVer); // Prints the software version to LCD03
delay(200);
Serial.println("Initialization Complete");
}
void loop(){
int max = 10;
int sum = 0;
for (int count = 0; count < max; count++) {
sum = sum + doRange();
}
int range = sum / max;
Serial.print("Range 100avg = ");
Serial.print(range); // Print range result to the screen
Serial.println(" "); // Print some spaces to the screen to make sure space direcly after the result is clear
if(range<10){
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
checkLock();
delay(100);
}
void SRF01_Cmd(byte Address, byte cmd){ // Function to send commands to the SRF01
pinMode(txrxPin, OUTPUT); // Set pin to output and send break by sending pin low, waiting 2ms and sending it high again for 1ms
digitalWrite(txrxPin, LOW);
delay(2);
digitalWrite(txrxPin, HIGH);
delay(1);
UltrasonicBus.print(Address, BYTE); // Send the address of the SRF01
UltrasonicBus.print(cmd, BYTE); // Send commnd byte to SRF01
pinMode(txrxPin, INPUT); // make input ready for Rx
}
void checkLock() {
SRF01_Cmd(srfAddress, getStatus); // Call to a function that checks if the trancducer is locked or unlocked
byte statusByte = UltrasonicBus.read(); // Reads the SRF01 status, The least significant bit tells us if it is locked or unlocked
int status = statusByte & 0x01; // Get status of lease significan bit
if(status == 0){
Serial.println("Unlocked"); // Prints the word unlocked followd by a couple of spaces to make sure space after has nothing in
}
else {
Serial.println("Locked "); // Prints the word locked followd by a couple of spaces to make sure that the space after has nothing in
}
}
int doRange() {
SRF01_Cmd(srfAddress, getRange); // Calls a function to get range from SRF01
byte highByte = UltrasonicBus.read(); // Get high byte
byte lowByte = UltrasonicBus.read(); // Get low byte
int dist = ((highByte<<8)+lowByte); // Put them together
return dist;
}
I tried using NewSoftSerial with this same code, just changing the appropriate SoftwareSerial references to NewSoftSerial. The data light on the sensor flashed as normal to indicated communication, but the data I got back was bogus- the software version, which should return "1", returned "-1." The distance measurement numbers were all random. I was getting fluctuating numbers in the 500-700 range while I should have been getting steady numbers in the 5-10 range. I suspect they might have been even more all over the place but the averaging probably smoothed it.
I am suspecting it is a timing issue. My understanding is SoftwareSerial is polling based, which allows them to use delays as necessary in getting the attention of the transducer, but since NewSoftSerial and hardware serial are interrupt based, the delays do nothing..............?
Anyone have some suggestions to this? Code greatly appreciated if anyone can post some.
Thanks.