Hi all, hopefully someone is able to help me, I've been stuck on this for days.
Basically when I create a new sketch and only read a single analog pin, I get a result of 0 as expected (there is a pull-down resistor on each pin). However, when I then loop over reading 5 of the analog pins as seen in my code below I get any values that vary between 30-80 depending on the pin. I have not altered the setup of my arduino, only uploaded new code.
#define snarePzo A0
#define highPzo A1
#define lowPzo A2
#define floorPzo A5
#define bassPzo A4
void setup() {
Serial.begin(9600);
}
void loop() {
int hitArray[5] = {analogRead(snarePzo), analogRead(highPzo), analogRead(lowPzo), analogRead(bassPzo), analogRead(floorPzo)};
for (int i = 0; i <= 4; i++) {
Serial.print(i); Serial.print(" "); Serial.print(hitArray[i]); Serial.println();
delay(20);
}
}
Am I going about this wrong? I've also tried reading the pins indivdually instead of using an array and for loop, and I get the same behaviour.
Thanks in advance for the help!
Jonathon
I have seen reports of analogRead() not returning correct values when read one after the other from different pins. Your method of declaring the array certainly does this. It is somethimes suggested that 2 analogRead()s should be taken from each pin and only the second one used.
As an experiment try this
#define snarePzo A0
#define highPzo A1
#define lowPzo A2
#define floorPzo A5
#define bassPzo A4
int analogPins[] = {snarePzo, highPzo, lowPzo, floorPzo, bassPzo};
void setup()
{
Serial.begin(115200);
int hitArray[5];
for (int pin = 0; pin < 5; pin++)
{
int dummy = analogRead(analogPins[pin]);
hitArray[pin] = analogRead(analogPins[pin]);
}
for (int i = 0; i <= 4; i++)
{
Serial.print(i);
Serial.print(" ");
Serial.print(hitArray[i]);
Serial.println();
}
}
void loop()
{
}
Hi UKHeliBob,
Your suggestion does indeed fix this issue!
One question, I noticed you changed the baud rate for Serial.begin. Is there an ideal value to use?
Thanks for your help!
I noticed you changed the baud rate for Serial.begin. Is there an ideal value to use?
Whatever suits you. For the Serial monitor as fast as possible is a good rule but for other devices you need to match baud rates of course.
I settled on 115200 when it was the maximum available and I have stuck to it although higher baud rates are now available.