2

So I just starting playing around with UART and SoftwareSerial while playing with these SRF-02 Sensors - http://www.robot-electronics.co.uk/htm/srf02techSer.htm

I need help getting one to read to the other, I have been using this, makes sense logically, but doesn't work. I was thinking I'm missing something important for the synchronization of both sensors to one another.

Code:

#include <SoftwareSerial.h>  
SoftwareSerial mySerial(10,11); // RX, TX 

void SendCmd(unsigned char address,unsigned char cmd)
{
  mySerial.write(address);//set the address of SRF02(factory default is 0)
  delayMicroseconds(100);//serial data is fixed at 9600,N,8,2,so we need some time to creat the sencond stop bit
  mySerial.write(cmd);//send the command to SRF02
  delayMicroseconds(100);//serial data is fixed at 9600,N,8,2,so we need some time to creat the sencond stop bit
}
void setup(void)
{
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("SRF02 TEST!");
}
void loop(void)
{
  unsigned int reading;
  SendCmd(0x00,0x5C);//Transmit 8 cycle 40khz pulse from adress 0x00
  delay(70);//time for SRF02 to measure the range
  SendCmd(0x02,0x5E);//Get Range, returns two bytes (high byte first) from the most recent ranging to adress 0x02
   delay(10);//wait for some time,let the Arduino receive 2 bytes data from the TX pin of SRF02
  if(mySerial.available()>=2)//if two bytes were received
  {
    reading = mySerial.read()<<8;//receive high byte (overwrites previous reading) and shift high byte to be high 8 bits
    reading |= mySerial.read(); // receive low byte as lower 8 bits
    Serial.print(reading); // print the reading
    Serial.println("cm");
  }
  delay(250); // wait a bit since people have to read the output :)
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.