map() question

Hello All –

I’m working on the ‘ultimate cat toy’ project and decided to learn Ardiuno programming to control it. I’m using the map() function to control speed and felt like it wasn’t acting the way I wanted. I made a simple program to see exactly what the map fucntion was doing and am curious about the output…

Below is the program…

int val = -25;
int cycle = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
do {
int valmap = map(val,0,10,0,255);
cycle++;
val++;

Serial.println("***************************************");
Serial.print("Count = ");
Serial.println(cycle);
Serial.print("Input = ");
Serial.println(val);
Serial.print("Map = “);
Serial.println(valmap);
Serial.println(” ");

} while (cycle < 305);

delay(1000);

}

For some reason it won’t let me copy the serial monitor output, but I don’t reach an output of 255 until my input value is 11. So, if I’m controling PWM using this fucntion, the input would have to go to 11 (Spinal Tap?)…

Is this correct? Am I missing Something?

-CW

The map function is described here: http://arduino.cc/en/Reference/Map you may check the calculus for your specific values.

The problem is that you are incrementing the val variable AFTER you call map(), but before you print the results.

As you'd expect, the result of map(10, 0, 10, 0, 255) is 255.

Ahhh - it's a sequence problem. Thanks, guys!

chris48083: Hello All --

I'm working on the 'ultimate cat toy' project and decided to learn Ardiuno programming to control it. I'm using the map() function to control speed and felt like it wasn't acting the way I wanted. I made a simple program to see exactly what the map fucntion was doing and am curious about the output...

Ok, I'm intrigued. I have cats and love driving them crazy with toys. I'd be interested in some of the details of your 'ultimate cat toy'. Care to share some details here or in a PM?