Go Down

Topic: Using an ultrasonic sensor to send signals to LEDs (Read 515 times) previous topic - next topic

Aphra

Hi there,

I have been struggling and puzzling with this project for a while. I am making an interactive light sculpture. I use an ultrasonic SFRX02 sensor to take readings of how far a person is in relation to my sculpture. I have been using a code for ages which wasn't good enough or fast enough. But now I have found the code below:
Code: [Select]
#include <Wire.h>

void setup()
{
  Wire.begin();                // join i2c bus (address optional for master)
  Serial.begin(9600);          // start serial communication at 9600bps
}

int reading = 0;

void loop()
{
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(112); // transmit to device #112 (0x70)
                               // the address specified in the datasheet is 224 (0xE0)
                               // but i2c adressing uses the high 7 bits so it's 112
  Wire.write(byte(0x00));      // sets register pointer to the command register (0x00) 
  Wire.write(byte(0x50));      // command sensor to measure in "inches" (0x50)
                               // use 0x51 for centimeters
                               // use 0x52 for ping microseconds
  Wire.endTransmission();      // stop transmitting

  // step 2: wait for readings to happen
  delay(70);                   // datasheet suggests at least 65 milliseconds

  // step 3: instruct sensor to return a particular echo reading
  Wire.beginTransmission(112); // transmit to device #112
  Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting

  // step 4: request reading from sensor
  Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

  // step 5: receive reading from sensor
  if(2 <= Wire.available())    // if two bytes were received
  {
    reading = Wire.read();  // receive high byte (overwrites previous reading)
    reading = reading << 8;    // shift high byte to be high 8 bits
    reading |= Wire.read(); // receive low byte as lower 8 bits
    Serial.println(reading);   // print the reading
  }

  delay(250);                  // wait a bit since people have to read the output :)
}


Which is great, really good feedback, fast enough and accurate too. But now I need to use these readings to control the brightness of two seperate strips of leds attached to points 5 and 6. This is where I get stuck in my old code my sensor was labelled SFRX02 so I knew whch bit of the code related to my sensor and could use a command like this:

Code: [Select]
if (srf02[0].read() >=50 && srf02[0].read() <= 0) {
     digitalWrite(5, HIGH);
     analogWrite(6, map(srf02[0].read(), 50, 0, 0, 255));


But with my new code I do not know which bit relates to my sensor in order to then say to my arduino take reading from the sensor and use those readings to control the brightness of my led strip.

Can anyone help?

I would like my code to say:

when the sensor takes a reading between 50 and 100.
fade in led strip on pin number 5, 100 being the faintest, 50 being the brightest.
led strip on pin number 6, do nothing, keep low/off

when sensor takes a reading between 50 and 0
keep led strip on pin number 5 on high
AND fade in strip on pin number 6, 50 being the faintest, 0 being the brightest.

Can anyone help me to work out, which part of the code above relates to the sensor (what is its name) as i may be able to work out the code from there. or can anyone help me with writing some code that corresponds to request below and the code above?

I would most appreciate it,

Thanks,

Aphra

Riva

I would turn the sensor reading code (from your example the entire void loop()) into a function that you call and it returns the distance.
Code: [Select]
#include <Wire.h>

void setup()
{
  Wire.begin();                // join i2c bus (address optional for master)
  Serial.begin(9600);          // start serial communication at 9600bps
}


void loop()
{
  int result=srf02Read();             // Read the sensor
  if (result >=50 && result <= 0) {   // Act on it
     digitalWrite(5, HIGH);
     analogWrite(6, map(result, 50, 0, 0, 255));
  }
}


int srf02Read()
{
  int reading = -1;            // assume error (-1)
  // step 1: instruct sensor to read echoes
  Wire.beginTransmission(112); // transmit to device #112 (0x70)
                               // the address specified in the datasheet is 224 (0xE0)
                               // but i2c addressing uses the high 7 bits so it's 112
  Wire.write(byte(0x00));      // sets register pointer to the command register (0x00) 
  Wire.write(byte(0x50));      // command sensor to measure in "inches" (0x50)
                               // use 0x51 for centimetres
                               // use 0x52 for ping microseconds
  Wire.endTransmission();      // stop transmitting

  // step 2: wait for readings to happen
  delay(70);                   // datasheet suggests at least 65 milliseconds

  // step 3: instruct sensor to return a particular echo reading
  Wire.beginTransmission(112); // transmit to device #112
  Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting

  // step 4: request reading from sensor
  Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

  // step 5: receive reading from sensor
  if(2 <= Wire.available())    // if two bytes were received
  {
    reading = Wire.read();     // receive high byte (overwrites previous reading)
    reading = reading << 8;    // shift high byte to be high 8 bits
    reading |= Wire.read();    // receive low byte as lower 8 bits
  }
  return reading;              // return -1 if failed or the distance reading
}
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

Go Up