Pages: [1]   Go Down
Author Topic: Read a 2-variable 'MAP'  (Read 414 times)
0 Members and 1 Guest are viewing this topic.
Newcastle, NSW, Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Just a mechanic
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50362
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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].
Logged

Newcastle, NSW, Australia
Offline Offline
Newbie
*
Karma: 0
Posts: 33
Just a mechanic
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes. It's trivial, even. Just define a 2D array, called map. The value of interest then is map[A-1][B-1].
Thanks for the quick reply!
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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50362
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You can do the interpolation explicitly yourself.
Don't forget that you need to interpolate in both directions.
Logged

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is how I did it on my Rover 5 controller.

Code:
// 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 13

int 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.
Logged

Pages: [1]   Go Up
Jump to: