Loading...
Pages: [1]   Go Down
Author Topic: Trying to read multible analog inputs  (Read 351 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I'm new in trying to use a Arduino Duemilanove adn created a little program that send the values from the analogRead() fuction of port 0 to 4 to the serial. When connecting nothing to the board I get as expected the following result send to the serial:

0##0##0##0##0/end

But when I connect the 5v output of the board to the analog input pin 2 I get:

989##979##1023##1012##1000/end

I expedted to get:

0##0##1023##0##0/end

I don't understand how this can be please help me the total code of the program is:

int val[] = {0, 0, 0, 0, 0};
int analogPin[] = {0 ,1 ,2 ,3 ,4};
int analogPin0 = 0;
int analogPin1 = 0;
int analogPin2 = 0;
int analogPin3 = 0;
int analogPin4 = 0;
int ledPin2 = 2;
int ledPin3 = 3;
int ledPin4 = 4;
int ledPin5 = 5;
int ledPin6 = 6;
int ledPin7 = 7;
int ledPin8 = 8;
int ledPin9 = 9;
int ledPin10 = 10;
int ledPin13 = 13;
int value = LOW;                // previous value of the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)
char StrP[ ] = "arduino";


void setup()
{
  Serial.begin(9600);      // open the serial port at 9600 bps:
  pinMode(ledPin2, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin3, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin4, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin5, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin6, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin7, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin8, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin9, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin10, OUTPUT);      // sets the digital pin as output
  pinMode(ledPin13, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.
  
  for (int i=0; i <= 4; i++){
      val = analogRead(analogPin);
      //Serial.print(val);
      }
   //delay(10000);

  
  // check to see if it's time to blink the LED; that is, is the difference
  // between the current time and last time we blinked the LED bigger than
  // the interval at which we want to blink the LED.
  if (millis() - previousMillis > interval) {
    previousMillis = millis();   // remember the last time we blinked the LED

    // if the LED is off turn it on and vice-versa.
    if (value == LOW)
      value = HIGH;
    else
      value = LOW;
    
    for (int i=0; i <=4; i++){
      Serial.print(val);
      if (i != 4) {
        Serial.print("##");
      }
    }
    Serial.print("/end");
    digitalWrite(ledPin13, value);
  }
}
Logged

London
Offline Offline
Faraday Member
**
Karma: 6
Posts: 6226
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are your analog pins floating –  analog pins not connected to 5 volts in your test should be connected to ground.

Also you can simplify:
   val = analogRead(analogPin);
to
   val = analogRead(i);
« Last Edit: January 11, 2009, 09:52:34 am by mem » Logged

Central MN, USA
Offline Offline
Faraday Member
**
Karma: 35
Posts: 5915
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The analog pins are cross-talking. If you want to read zero you will need to connect the other input pins to zero volts (GND), not floating the pins. Maybe you can use the internal pullup or pulldown resistors on the other pins. I think it should be like this in setup:


digitalWrite(pin#,HIGH) makes the analog pin read 1023
digitalWrite(pin#,LOW) makes the analog pin read 0

But I am just as new to arduino as you're, indeed newer (I'm still waiting for my chip to be shipped [smiley=rolleyes.gif])
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are no internal pull-downs on the AVR. Writing a '0' to an input pin sets the pin in tri-state mode. Writeing a 1 sets it in pull-up mode.
Logged

Pages: [1]   Go Up
Print
 
Jump to: