division problem

i have a problem with the division function:
i read from a rotary encoder, the variable is between 870 and 1070 and i convert the DEZ number to 3 HEX numbers, later i push some bit's around and send it with shiftOut.
the code is somthing like this (sorry couldent copy and paste, i have no internet):

(all variables are int)
RestDEZ2 = encoder0Pos / 16; // 1050 / 16 =65.625
SendHEX1 = encoder0Pos % 16; //first HEXnumber to send
RestDEZ1 = RestDEZ2 / 16;
SendHEX2 = RestDEZ2 % 16; //second HEXnumber to send
SendHEX3 = RestDEZ1 % 16; //third HEXnumber to send

but in the first line i get a problem.the RestDEZ2 i get is 62 instead of 65, so the calculations trail an error :-? normaly a int variable is big enough i think, i also tried with large because in the refence is written that this function can overflow, but nothing changed... float variabes diden't work, the compiler aleaves something like: error: float variable doesn't work with Arithmetic function..
in C the rest falls off if it's not a float variable, is there a hint to do this operation in the arduino IDE or is there might a bug? :-X
somebody got an idea?

Are you sure that the encoder0Pos value is 1050 when you calc is returning 62

Not knowing what else is in the code I don't see anything wrong. The division should work fine for the value range you specified. 62 would indicate that the encoder0Pos is between 992 and 1007.

shure,after i recogniced that something is wrong, i changed my sketch to see all variables on the serial monitor because i also thought so (992-1007)...but i think it's like that...
i took the rotary encoder howtoo (Arduino Playground - HomePage).
i take some snapshots this evening and i try to find some internet in the streets to post that...

The overflow will only occur if the result is too large for the output. For instance if the result was 1024 and you were trying to store it in a byte or 70000 and trying to store in an integer.

The interesting problem is your statement that when you tried floats you got an error like:

error: float variable doesn't work with Arithmetic function..

Since the division operator should work fine with floating point values I am inclined to think there is something in another part of your code that is causing the problem. If you can post the whole code as it stands it would help. From what you've shown I don't see anything that could be causing the problem. The division operator probably isn't the source of your problem.

ok, now it works, i re wrote and improved the sketch. i think there was a error in my syntax...

here is my sketch:

sketch for the radio emiter project
int val;
int encoder0PinA = 3;
int encoder0PinB = 4;
int encoder0Pos = 1000;
int encoder0PinALast = LOW;
int n = LOW;
byte HEX1; //HEXnumbers to send to device
byte HEX2;
byte HEX3;
int RestDEZ1;
int RestDEZ2;
void setup() {

  • pinMode (encoder0PinA,INPUT);*
  • pinMode (encoder0PinB,INPUT);*
  • Serial.begin (300);*
    void loop() {
  • n = digitalRead(encoder0PinA);*
  • if ((encoder0PinALast == LOW) && (n == HIGH)) {*
  • if (digitalRead(encoder0PinB) == LOW) {*
  • encoder0Pos--;*
  • } else {*
  • encoder0Pos++;*
  • }*
  • {*
  • RestDEZ2 = encoder0Pos / 16; //z.B. 1050 / 16 = 65,625*
  • HEX1 = encoder0Pos % 16; *
  • RestDEZ1 = RestDEZ2 / 16;*
  • HEX2 = RestDEZ2 % 16;*
  • HEX3 = RestDEZ1 % 16;*
  • }*
  • Serial.print (encoder0Pos);*
  • Serial.print ("/");*
  • Serial.print (HEX3, HEX);*
  • Serial.print ("/");*
  • Serial.print (HEX2, HEX);*
  • Serial.print ("/");*
  • Serial.print (HEX1, HEX);*
  • Serial.print ("/");*
  • Serial.print (RestDEZ2);*
  • Serial.print ("/");*
  • Serial.print (RestDEZ1);*
  • Serial.print ("/");*
  • }*
  • encoder0PinALast = n;*

when i change RestDEZ1 & 2 in float i get this compiling error:

In function 'void loop()':
error: invalid operands of types 'float' and 'int' to binary 'operator%'

and he marks line:

HEX2 = RestDEZ2 % 16;
this also happens when HEX variables are int or long...

i don't know, but this seems quite logic...and i thought there was something with function..sorry
thanks for the help

An easier approach to the whole thing would be to do the conversion to Hex-values by some simple bit-shifting.
Since encoder0Pos is an int we will have a maximum of 4 Hex-bytes

byte HEX0; //The lowest nibble (4bit = Hex-value ) of the encoderpos
byte HEX1;
byte HEX2;
byte HEX3; //The Highest nibble (4bit = Hex-value ) of the encoderpos

//We clear the upper 12 bits of the encoder pos. The lowest Hex-byte is just the bits 0-3 of encoder0Pos 
HEX0 = (byte) (encoder0Pos & 0x000F);

//The higher the nibbles need to be shifted to the right after being masked
HEX1 = (byte) ((encoder0Pos & 0x00F0) >> 4);
HEX2 = (byte) ((encoder0Pos & 0x0F00) >> 8);
HEX3 = (byte) ((encoder0Pos & 0xF000) >> 12);

I guess you know that these bytes are not the Hex-Numbers characters ?

They are just bytes in range 0..16. (In case you want to send the characters '0'..'F' over the serial line)


yes, i know. i want to programm a device who is a little bit space... ( http://www.rohmelectronics.com/downloads/products/focus/audio_lsi/bh1414k.pdf ) see page 17-18 for communication details.
my sketch is far from beeing complete...

yes, i know. i want to programm a device who is a little bit space

I had a look at the datasheet.
Assuming you are talking about the 12 Bit "Program Counter" Part, you would not need to do any hex-conversion at all.
Simply send the lower 12 Bits of encoder0Pos to the device (LSB first).

Is there still a problem to be solved with the division thing?