Go Down

Topic: 2 IR Sensors to control 2 LED brightnesses - TROUBLESHOOT (Read 619 times) previous topic - next topic

dantedraven13

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! :D 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!  :D

Thanks in advance!

Jack

Grumpy_Mike

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?

Zack_Johnston

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

dantedraven13

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

Awesome! Didn't know that :D

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

Grumpy_Mike

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

Yes that sort of thing.  :)

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.

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.


Code: [Select]

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

Grumpy_Mike

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

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

dantedraven13


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

Yes that sort of thing.  :)

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.


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.


Code: [Select]

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 :D

Thanks again, I really appreciate it!

Jack

Erni

#8
Nov 22, 2012, 08:24 pm Last Edit: Nov 22, 2012, 08:26 pm by Erni Reason: 1
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

Grumpy_Mike

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

dantedraven13


Quote
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!?!?  :smiley-eek: Why's that? I just thought the LED's would burn out quicker, didn't think it would damage the Arduino ???

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.

dantedraven13


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  :smiley-sweat:

Grumpy_Mike

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

dantedraven13


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!

Go Up