need help with decimal to binary conversion.

i have a DSM2 Tx module. it communicates via serial at 125000 8,n,1. i got it working. i can send data and get the expected result.
the problem is i need to convert an analog.read integer (1023, or binary B00000011 11111111) and convert it to a 8 bit binary to send via the serial port.

(edited binary for correct number /edit)

tho the catch is the upper half is encoded to have a channel identifer to it.

for channel one its B00000000 00000000 (zero value)
for channel two its B00000100 00000000 (zero value)
for channel 3 its B00001000 00000000 (zero value)
fo channel 4 its B00001100 00000000 (zero value)

and so on. 6 channels total.

ive been working with AVR for a while, and i have mostly usd AVR ASM.
what i would do in ASM is read ADCH then ADCL, then use a third register with B00000100 and bitwise OR that with what i got from ADCH then id be done.

but i can do that in C. (still learning C)

i fount the MAP function, but it did not output as expected. (i really miss avr studio simulator right now!)

here is the code i got. it works, it sends what i expect. the reciver responds as it should. im haveing trouble with converting 1023 to a pair of byte and modding them.

my current code.

int gen = 0;
byte genb = B00000000;
int led = 13;
int elevator = 0;

byte Th = B00000000;
byte Tl = 0x00;
byte Ah = B00000100;
byte Al = 0x00;
byte Eh = B00001000;
byte El = 0x00;
byte Rh = B00001100;
byte Rl = 0x00;
byte Gh = B00010000;
byte Gl = 0x00;
byte Fh = B00010100;
byte Fl = 0x00;
byte bind[14] = {0x98, 0x00, 0x00, 0x00, 0x05, 0XFF, 0x09, 0xFF, 0x0D, 0xFF, 0x10, 0xAA, 0x14, 0xAA};
byte header[14] = {0x00, 0x00, Th, Tl, Ah, Al, Eh, El, Rh, Rl, Gh, Gl, Fh, Fl};
//                            

void setup() {
  // put your setup code here, to run once:

  pinMode(led, OUTPUT);

  Serial.begin(125000);  
 
 delay(500);
 
 
 do
   {
      Serial.write(bind, 14);
      delay(22);
      ++gen;
}  while (gen < 250);



}
void loop() {
  // put your main code here, to run repeatedly: 
 
  elevator = 512;
  
  El = map(elevator, 0,255,0,255);
  Eh = map(elevator,256,1023,0,255);
  
  genb = B00001000;
  
  Eh = Eh | genb;
 
  
  delay(22);
  
  Serial.write(header, 14); 
}

(1023, or binary B00000111 11111111)

No, 1023 is 0x3FF, B00000011 11111111

oops, my bad.

here is the code i got. it works, it sends what i expect. the reciver responds as it should. im haveing trouble with converting 1023 to a pair of byte and modding them.

There are highByte() and lowByte() macros that do the first part for you. What modifications you want to make are then up to you.

in any case:

byte hi, lo;
int reading;

lo = reading & 0xFF;
hi = (reading >> 8) & 0x03;

hi |= 0x04;  // sets the 3rd bit to 1

PS: the code you posted is probably not doing what you expect. Changing El and Eh doesn't change the header array. Also, I'm not sure you understand what map() does. For example the first map() call is returning 257. Is that what you expected?

It is fairly easy:

#define MSB(word) ((word) >> 8) //return the most significant byte or a word
#define LSB(word) ((word) & 0x00ff) //return the least significant byte of a word

const unsigned short CH_MSB[]={
  0x00; //ch 0
  (1<<2); //ch 1
  (1<<3); //ch 2
  ...
};

...
  unsigned short elevator;
  for (ch = 0; ch < 6; ch++) {
    elevator = read_elevator(); //read elevator. 10-bit results, right adjusted
    Serial.Write(CH_MSB[i] | MSB(elevator)); //write msb first
    Serial.Write(LSB(elevator)); //write lsb
  }

You can fiddle with it more to do it the way you wanted but that’s the general idea.

oaky, highByte() and lowByte(), i will look into those.

map, i really have no idea what id does. i was trying to split up the 1023 in to 255 chunks to work with those in 8-bit binary.

