Map() function

Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the map() function may be used to reverse a range of numbers...The function also handles negative numbers well

Source: http://arduino.cc/en/Reference/Map

Can the map function also assign a range of a numbers to an association to just one number?

For example:

//assign analog read to a value of 5 if range of analog read is between 1 and 1023
y=map(x, 1, 1023, 5, 5);

//or

y=map(x, 1, 1023, 5);

No, the first version is a bad idea giving you a division by zero error, which in turn may lead to nasty things including the opening of a wormhole to a different universe and the invasion of alien locusts feeding on brains and farting Britney Spears tunes. It'll be your fault!

The second version just gives a syntax error.

If you want to do that use: y=5;

Korman

Hmm, well now I'm having another dilemma. What about a potentiometer? I have a joystick controller where the potentiometer is centered at a digital reading value of 505 but the whole range is 0-1023. I want to be able to control the speed of a motor from 507 to 1023 and 503 down to 0 with 1023 and 0 being the highest speed the motor can achieve. Range from 507 - 1023 is forward while 0 - 503 is reverse, but again the speed varies with the potentiometer. Is that possible with a map function?

Yes, you can do that with map as long as the whole thing is linear, but I don't understand what you want. Do you have one or two input values or just one. If you would make a little table, things are easier. Something like:

Pot = 505 -> Motorcontrol = 505 Pot = 1023 -> Motorcontrol = 1023 Pot = 0 -> Motorcontrol = 0

In that case, the function to use is: motor = pot;

If it was: Pot = 505 -> Motorcontrol = 0 Pot = 1023 -> Motorcontrol = 1023 Pot = 0 -> Motorcontrol = -1023

use: motor = map(pot, 505, 1023, 0, 1023);

To constrain the values in case of slightly asymmetric and to prevent excessively high values, you could expand that to: motor = constrain (map(pot, 505, 1023, 0, 1023), -1023, 1023); But in most cases that shouldn't be necessary.

If it was something else, you'd have to tell us what you want.

Korman

Yes, I have two input values (possibly a third but the third is a button that I'm going to do analog read of its current) it's a joystick controller.

Some weird things are happening with my joystick now too. I am hoping it's not the hardware (I soldered it to a breakout board. All of the components were bought at SparkFun). After a while, at least today my potentiometer is reading a smaller and smaller range. Was initally 0 - 1023 and now it's down to 100 to 800 (after 30 mins to an hour of testing and reading analog input). I'll have to take a look at it when I'm home again.

On topic, what I want is to get the the motor to move one direction when going from the range of 507 - 1023 with varying speed depending on how far up the joystick is pressed. Same for the reverse direction and same for the axis of the other potentiometer. It's a stepper motor so I want the range of the speed of the motor to be from 60 to 150 rpm. 150rpm being the the top and bottom values (0 and 1023).

From my first post, I don't want the motors to move from a range close to the midpoint of the two potentiometers. That's why I want 503 - 507 needs to have rpm speed of 0.

This is the joystick/potentiometers I ordered from SF Source: http://www.sparkfun.com/commerce/product_info.php?products_id=9032

Constrain manually and use an if statement to separate the positive and negative direction, then map. Kind like:

if (pot > 507) {
  motor = pot;
}
else if (pot < 503) {
  motor = map (pot, 0, 503, 0, 507);
}
else {
  motor = 507;
}

It isn’t very elegant but will do what you need. One single map statement can’t deal with the plateau from 503 to 507.

Korman

Alright, thanks. I'll try something along those lines

I'm concerned about your changing values. That might indicate you have too much current through the pots. How is it wired?

Well after a period of time, the range of values read onto the Serial.print were getting becoming less and less (0 to 1023 down to 15 and 900 along those lines). I rewired everything and 2nd time it the values didn't change and left the microcontroller running the whole day. I'll have to test it again a third time because I'll have to rewire again once I start hooking up all four motors and the joystick controller.

Since I'm not at home right now I'll try to explain how it's wired up. I soldered 5 breakaway header pins to the breakout board of the joystick controller below it (See image below) I soldered the top of the header pins below the breakout board because I am using a solderless breadboard and the bottom of the headers are too short to go through the joystick breakout board and connect to the board. I am using a solderless breadboard so I can have clean components for other future projects (I don't have the budget to be constantly buying new things). I have 5v hooked to 5v on arduino but through the red channel of my breadboard, ground is grounded on both. Vertical and Horizontal potentiometer is hooked up to analog pins 0 and 1. I'll eventually hook up the pushbutton too to an analog pin (with a resistor) since all my digital pins will be utilized for my project. No resistors are used in any of my circuitry yet.

Again, I'll try to take pictures when I'm home. There was a slight amount of something burnt and black on the breakoutboard while I soldered the joystick and headers to the breakout board. I don't think any of the solder is touching more than one connection as close as I could check with a magnifying glass.

Source: http://www.sparkfun.com/products/116