Pages: [1]   Go Down
Author Topic: Controlling LEDs with 4 ultra sonic sensors  (Read 1045 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

I am creating an interactive sculpture. I have 4 srf02 ultrasonic range finders each with with identifiable ids, 70, 71, 72 and 73. These sensors are hooked up to my arduino and via the A5 and A4 channels and feed numbers back into my computer using the serial monitor. All good so far.

I have two 5m strips of LED, one colour tape attached to external power supplies, these are interfaces with the arduino with a mosfet and are plugged into ports 6 and 5 digital pins on my arduino uno. I can control my LED tape using my arduino to turn it on and off and fade in and out. This is also all good so far.

What I need help with is my missing link, how to I use the readings from my sensors to control my LEDs. I think this bit is to do with the code I am using perhaps.

I am using this code to feed my sensor readings to my arduino:

#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;
  }
}

This works fine - what I need to know now is how to say the following in code - so that my arduino now controls my LEDs.

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

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.'


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 brghtest.

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 anyone can help me with either of these queries, I would be much obliged.

Many thanks in advance,

Aphra
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// 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(5, map(srf02[0].read(), 0, 50, 0, 255));
if (srf02[1].read() >=0 && srf02[1].read() <= 50)
     analogWrite(5, map(srf02[1].read(), 0, 50, 0, 255));
if (srf02[2].read() >=0 && srf02[2].read() <= 50)
     analogWrite(5, map(srf02[2].read(), 0, 50, 0, 255));
if (srf02[3].read() >=0 && srf02[3].read() <= 50)
     analogWrite(5, 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(5, HIGH);
     analogWrite(6, map(srf02[0].read(), 50, 100, 0, 255));
}
if (srf02[1].read() >=50 && srf02[1].read() <= 100) {
     digitalWrite(5, HIGH);
     analogWrite(6, map(srf02[1].read(), 50, 100, 0, 255));
}
if (srf02[2].read() >=50 && srf02[2].read() <= 100) {
     digitalWrite(5, HIGH);
     analogWrite(6, map(srf02[2].read(), 50, 100, 0, 255));
}
if (srf02[3].read() >=50 && srf02[3].read() <= 100) {
     digitalWrite(5, HIGH);
     analogWrite(6, map(srf02[3].read(), 50, 100, 0, 255));
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

Thanks so much for your response, I am so pleased to be in touch with someone who knows what they are talking about!

I am unfortunately having a problem with the code, perhaps you could help me? please see error message below:

allsensorfeedback:38: error: expected unqualified-id before 'if'
allsensorfeedback:40: error: expected unqualified-id before 'if'
allsensorfeedback:42: error: expected unqualified-id before 'if'
allsensorfeedback:44: error: expected unqualified-id before 'if'
allsensorfeedback:51: error: expected unqualified-id before 'if'
allsensorfeedback:55: error: expected unqualified-id before 'if'
allsensorfeedback:59: error: expected unqualified-id before 'if'
allsensorfeedback:63: error: expected unqualified-id before 'if'

what do you think, might be the problem?

Thanks,

Aphra

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where in your program did you put the code?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

I managed to solve that problem that i had before but have managed to get myslef into a silly situation: I am now at a gallery installing my work and I have not sorted out the problems with the code that I had before I made my sculpture itself! Too much to think about I think!

So I could really use some help!

The code
Code:
#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 you or anyone else can help me, I would most appreciate it.

Thankyou,

Aphra
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should use pinMode() to set pins to INPUT or OUTPUT before you use them.  The exception is analogWrite() which will set the pin as an OUTPUT.

Pin 4 is not a PWM (analogWrite()) pin.  Use 3, 5, 6, 9, 10, and 11 for analogWrite().

digitalWrite() will set an LED on (HIGH) or off (LOW).  To fade, use analogWrite() with values from 0 (off) to 255 (on).
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: