Go Down

Topic: analogIn only showing floating voltage (Read 1 time) previous topic - next topic

tedder

Okay, I'm obviously doing something wrong. I have an LCD and a MaxSonar hooked up, and the maxSonar is to the "AN" port, which is analog voltage. I can peek at the voltage coming off the MaxSonar and see it is what I expect: 0.06v when my hand is over it, 0.9v when it's "looking" across the room. That's fine.

When I do my AnalogRead, it returns a floating voltage value, right around 330-340. I've tried reading from port 2 or port "A2". No dice. Here's a code snippet:
Code: [Select]
 LCD.print( "prox pin2: ");
 LCD.print( analogRead(A2) );


I can hold my Fluke meter on the line and see the result vary from 0.06 to 0.9, but the Arduino output still says the value is somewhere around 340, meaning the Arduino thinks it's sitting at about 1.66v. Not exactly what I would have hoped.

I have a couple of boards handy. This one is a Freeduino/BBB from ModernDevice, running a ATMEGA168. I've tried setting pinMode to INPUT, I've tried changing what port I'm using (instead of 2/A2). Nothing changes. What am I missing? Do I need a pulldown resistor? (I thought those were only necessary for digital work)

mowcius

Quote
Do I need a pulldown resistor? (I thought those were only necessary for digital work)

Have you tried testing the analog pin you are using by connecting it to 5v or ground to see what it gives you.

Are you sure your code has the write pin number defined?

Mowcius

tedder

#2
Oct 10, 2010, 10:51 pm Last Edit: Oct 10, 2010, 10:51 pm by tedder Reason: 1
Quote
Have you tried testing the analog pin you are using by connecting it to 5v or ground to see what it gives you.


Nope. Good point. Just tried that, the number doesn't change.

Quote
Are you sure your code has the write pin number defined?


Uh, the write pin? To the LCD? The LCD is working fine, it's the analogRead that isn't working properly. Here's the ENTIRE code, though it's only analogRead I'm having trouble with:
Code: [Select]
#include <SoftwareSerial.h>

#define lcdPin 10
#define proxPin 2

SoftwareSerial LCD = SoftwareSerial(0, lcdPin);
// since the LCD does not send data back to the Arduino, we should only define the txPin

void setup() {
 pinMode(lcdPin, OUTPUT);
 pinMode(2, INPUT);
 LCD.begin(9600);
 clearLCD();
 delay (1000);

}

void loop() {
//  int proxVal = 0;
//  int proxVal = analogRead(proxPin);
 
 selectLineOne();
 delay(100);
 LCD.print( "line1");
 selectLineTwo();
 delay(100);
 LCD.print( "line2");
 selectLineThree();
 delay(100);
 LCD.print( "line3");
 selectLineFour();
 delay(100);
 LCD.print( "pin2: ");
 LCD.print( analogRead(2) );
 delay(100);
}

void selectLineOne() { //puts the cursor at line 0 char 0.
 LCD.print(0xFE, BYTE); //command flag
 LCD.print(0x80, BYTE); //position
}
void selectLineTwo() { //puts the cursor at line 0 char 0.
 LCD.print(0xFE, BYTE); //command flag
 LCD.print(192, BYTE); //position
}
void selectLineThree() {
 LCD.print(0xFE, BYTE); //command flag
 LCD.print(148, BYTE); //position
}
void selectLineFour() {
 LCD.print(0xFE, BYTE); //command flag
 LCD.print(212, BYTE); //position
}

void clearLCD() {
 LCD.print(0xFE, BYTE); //command flag
 LCD.print(0x01, BYTE); //clear command.
 LCD.print(0x7C, BYTE); //backlight
 LCD.print(214, BYTE); //backlight

 delay(50);
}


tedder

Hmm. I think I was trying to read off a digital port. I'm used to the feel for a regular 'duino, and forgot the analog and digital ports aren't the same ports.

Go Up