map to sin

Is it possible to to map to a sin wave, instead of doing a linear map?

You might be able to write your own function.

.

Yes, you can create a lookup table of 1/4 of a sine wave values, say 90 values long. Count up thru the array, then down thru the array, then up thru the array but multiply by -1, then up thru the array but multiply by -1.

degrees	radians	sin
0	0	0
1	0.017453293	0.017452406
2	0.034906585	0.034899497
3	0.052359878	0.052335956
4	0.06981317	0.069756474
5	0.087266463	0.087155743
6	0.104719755	0.104528463
7	0.122173048	0.121869343
8	0.13962634	0.139173101
9	0.157079633	0.156434465
10	0.174532925	0.173648178
11	0.191986218	0.190808995
12	0.20943951	0.207911691
13	0.226892803	0.224951054
14	0.244346095	0.241921896
15	0.261799388	0.258819045
16	0.27925268	0.275637356
17	0.296705973	0.292371705
18	0.314159265	0.309016994
19	0.331612558	0.325568154
20	0.34906585	0.342020143
21	0.366519143	0.35836795
22	0.383972435	0.374606593
23	0.401425728	0.390731128
24	0.41887902	0.406736643
25	0.436332313	0.422618262
26	0.453785606	0.438371147
27	0.471238898	0.4539905
28	0.488692191	0.469471563
29	0.506145483	0.48480962
30	0.523598776	0.5
31	0.541052068	0.515038075
32	0.558505361	0.529919264
33	0.575958653	0.544639035
34	0.593411946	0.559192903
35	0.610865238	0.573576436
36	0.628318531	0.587785252
37	0.645771823	0.601815023
38	0.663225116	0.615661475
39	0.680678408	0.629320391
40	0.698131701	0.64278761
41	0.715584993	0.656059029
42	0.733038286	0.669130606
43	0.750491578	0.68199836
44	0.767944871	0.69465837
45	0.785398163	0.707106781
46	0.802851456	0.7193398
47	0.820304748	0.731353702
48	0.837758041	0.743144825
49	0.855211333	0.75470958
50	0.872664626	0.766044443
51	0.890117919	0.777145961
52	0.907571211	0.788010754
53	0.925024504	0.79863551
54	0.942477796	0.809016994
55	0.959931089	0.819152044
56	0.977384381	0.829037573
57	0.994837674	0.838670568
58	1.012290966	0.848048096
59	1.029744259	0.857167301
60	1.047197551	0.866025404
61	1.064650844	0.874619707
62	1.082104136	0.882947593
63	1.099557429	0.891006524
64	1.117010721	0.898794046
65	1.134464014	0.906307787
66	1.151917306	0.913545458
67	1.169370599	0.920504853
68	1.186823891	0.927183855
69	1.204277184	0.933580426
70	1.221730476	0.939692621
71	1.239183769	0.945518576
72	1.256637061	0.951056516
73	1.274090354	0.956304756
74	1.291543646	0.961261696
75	1.308996939	0.965925826
76	1.326450232	0.970295726
77	1.343903524	0.974370065
78	1.361356817	0.978147601
79	1.378810109	0.981627183
80	1.396263402	0.984807753
81	1.413716694	0.987688341
82	1.431169987	0.990268069
83	1.448623279	0.992546152
84	1.466076572	0.994521895
85	1.483529864	0.996194698
86	1.500983157	0.99756405
87	1.518436449	0.998629535
88	1.535889742	0.999390827
89	1.553343034	0.999847695
90	1.570796327	1

Oh that's cool. I've never done a lookup table before. Do I have to worry about it slowing down my encoder readings?

xxmamakinxx:
Do I have to worry about it slowing down my encoder readings?

Possibly - it's the first mention of encoders.
Why don't you try it and see?

Another approach would be to include the math.h library and base the mapping on its sin() function.

It'd probably be slower to execute than a lookup table but possibly a bit easier to write the code.

Whichever way you do it, yes it will slow down your sketch compared to a linear mapping. You need to experiment to see if it's still fast enough.

This must be the best (or worst) Thread title I have come across.

I don't think any of us would have trouble finding sin without a map :slight_smile:

...R

Finally got around to spending some time with this, and found my map to sin:

        int inInt = map(var_x, in_min, in_max, 0, 6283);
        float outFloat = inInt/1000;
        mappedSinVar = sin(outFloat) * range + offset;

It's modified from SparkFuns pulse led tutorial.
If you go there, it will explain the range and offset.

Why not simply use the sin() function? Is it too slow? Is the fact that the argument is in radians a problem?

How do you mean? I am using the sin() function, but I need to go from a set of variables with a low/high of 0-18000 and output another set from 180-255. What I wrote allows me to map the input variables to radians and plug that into the sin() function. Then the range and offset allow me to output to my values to the range I need, 180-255.

This is my first time using sin(), how else would you recommend using it?

Robin2:
This must be the best (or worst) Thread title I have come across.
I don't think any of us would have trouble finding sin without a map :slight_smile:

But if you do need one, here it is: Google Maps