Having trouble with the map command

The intended purpose of this code is to read a joysticks value, map it down to 0-179 and have a servo(either being X or Y) move corresponding to the joystick. I have put in the servo.attach line in because i was trying to get it to give the right values in the serial monitor. The joysticks value(0-1023) is right but im getting 2 for the X and Y degrees in the serial monitor when i move it or when its still.

#include <Servo.h>
Servo myservoX;
Servo myservoY;
const int servoX = 9;
const int servoY = 10;
const int joyX = A0;
const int joyY = A1;
int potXval = 0;
int potYval = 0;
int degreeX = 0;
int degreeY = 0;
void setup() {
Serial.begin(9600);
myservoX.attach(servoX);
myservoY.attach(servoY);

}

void loop() {
potXval = analogRead(joyX);
potYval = analogRead(joyY);
degreeY= map(joyY, 0, 1023, 0, 179);
degreeX= map(joyX, 0, 1023, 0, 179);

Serial.print("X = “);
Serial.print(potXval);
Serial.print(” “);
Serial.print(degreeX);
Serial.print(” ");
Serial.print("Y = “);
Serial.print(potYval);
Serial.print(” ");
Serial.print(degreeY);
Serial.println();
//delay(20);
}

Please edit your post to include code-tags. See How to use the forum.

the_quadster:
The joysticks value(0-1023) is right but im getting 2 for the X and Y degrees in the serial monitor when i move it or when its still.

Can you "prove" that for use by posting a copy of the serial monitor data. Because I see nothing to indicate that.

And uhmmm

the_quadster:

Servo myservoX;

Servo myservoY;
const int servoX = 9;
const int servoY = 10;
const int joyX = A0;
const int joyY = A1;

Why are the servo's yours? Aka my doesn't add anything other then you copying it from the example :wink:

And those servoX and servoY seem an awful lot like pin numbers, not servo's :wink: Aka servoXPin and servoYPin are better names. And a byte will do :wink: But good job using const! :smiley:

And I don't know why exectly you try to do but try not to just declare all variables as global.

Hi,

LOOK:

potXval = analogRead(joyX);
  potYval = analogRead(joyY);
  degreeY= map(joyY, 0, 1023, 0, 179);
  degreeX= map(joyX, 0, 1023, 0, 179);

You want to map potXval, right? You wrote joyX which is the PORT address..

so,

 degreeY= map(potYval, 0, 1023, 0, 179);

etc..

OK?

I thought A0, A1 were aliases used when you want to use them with digitalRead/Write. I just use 0, 1 with a macro and skip the variable.

From pins_arduino.h:

// Mapping of analog pins as digital I/O
// A6-A11 share with digital pins
#define PIN_A0   (18)
#define PIN_A1 (19)

so mapping 18, 19 from a range of 1023 to 179 will always result in 2. Sounds about right...

Gotcha

bDeters:
I thought A0, A1 were aliases used when you want to use them with digitalRead/Write. I just use 0, 1 with a macro and skip the variable.

From pins_arduino.h:

// Mapping of analog pins as digital I/O

// A6-A11 share with digital pins
#define PIN_A0  (18)
#define PIN_A1 (19)




so mapping 18, 19 from a range of 1023 to 179 will always result in 2. Sounds about right...

Gotcha, i fixed it. But why shouldn't i put an A in front of 0 and 1? Cause then the arduino wont know if it should read the digital or analog pins, right? Also could i power the joystick with my 4 AA pack that's about 6 volts? Would the arduino still work exactly the same(with sensing the value)?

Ahhhhh, yeah! So my whining about the names was actually the problem!

@the_quadster, it's not nice to not change your post :wink:

And don't worry about the A. Just use a A in front to not confuse and to be sure it always works.*

And no, you can not. That will put 6V on the Arduino pin which will damage it. Just use the 5V output of the Arduino.

*Alright, if you really want to know, A0 is indeed a higher number (14 to be exact on a Uno). analogRead() will do the same if you do analogRead(0), analogRead(A0), or analogRead(14) because if the number entered is higher then the highest digital pin number (13 on a Uno) it will subtract 14 from the number to get to 0.

But you can use a analog pin as a digital pin. The analog is just an extra function of the pin, just like PWM. But digitalRead(0) and digitalRead(A0) will read a different pin. So just use the A notation :slight_smile:

map is a function, not a command. If you don't understand the difference its something
worth understanding.

septillion:
Ahhhhh, yeah! So my whining about the names was actually the problem!

@the_quadster, it's not nice to not change your post :wink:

And don't worry about the A. Just use a A in front to not confuse and to be sure it always works.*

And no, you can not. That will put 6V on the Arduino pin which will damage it. Just use the 5V output of the Arduino.

*Alright, if you really want to know, A0 is indeed a higher number (14 to be exact on a Uno). analogRead() will do the same if you do analogRead(0), analogRead(A0), or analogRead(14) because if the number entered is higher then the highest digital pin number (13 on a Uno) it will subtract 14 from the number to get to 0.

But you can use a analog pin as a digital pin. The analog is just an extra function of the pin, just like PWM. But digitalRead(0) and digitalRead(A0) will read a different pin. So just use the A notation :slight_smile:

Whats the max current and voltage an analog pin can take in?

Same as any other pin. As input (both analog or digital) it's high impedance so barely any current will flow. Max voltage, same as any other pin, max -0,5V and 5,5V. Analog it can't read a voltage different between -0,5 and 0V) (will all read 0) or between 5V and 5,5V (read all 1023). As output (digital), 40mA absolute max, stay under 25mA to be save.

“it’s high impedance so barely any current will flow.”, yep, just 1uA Max per the datasheet.

You want to use a source with at least 500uA drive capability if you want to perform ADCs at the best rate possible tho to quickly charge the ADC sample & hold capacitor. (5V/10K resistor).

CrossRoads:
"it's high impedance so barely any current will flow.", yep, just 1uA Max per the datasheet.

You want to use a source with at least 500uA drive capability if you want to perform ADCs at the best rate possible tho to quickly charge the ADC sample & hold capacitor. (5V/10K resistor).

Don't really get what you just said, can you simplify it for someone new to this haha?

Which part?

Or try to tell us what you actually try to do.