LusoRobótica - Robótica em Português

Sistemas específicos => Arduino / AVR => Tópico iniciado por: joao1996f em 07 de Dezembro de 2012, 23:00

Título: Sensor SRF02
Enviado por: joao1996f em 07 de Dezembro de 2012, 23:00
Olá, o meu Arduino chegou hoje juntamente com um lcd e um sensor ultra-sons SRF02.
Queria meter a distancia que  sensor deteta no lcd, mas so consigo na porta serial.
Código: [Seleccione]
/*


  SRF02 sensor reader


 language: Wiring/Arduino Reads data from a Devantech SRF02 ultrasonic sensor.


 Should also work for the SRF08 and SRF10 sensors as well.


Sensor connections:


 SDA - Analog pin 4


 SCL - Analog pin 5


created 5 Mar. 2007


 by Tom Igoe


*/


// include Wire library to read and write I2C commands:




//#include
#include <Wire.h>
#include <SoftwareSerial.h>


// the commands needed for the SRF sensors:


#define sensorAddress 0x70


#define readInches 0x50


// use these as alternatives if you want centimeters or microseconds:


#define readCentimeters 0x51


#define readMicroseconds 0x52


// this is the memory register in the sensor that contains the result:


#define resultRegister 0x02


void setup()


{


  // start the I2C bus
  pinMode(2, OUTPUT);   
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  Wire.begin();


  // open the serial port:


  Serial.begin(9600);


}


void loop()


{


  // send the command to read the result in inches:


  sendCommand(sensorAddress, readCentimeters);


  // wait at least 70 milliseconds for a result:


  delay(70);


  // set the register that you want to reas the result from:


  setRegister(sensorAddress, resultRegister);


// read the result:


  int sensorReading = readData(sensorAddress, 2);


  // print it:


if(sensorReading < 25)
{
digitalWrite(2, HIGH);   // set the verm LED on
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if(sensorReading > 75)
{
digitalWrite(4, HIGH);   // set the verd LED on
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}
if((sensorReading > 25) && (sensorReading < 75))
{
digitalWrite(3, HIGH);   // set the amar LED on
digitalWrite(2, LOW);
digitalWrite(4, LOW);
}




  Serial.print("distancia: ");


  Serial.print(sensorReading);


  Serial.println(" cm");


  // wait before next reading:


  delay(270);


}


/*


  SendCommand() sends commands in the format that the SRF sensors expect


 */


void sendCommand (int address, int command) {


  // start I2C transmission:


  Wire.beginTransmission(address);


  // send command:


  Wire.send(0x00);


  Wire.send(command);


  // end I2C transmission:


  Wire.endTransmission();


}


/*


  setRegister() tells the SRF sensor to change the address pointer position


 */


void setRegister(int address, int thisRegister) {


  // start I2C transmission:


  Wire.beginTransmission(address);


  // send address to read from:


  Wire.send(thisRegister);


  // end I2C transmission:


  Wire.endTransmission();


}


/*


readData() returns a result from the SRF sensor


 */


int readData(int address, int numBytes) {


  int result = 0;        // the result is two bytes long


// send I2C request for data:


  Wire.requestFrom(address, numBytes);


  // wait for two bytes to return:


  while (Wire.available() < 2 )   {


    // wait for result


  }


  // read the two bytes, and combine them into one int:


  result = Wire.receive() * 256;


  result = result + Wire.receive();


  // return the result:


  return result;


}
Título: Re: Sensor SRF02
Enviado por: senso em 07 de Dezembro de 2012, 23:11
Está mais legivel assim.
Pois, não tens código para escrever nada para o lcd, é normal que não mostre nada.
Título: Re: Sensor SRF02
Enviado por: joao1996f em 07 de Dezembro de 2012, 23:14
Resolvido