Home » code snippets, projects

Ardunio Visible Distance Detector v1.0 snippet

Submitted by Carl on Monday, 24 November 2008No Comment

/*
This code receives input from the SRF02 ultrasonic and maps the values to RGB outputs to a tri-colour LED.
*/

#include <Wire.h>
int redPin   = 3;   // Red LED,   connected to digital pin 9
int greenPin = 6;  // Green LED, connected to digital pin 10
int bluePin  = 5;  // Blue LED,  connected to digital pin 11

int redVal   = 1; // Variables to store the values to send to the pins
int greenVal = 1;   // Initial values are Red full, Green and Blue off
int blueVal  = 255;

// the commands needed for the SRF sensors:
#define sensorAddress 0x70
//#define readInches 0x50
//#define readCentimeters 0x51
#define readMicroseconds 0x52
// this is the memory register in the sensor that contains the result:
#define resultRegister 0x02

void setup() {
  pinMode(redPin,   OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin,  OUTPUT);
  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin,  blueVal);
  Wire.begin();

  // open the serial port:

  Serial.begin(9600);

}

void loop()

{

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

  sendCommand(sensorAddress, readMicroseconds);
  // wait at least 70 milliseconds for a result:
  delay(100);
  // set the register that you want to reas the result from:
  setRegister(sensorAddress, resultRegister);
  int sensorReading = readData(sensorAddress, 2);

  //sensorReading = constrain(sensorReading,0,180);
  blueVal  = constrain(map(sensorReading, 800, 2000, 0, 250),0,255);
  redVal   = constrain(map(blueVal, 0, 255, 255, 0),0,255);
  greenVal = 1;   // Initial values are Red full, Green and Blue off

  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin,  blueVal);  

  Serial.print("distance: ");
  Serial.print(sensorReading);
  Serial.print("  mapval: ");
  Serial.print(redVal);
  Serial.print("\n");
  delay(250);
}

/*

  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;

}

code>

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.