dhenery: i have no idea what that code does! i dont even know were to start on that.

im working off of a arduino refference to got as a pdf, and its incomplete.

at any rate, i was able to get bits 10, 9 converted. but when i try to put that in a function, it give me a compile error, yet when i run it in void loop() it runs and compiles fine. i donr really under stand functions, last time i used subs were in QBASIC, then in AVR ASM the RCALL made no diffence as to what variable passed to what.

edit:
okay! i got it working! i used the highbyte() and such.
i also had trouble with witch way the servos move… the dsm2 module advertises 1024 bit resolution, but the rx only supports in the 100 - 800 range, other than than and it overflows and the servo maxes out in the same direction…whatever.

thank you so much!

here is my code

int gen = 0;
int led = 13;

unsigned int val = 0;

byte binaryH = 0x00;
byte binaryL = 0x00;


byte Th = B00000000;
byte Tl = 0x00;
byte Ah = B00000100;
byte Al = 0x00;
byte Eh = B00001000;
byte El = 0x00;
byte Rh = B00001100;
byte Rl = 0x00;
byte Gh = B00010000;
byte Gl = 0x00;
byte Fh = B00010100;
byte Fl = 0x00;
 
byte bind[14] = {0x98, 0x00, 0x00, 0x00, 0x05, 0XFF, 0x09, 0xFF, 0x0D, 0xFF, 0x10, 0xAA, 0x14, 0xAA};

//                            

void setup() {
  // put your setup code here, to run once:

  pinMode(led, OUTPUT);

  Serial.begin(125000);  
 
 delay(500);
 
 
 do
   {
      Serial.write(bind, 14);
      delay(22);
      ++gen;
}  while (gen < 250);



}
void loop() {
  // put your main code here, to run repeatedly: 
  byte header[14] = {0x00, 0x00, Th, Tl, Ah, Al, Eh, El,  Rh, Rl, Gh, Gl, Fh, Fl};

  digitalWrite(led, HIGH);
 
  val = 800;  // misc value to simulate analog read

  binaryH = highByte(val);
  binaryL = lowByte(val);
  
  Eh = Eh | binaryH;
  El = El | binaryL;
  

  delay(22);
  
  Serial.write(header, 14); 
}

map, i really have no idea what id does.

http://arduino.cc/en/Reference/Map

Ok, tried to keep quiet, but had to say something...

This code is NOT doing what you want it to do.

Biggest problem is that you never reset the values of Eh and El, so they just accumulate bits until they are all 1's. This might explain why your servos get pegged after a while.

More subtly, the headers array is not changed until the NEXT time loop is called and it is re-initialized. So you send the old value of headers, not the current value. The reason in looks like it works is that you are sending the same value each time.

Finally, you send the serial init string (bind) 250 times. Is that really what you wanted to do?

int led = 13;

#define Th 2
#define Tl 3
#define Ah 4
#define Al 5
#define Eh 6
#define El 7
#define Rh 8
#define Rl 9
#define Gh 10
#define Gl 11
#define Fh 12
#define Fl 13
 
byte bind[14] = {0x98, 0x00, 0x00, 0x00, 0x05, 0XFF, 0x09, 0xFF, 0x0D, 0xFF, 0x10, 0xAA, 0x14, 0xAA};
                    
void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(125000);  
  delay(500);
  Serial.write(bind, 14);
  delay(22);
}

void loop() {
  int val;
  byte header[14] = { 0x00,0x00,0x00,0x00,0x04,0x00,0x08,0x00,0x0c,0x00,0x10,0x00,0x14,0x00};

  digitalWrite(led, HIGH);
 
  val = 0x2A5;  // misc value to simulate analog read

  header[Eh] |= highByte(val);
  header[El] |= lowByte(val);

  delay(22);
  Serial.write(header, 14); 
}

If you have used the Simulator in AVR Studio then you should also try out Simulator for Arduino. This Simulator allows for watching variables as the sketch is single stepped and variables can be switched between decimal, hex and char by right clicking in the variable area ( we can add binary to the nect version if this is useful) Version 0.98 will be out soon with many improvements. There are well over a thousand Arduino users utilising Simulator for Arduino now.