Newbie Question

Hello,

I am trying to take 5 analogue readings and send them out the serial port followed by an identifier (in this case “break” to mark the end of the 5 reads). however, with only 1 FSR attached to say, analogue input 0 a reading of say, 250 spreads as though the same reading (or there abouts +/- 5) had been taken from all analogue inputs. here’s the code:

int PIN_NUM [5];
int READ_VAL;

void setup ()
{
Serial.begin(57600);
for (int i=0;i<5;i++)
{
pinMode(PIN_NUM ,INPUT);

  • }*
    }
    void loop ()
    {
  • for (int i=0;i<5;i++)*
  • {*
    analogRead(PIN_NUM );
    READ_VAL = analogRead(PIN_NUM );
    * Serial.println(READ_VAL>>2, DEC);
    _ delay (500);
    }
    Serial.println(“break”);
    delay (500);
    }
    I then tried to write it out the long way just to see if it would work and it does (please see below). Could anybody tell me what the reason for the behaviour of the first lot of code please. Shouldn’t these two programmes be identical in behaviour? Any help would be much appreciated.
    int READ_VAL;
    void setup ()
    {
    Serial.begin(57600);
    pinMode(0,INPUT);
    pinMode(1,INPUT);
    pinMode(2,INPUT);
    pinMode(3,INPUT);
    pinMode(4,INPUT);
    }
    void loop ()
    {
    analogRead(0);_
    READ_VAL = analogRead(0);
    Serial.println(READ_VAL>>2, DEC);
    _ delay (500);
    analogRead(1);_
    READ_VAL = analogRead(1);
    Serial.println(READ_VAL>>2, DEC);
    _ delay (500);
    analogRead(2);_
    READ_VAL = analogRead(2);
    Serial.println(READ_VAL>>2, DEC);
    _ delay (500);
    analogRead(3);_
    READ_VAL = analogRead(3);
    Serial.println(READ_VAL>>2, DEC);
    _ delay (500);
    analogRead(4);_
    READ_VAL = analogRead(4);
    Serial.println(READ_VAL>>2, DEC);
    _ delay (500);
    Serial.println(“break”);
    delay (500);
    }*_

A floating input, i.e. a input with nothing connected to it, will return a more or less random, at least totally unreliable vallue.

thanks for the reply. However, i don't think i explained it clear enough.

the point is the first lot of code returns the same values for each analogue pin even though only one FSR is attached to a single input. The reason I know that this is not just noise is because of the second lot of code in the above example which returns meaningful values only for the pin which has an FSR attached, i.e. if an FSR is attached to analogue 0 only analogue 0 returns a changing value which corresponds to my gestures with the FSR. the others show background noise but its clear there is no relationship between the data at input pins with no FSR attached and the input pin with an FSR attached.

In a nutshell, if both programmes are identical why does the first lot of code not work and the second lot of code work?

Thanks and sorry for my lack of clarity before.

You didn't assign any values to the elements of the int PIN_NUM array. Actually, I don't think you need an array at all. Just use i (since the pin numbers go from 0 to 5 anyway).

That’s it. Much appreciated and thanks for the advice.