Go Down

Topic: code for ultrasonic sensors setting off led strips (Read 348 times) previous topic - next topic

Aphra

Hi there,

I have managed to get myself into an impossible situation where I am at a gallery installing my show and have got some glitches in the code and don't know how to fix them. If anyone could help me i would be much appreciated.

The reasons why I do not know the answers to the questions is because someone very kindly helped me with the code, i didn't write it and am not that clued up about it!

My code below:

Code: [Select]
#include "Wire.h"
#include "SRF02.h"

SRF02 srf02[4] = {
  SRF02(0x70, SRF02_CENTIMETERS),
  SRF02(0x71, SRF02_CENTIMETERS),
  SRF02(0x72, SRF02_CENTIMETERS),
  SRF02(0x73, SRF02_CENTIMETERS)
};

unsigned long nextPrint = 0;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
}

void loop()
{
  SRF02::update();
  if (millis() > nextPrint)
  {
    Serial.print(srf02[0].read());
      Serial.print(",");
      Serial.print(srf02[1].read());
      Serial.print(",");
      Serial.print(srf02[2].read());
      Serial.print(",");
      Serial.print(srf02[3].read());
      Serial.println();
      nextPrint = millis () + 1000;
}



// When sensor's 70, 71, 72 & 73 take a reading between 0-50 turn on LED strip on port 5.

if (srf02[0].read() >=0 && srf02[0].read() <= 50)
     digitalWrite(5, HIGH);
if (srf02[1].read() >=0 && srf02[1].read() <= 50)
     digitalWrite(5, HIGH);
if (srf02[2].read() >=0 && srf02[2].read() <= 50)
     digitalWrite(5, HIGH);
if (srf02[3].read() >=0 && srf02[3].read() <= 50)
     digitalWrite(5, HIGH);

// When sensor's 70, 71, 72 & 73 take a reading between 50-100, keep on LED strip 5 and also turn on the LED strip on port 6.'

if (srf02[0].read() >=50 && srf02[0].read() <= 100) {
     digitalWrite(5, HIGH);
     digitalWrite(6, HIGH);
}
if (srf02[1].read() >=50 && srf02[1].read() <= 100) {
     digitalWrite(5, HIGH);
     digitalWrite(6, HIGH);
}
if (srf02[2].read() >=50 && srf02[2].read() <= 100) {
     digitalWrite(5, HIGH);
     digitalWrite(6, HIGH);
}
if (srf02[3].read() >=50 && srf02[3].read() <= 100) {
     digitalWrite(5, HIGH);
     digitalWrite(6, HIGH);
}


// I would be happy to have this communication going first but ultimately I actually want
// the readings to fade in and out my LEDs, see below:

// 'When sensors 70, 71, 72 & 73 take readings 0-50 fade in LED strip on port 5, 0 being the lowest, 50 being the brightest.

if (srf02[0].read() >=0 && srf02[0].read() <= 50)
     analogWrite(4, map(srf02[0].read(), 0, 50, 0, 255));
if (srf02[1].read() >=0 && srf02[1].read() <= 50)
     analogWrite(4, map(srf02[1].read(), 0, 50, 0, 255));
if (srf02[2].read() >=0 && srf02[2].read() <= 50)
     analogWrite(4, map(srf02[2].read(), 0, 50, 0, 255));
if (srf02[3].read() >=0 && srf02[3].read() <= 50)
     analogWrite(4, map(srf02[3].read(), 0, 50, 0, 255));


// When sensors 70, 71, 72 & 73 take readings 50-100, keep strip 5 on its brightest and fade in strip on port 6, 50 beng the lowest, 100 being the brightest.'


if (srf02[0].read() >=50 && srf02[0].read() <= 100) {
     digitalWrite(4, HIGH);
     analogWrite(5, map(srf02[0].read(), 50, 100, 0, 255));
}
if (srf02[1].read() >=50 && srf02[1].read() <= 100) {
     digitalWrite(4, HIGH);
     analogWrite(5, map(srf02[1].read(), 50, 100, 0, 255));
}
if (srf02[2].read() >=50 && srf02[2].read() <= 100) {
     digitalWrite(4, HIGH);
     analogWrite(5, map(srf02[2].read(), 50, 100, 0, 255));
}
if (srf02[3].read() >=50 && srf02[3].read() <= 100) {
     digitalWrite(4, HIGH);
     analogWrite(5
     , map(srf02[3].read(), 50, 100, 0, 255));
}
}


My problems:

1. For some reason when the sensors read above 100 the light in port 5 just stays on all the time. I beileve that i need to state in the code when sensors read above 100 keep both lights in ports 5 and 4 off.
2. The lights don't appear to fade in or out - please can you indicate in the code which bit is that states the fade in and out and how to alter it.
3. i would also like to know which bit in the code indicates the sensor readings as i may need to tweak these. Please can you point this out to me aswell.

If anyone can help me i would appreciate it greatly,

Thankyou,

Aphra

PaulS

Quote
For some reason when the sensors read above 100 the light in port 5 just stays on all the time.

You don't have any code to turn it off. It's turned on of any range is between 0 and 50 and PWM'd if the range is between 50 and 100. The minimum brightness (off, actually) occurs right at 50. It looks to me like the mapping is wrong. It looks like the from range should be 100 to 50, not 50 to 100.

Quote
2. The lights don't appear to fade in or out

What do they appear to do? As I said, I think the mapping is wrong. It is the map function that determines the PWM (brightness) value, and analogWrite() that applies it.

Quote
3. i would also like to know which bit in the code indicates the sensor readings as i may need to tweak these.

srf02[ n ].read() is what gets the value for the nth sensor.

Go Up