interactive leds controlled by sensors

Hi there,

I have 4 srf02 ultrasonic range finders hooked up to my arduino uno which are feeding back data to my serial monitor. I also have two 5m strips of leds attached to ports 5 and 6. I am using the code below to use the data from my range finders to fade in and out my leds depending on the readings.

However I keep getting an error message and cannot work out what the problem is see 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 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));
}

and error message:

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’

If anyone can help I would be much obliged,

many thanks,

A

Where does your loop() function end? How much code do you have beyond that point?

Hi,

There is no more code beyond what I have there, I am quite new to all this so I also am not entirely sure what you mean!? any suggestions?

thanks

There is no more code beyond what I have there

Yes, but the end of the loop() function happens before the end of the code that you have. All executable code must be in a function. Not all of yours is, which is why the compiler is complaining.

Hi there,

I’ve cracked that problem so the code is now uploaded. I have a problem with the code now perhaps you can help me?

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

It is fading my lights on and off how I wish however when the reading goes above 100 the lights seem to just stay on, is there a way to remedy this in the code?

thanks for your help

The Tools menu has an Auto Format option that you would be well advised to learn how to use. Often, that is.

You really should think about getting the logic write for one sensor. Right now, you write to one pin when the range is 0 to 50, and to another pin when the range is 50 to 100. You never turn off the other pin. You should be turning off pin 5 when the range is greater than 50, and should be turning off pin 6 when the range is less than 50.

I don't see the point of having 4 sensors that define what happens to one pin.