Go Down

Topic: analogread() only returning 8 bits (Read 3097 times) previous topic - next topic

Synan

May 30, 2015, 06:12 am Last Edit: Jun 02, 2015, 12:34 am by Synan Reason: Added Code
I'm trying to use a pentometer with my Arduino Mega, but analogread only seems to return the 8 lower bits, ignoring the first two. As I turn the pentometer it goes up to around 256, the jumps back to 0. It does this four times. I've tried multiple pentometers all different ports, but it's the same problem.

The Knob servo example shows this problem, as does simply sending the values to the computer over serial. Is there something physical I'm missing (like adding a resistor)?

My code (I commented out the servo code):
Code: [Select]
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
Servo myservoB;
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int valB;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Pent Readings");
 /* myservo.attach(6);
  myservoB.attach(9); */ // attaches the servo on pin 9 to the servo object
}
 
void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  Serial.println(String(val));
/*  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val); */
  delay(10);
}


Thank you.

TomGeorge

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the  </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom...... :)
Everything runs on smoke, let the smoke out, it stops running....

JimboZA

Let's see your code: what variable type are you reading the value into? Maybe an int?.....
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

JoeN

#3
May 30, 2015, 07:07 am Last Edit: May 30, 2015, 07:08 am by JoeN
Let's see your code: what variable type are you reading the value into? Maybe an int?.....
Arduino ints are 16 bit signed.  Maybe he used code from a sketch with the cross platform types.  uint_8 is popular and that is a true unsigned 8 bitter and would cause a problem.
I will never ask you to do anything that I wouldn't do myself.

JimboZA

Yeah I meant he should use an int, sorry I worded it badly but in my defence it was 6am on a Saturday. It might be that at the moment he's trying to fit it into a byte.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

Paul__B

Well, as we always say:


(Yes, of course he is clipping it to a byte.)

Riva

Sounds like your reading the analogue value into a byte instead of an int.
Don't PM me for help as I will ignore it.

Grumpy_Mike

My bet is it is a char not a byte.  :)

Paul__B


Synan

It was always a normal int.
Sorry, I added the code to the top post.

Zapro


cmiyc

Turn the knob until analogRead() returns 250-255, or something near it.

Then use a multimeter and measure the voltage going into the analog pin.

What voltage do you measure?

Quote
Code: [Select]

  Serial.println(String(val));

Serial.println(val); //works just fine
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Isaac96

Are you sure you have the pot connected right? (I know, stupid question)
Do not PM me for help. I will delete immediately.
CONNECT THE GROUNDS!

After Tuesday, even the calendar goes W T F

TomGeorge

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?


Please don't update previous posts, although you added the code it makes for a discontinuous run of posts that follow it.


Tom..... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up