Pages: [1]   Go Down
Author Topic: code help regarding reading multiple analog inputs  (Read 1089 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone, I am trying to write a sketch that will read in two analog input signals (through pins A0 and A2) which will then be used to turn on two different sets of LEDs based on their respective input voltage levels. For A0, I have specified output pins 2-6 and for A2, 7-11. (I have used the example LED bar graph code to set up my code). My code compiles without any errors, however, when I went to test it with the Arduino, both sets of LEDs would light up the same even when there was only one input, regardless of whether it was connected to A0 or A2. Basically, both groups of LEDs are not being controlled independently by the two different inputs as I would like them to.  I have a feeling there is something wrong with how I set up my for() loops in the main body of the code but can't quite put my finger on it. Any help would be greatly appreciated!

Thanks!






Code:
const int analogPin = A0;   // the pin that the input signal is attached to
const int ledCount = 5;  // the number of LEDs

const int analogPin2 = A2;   
const int ledCount2 = 5;

int ledPins[] = {2, 3, 4, 5, 6};
int ledPins2[] = {7, 8, 9, 10, 11};// an array of pin numbers to which LEDs are attached


void setup() {
  for (int thisLed = 0; thisLed < ledCount; thisLed++) // set digital pins to OUTPUT
  { pinMode(ledPins[thisLed], OUTPUT);}
 
  for (int thisLed2 = 0; thisLed2 < ledCount2; thisLed2++)
  { pinMode(ledPins2[thisLed2], OUTPUT);}
}


void loop() {
 
  int sensorReading = analogRead(analogPin);
  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
 
  int sensorReading2 = analogRead(analogPin2);
  int ledLevel2 = map(sensorReading2, 0, 1023, 0, ledCount2);

 
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
   
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
    }
   
    else {
      digitalWrite(ledPins[thisLed], LOW);
    }
  }
 
 
  for (int thisLed2 = 0; thisLed2 < ledCount2; thisLed2++) {
   
    if (thisLed2 < ledLevel2) {
      digitalWrite(ledPins2[thisLed2], HIGH);
    }
   
    else {
      digitalWrite(ledPins2[thisLed2], LOW);
    }
  }
 
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 513
Posts: 31529
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int analogPin = A0;
You only use A0 and A1 when you want to use the analogue pins as digital ones.
To use them as analogue ones you must use:-
Code:
const int analogPin = 0;

I haven't checked the rest but correct that first.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46204
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
const int analogPin = A0;   // the pin that the input signal is attached to
const int ledCount = 5;  // the number of LEDs

const int analogPin2 = A2;   
const int ledCount2 = 5;
Do you count things by going "yeah, I got a thing, 2, 3, 4, ...". Of course not, you go "1, 2, 3,...". So, if you are going to number variable names, number ALL of them.

There are no Serial.print() statements in your code. Time to add some.

Quote
My code compiles without any errors, however, when I went to test it with the Arduino, both sets of LEDs would light up the same even when there was only one input, regardless of whether it was connected to A0 or A2.
The "even when there is only one input" bothers me. You should not be trying to read an analog pin with nothing connected to it.

Code:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
This statement is too hard to read. The index variable should be a one letter name.
Code:
for(int i=0; i<ledCount; i++)
{
}
Logged

Northern Ireland
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You also haven't set the analog pins to INPUT.  I don't know if the Arduino will default to INPUT, hopefully someone else can answer that.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24424
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You only use A0 and A1 when you want to use the analogue pins as digital ones.
Sorry Mike, that's not so.

Quote
You also haven't set the analog pins to INPUT. 
analogRead implies that the pin is an input; there's no need to set pinMode
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

mdg48,
did you ever figure this out?  i am running into a very similar issue without a good solution and wanted to see what you may have ended up doing.
thanks
j
Logged

Montreal
Offline Offline
Edison Member
*
Karma: 23
Posts: 2487
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino has only one ADC, and one sampling and hold (S/H) capacitor is used for different channels. So , observed behavior is normal, to prevent inter-channel interference BOTH inputs have to be connected to low impedance (relatively to sampling rate)  source. Ground other input to track software issues.
Logged

Pages: [1]   Go Up
Jump to: