Go Down

### Topic: Read a 2-variable 'MAP' (Read 608 times)previous topic - next topic

#### nitrolx

##### Feb 20, 2012, 11:35 am
Is it possible, using a library or just straight coding, for 2 variables (inputs)  to be read and then an output decided on a 'MAP' of values and the 2 variables??

Something Like;

1    2    3    4    5    6    7    8
1   a    b    c    d    e    f    g     h
2   i     j     k    l     m  n    o    p
3   q    r     s     t    u    v    w   x
4   y    z   etc   etc

6
7
8

therefore, if input A = 2, and input B = 4, output = z.

Cheers!

#### PaulS

#1
##### Feb 20, 2012, 11:37 am
Quote
Is it possible, using a library or just straight coding, for 2 variables (inputs)  to be read and then an output decided on a 'MAP' of values and the 2 variables??

Yes. It's trivial, even. Just define a 2D array, called map. The value of interest then is map[A-1][B-1].

#### nitrolx

#2
##### Feb 20, 2012, 11:44 am

Yes. It's trivial, even. Just define a 2D array, called map. The value of interest then is map[A-1][B-1].

I'll have to hunt up an example of how to use Arrays.

Is it possible to interpolate values between the finite values defined?
Probably just for the inputs, the outputs can remain discrete and defined in the 'map'.(array?).

so, if input A is between 2 & 3, (rather than exactly 1 or 2), what is the output?

#### AWOL

#3
##### Feb 20, 2012, 11:50 am
Quote
if input A is between 2 & 3, (rather than exactly 1 or 2), what is the output?

C specifically forbids non-integer array subscripts, so the situation cannot arise.
You can do the interpolation explicitly yourself.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### PaulS

#4
##### Feb 20, 2012, 12:25 pm
Quote
You can do the interpolation explicitly yourself.

Don't forget that you need to interpolate in both directions.

#### cyberteque

#5
##### Feb 20, 2012, 01:02 pm
This is how I did it on my Rover 5 controller.

Code: [Select]
`// Simple controller for my Rover 5// On the rover end, direction keys are mapped out for a numeric keypad.// speed is selected from z- 25%, x- 50%, c- 75%, v- 100%.// at the moment any other key stops the rover.// this programme takes X/Y values from a Sparkfun joystick shield// and translates the 2 axis into a character from "1" to "9"// at the same time we get a "speed" value based on how much stick was given.// "z" -> "v"// the speed command is sent first, then the direction command.// I'm running this code on a Mega.#define Joy_X_pin A0 #define Joy_Y_pin A1#define statusPin 13int rawX;int rawY;int xspeed;int yspeed;boolean ledState = false;char speedCom;char steerCom;char speedArray[7][7] ={    {'v','v','v','v','v','v','v'},    {'v','c','c','c','c','c','v'},    {'v','c','x','x','x','c','v'},    {'v','c','x','z','x','c','v'},     {'v','c','x','x','x','c','v'},    {'v','c','c','c','c','c','v'},    {'v','v','v','v','v','v','v'},};char steerArray[7][7] ={    {'1','1','2','2','2','3','3'},    {'1','1','2','2','2','3','3'},    {'4','4','1','5','3','6','6'},    {'4','4','4','5','6','6','6'},    {'4','4','7','8','9','6','6'},    {'7','7','8','8','8','9','9'},    {'7','7','8','8','8','9','9'},};void setup(void){  Serial.begin(9600);           //Turn on the Serial Port at 9600 bps  Serial3.begin(9600);          //Turn on the XBee Serial Port at 9600 bps  pinMode(statusPin, OUTPUT);}void loop(void){  rawX = analogRead(Joy_X_pin);  rawY = analogRead(Joy_Y_pin);  xspeed = map(rawX,0,1023,0,6);  yspeed = map(rawY,0,1023,0,6);  speedCom = speedArray[yspeed][xspeed];  steerCom = steerArray[yspeed][xspeed]; Serial3.print(speedCom); delay(50); Serial3.print(steerCom); delay(50); ledState = !ledState;  if (ledState) {    digitalWrite(statusPin,HIGH);  }  else  {    digitalWrite(statusPin,LOW);  }}`

Based on the crappy thumbstick position it selects a speed and a direction.
I did it like this to "map" out the centre zone.

Go Up

Please enter a valid email to subscribe