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! 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!
Thanks in advance!
Jack