2 IR Sensors to control 2 LED brightnesses - TROUBLESHOOT

Hi all!

I'm new to using an Arduino and I'm having trouble. I have an Uno and am trying to create a simple script that allows me to change the brightness of each LED depending on my proximity to each sensor, however I keep having the problem that when I try to create another analogRead/analogWrite path, both LED's end up reacting to only the one sensor. On the other hand I have looked to see if I am getting separate data from each sensor and I am. Furthermore, I am able to create a script(shown below) with one LED reacting with an analog output to one sensor's data and the other reacting with a digital on/off 'if statement.' This has really confused me as I have no idea what I've done wrong.

Below is my code with the analog and digital outputs working, as well as the code I tried to use to get both LED's to work on analog (commented out):

int distPin = 0, distPin2 = 1, ledPin = 9, ledPin2 = 10;
void setup()
{
** analogReference(DEFAULT);**
** pinMode(ledPin, OUTPUT);**
** pinMode(ledPin2, OUTPUT);**
** Serial.begin(9600); // Allow Serial Monitor to display data coming backs**
}
void loop()
{
** Serial.println(analogRead(distPin2)); // Print the Recieved Data**

int val = analogRead(distPin);
int val2 = analogRead(distPin2);

** val = constrain(val, 100, 550); // constrains incoming data to this range**

// val2 = constrain(val, 100, 550); // constrains incoming data to this range

** int ledLevel = map(val, 100, 550, 0, 255); // maps the incoming data range(analog)**
** //to a digital output ranging from 0 to 255**

// int ledLevel2 = map(val, 100, 550, 0, 255);

analogWrite(ledPin, ledLevel);
// analogWrite(ledPin2, ledLevel2);
if(val2 > 200) digitalWrite(ledPin2, HIGH);
else digitalWrite(ledPin2, LOW);

}

As I said, I am very new to all this so if there is any rockie mistakes please point them out! :smiley: Also, using this code, the analog LED keeps flickering even when there is nothing close to the sensor; I've been told that I can use a filter to fix this, however I don't know about that either so if anyone can help with that I would be very grateful! :smiley:

Thanks in advance!

Jack

Do not use pins 0 and 1 as they are connected to the serial port.
Do not use constrain and map just divide the analogue reading by four to convert it into the range for the PWM.

Didn't you ask the same question last week?

Just to let you know, you can do this with analog circuitry as well....

Do not use pins 0 and 1 as they are connected to the serial port.

Awesome! Didn't know that :smiley:

Do not use constrain and map just divide the analogue reading by four to convert it into the range for the PWM.

So would I do something like this:
ledLevel = val / 4

Didn't you ask the same question last week?

Nope, just created the account 10 mins before my first post! Could you link me to that thread if you know where it is?? It sounds very useful lol

Just to let you know, you can do this with analog circuitry as well....

I know, however I'm new to all this stuff but have done some programming before. Plus I'm hoping to use these inputs for other things once I get the hang of this, example being a Daft Punk table for a friend lol

FILTER FLICKER

Do you guy have any ideas on this part? I need to know how to create a steady signal :~

Thanks again!!

Jack

So would I do something like this:
ledLevel = val / 4

Yes that sort of thing. :slight_smile:

Sorry but as you can see I do post a lot so I have not got that thread handy.

When you try this and have any questions then post the code and ask again.
You do have resistors in line with the LEDs don't you.

It would be a good idea to do the analogRead() two (ore more) times and only use the last.
Also a small delay() betweem reading the different pins, gives the ADC time to settle.

 int val = analogRead(distPin);
  int val = analogRead(distPin);
delay(100);
int val2 = analogRead(distPin2);
int val2 = analogRead(distPin2);

Also a small delay() betweem reading the different pins, gives the ADC time to settle.

No, that delay is doing nothing.

If you need time for the analogue input to settle you must put the delay between the first and the second reading of the same channel.
That delay is after the reading of the first channel BUT when the analogue multiplexer is still switched to that channel. Therefore there is no point in settling on that voltage because after the delay it is immediately switched to the other input.

It would be a good idea to do the analogRead() two (ore more) times and only use the last.

That is only a good idea if the input impedance is way over the recommended 10K, and in this case it should not be necessary.

Grumpy_Mike:

So would I do something like this:
ledLevel = val / 4

Yes that sort of thing. :slight_smile:

Sorry but as you can see I do post a lot so I have not got that thread handy.

When you try this and have any questions then post the code and ask again.
You do have resistors in line with the LEDs don't you.

Thanks for the help! No I haven't got them on resistors as I only have 1k and 10k at the moment and I've been told I need 330Ohms.

Erni:
It would be a good idea to do the analogRead() two (ore more) times and only use the last.
Also a small delay() betweem reading the different pins, gives the ADC time to settle.

 int val = analogRead(distPin);

int val = analogRead(distPin);
delay(100);
int val2 = analogRead(distPin2);
int val2 = analogRead(distPin2);

I tried your code and it threw up an error, however I resolved the problem as in my original code I had the ledLevel1 and 2 both using 'val' instead of val and val2. All Sorted now! Although they still are flickering which is annoying lol, I'll have to hunt around for that filter I've heard about :smiley:

Thanks again, I really appreciate it!

Jack

Grumpy_Mike
Ofcource you are right.
As far as iI remeber it once solved a problem when two sensors using the ADC were affecting each other. Obviously it must have been a different situation

No I haven't got them on resistors as I only have 1k and 10k at the moment and I've been told I need 330Ohms.

Use 1K if that is all you have. By not using any you are damaging your Arduino and causing it to fail early.

Grumpy_Mike:

No I haven't got them on resistors as I only have 1k and 10k at the moment and I've been told I need 330Ohms.

Use 1K if that is all you have. By not using any you are damaging your Arduino and causing it to fail early.

Really!?!? :astonished: Why's that? I just thought the LED's would burn out quicker, didn't think it would damage the Arduino ???

You can only draw 49mA from an arduino output pin without damage. An LED without a resistor can draw upwards of 250mA many times over the damage threshold.

Grumpy_Mike:
You can only draw 49mA from an arduino output pin without damage. An LED without a resistor can draw upwards of 250mA many times over the damage threshold.

Right, so should I create a separate 5v circuit for the 2 sensors then? They draw 50mA(max) each :sweat_smile:

No you are not powering the sensors from an arduino output pin, they are just connected to the 5V supply.

Grumpy_Mike:
No you are not powering the sensors from an arduino output pin, they are just connected to the 5V supply.

Thats good to know! I don't have anything I can vary the voltage on yet, I'm having to cut up old USB cables to get a supply and gnd :~ Not the best way, but its all I've got so far lol. Thanks again for the help, I would've been screwed if my Uno packed up!

No need to cut up a lead the +5V and the ground on the arduino can be used to power the sensors.

Grumpy_Mike:
No need to cut up a lead the +5V and the ground on the arduino can be used to power the sensors.

Do you know what the maximum I could use is? I have 4 each drawing 50mA max, but it would be useful to know if it can handle them/the max it can power

Each USB port should be capable of supplying 500mA, but in practice this depends on the capability of the computer supping the power.