Pages: [1]   Go Down
Author Topic: Basic question concerning types  (Read 594 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In my code I try to map the value of delta (which is time interval based on millis() ), usually in the range of 400-800 ms, to a byte (400 = 0, 800 = 255, values outside the range are clipped to the border values). I have this code:

 if (delta > 800) delta = 800;
 axis = delta - 400;
  if (axis < 0) axis = 0;
  axis = (axis * 1000) / 1569;
 ...
  Wire.send(axis);

axis is int, delta is unsigned long. When I output the values, delta behaves exactly as it should. axis, however, takes rather strange values, even negative ones, although those should be ruled out completely.

I suppose this is a case of mismatched types. I guess that when I get the axis value right, it would still be sent incorrectly to Wire (as it should be a byte)?

I could hack it somehow, I suppose, but I would prefer to understand the underlying principles and see a more elegant solution...
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 638
Posts: 50318
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I output the values, delta behaves exactly as it should. axis, however, takes rather strange values, even negative ones, although those should be ruled out completely.
This depends on how you output the value.

Quote
In my code I try to map the value of delta (which is time interval based on millis() ), usually in the range of 400-800 ms, to a byte (400 = 0, 800 = 255, values outside the range are clipped to the border values). I have this code:
There are map() and constrain() functions to do this. Why not use them?

Finally, just because the value in axis could fit in a byte, Wire.send() will not send it as a byte, because it isn't a byte. If you need Wire.send() to send a single byte, you need to store the value to be sent in a byte variable, and pass that variable to the Wire.send() function.
« Last Edit: February 27, 2011, 09:35:24 am by PaulS » Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 30
Posts: 2602
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suggest, that do a casting than subtract:
axis = (int)delta - 400;
Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Providing you define the variables taking millis() values as 'unsigned long' rather than 'int' there shouldn't be any problems.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for helpful replies!

I have misinterpreted the purpose of map() before - it seems it is indeed perfect for this case.

If I type axis as byte, then, this should work?

delta = constrain (delta, 400, 800) ;
axis = map(delta, 400, 800, 1, 255);
Wire.send (axis);

I cannot test this out yet, wife's still around smiley-razz


 
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 638
Posts: 50318
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If I type axis as byte, then, this should work?

delta = constrain (delta, 400, 800) ;
axis = map(delta, 400, 800, 1, 255);
Wire.send (axis);
Shouldn't the to range be 0 to 255? Otherwise, yes.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Shouldn't the to range be 0 to 255? Otherwise, yes.

Oh, sorry, this is for non-programmatic reasons - in short, I want to use a training bike to emulate pedals attached to a computer driving wheel. In some games it is useful to have it "idling" a bit, but of course in some cases it will be zero.
Logged

Pages: [1]   Go Up
Jump to: