Go Down

Topic: Converting byte to nibbles (Read 1 time) previous topic - next topic

Palliser

Hello. I need help converting 1 byte into 2 nibbles and print them. I tried this but it doesn't work.

Code: [Select]
const int analogInPin0 = A0;
int readByte = 0;
int mappedByte = 0;
int bottom_nibble  = 0;
int top_nibble = 0;

void setup() {
  // initialize serial communications at 115200 bps:
  Serial.begin(115200);
}

void loop() {
   readByte = analogRead(analogInPin0);           
   mappedByte = map(readByte, 0, 1023, 0, 255);
   
   bottom_nibble = readByte & 0xf;
   top_nibble = ( readByte > 4 ) & 0xf;
   
   Serial.print(bottom_nibble);
   Serial.print(top_nibble); 
 
   delay(10);                     
}


Thanks!

Grumpy_Mike

I. Think you need the shift operator >> not the greater than operator >

PaulS

Code: [Select]
   mappedByte = map(readByte, 0, 1023, 0, 255);
That's an expensive way to divide by 4.

Palliser

I thought that but then.. I heard a voice-over (it sounded like HAL9000) that was all "don't make it so easy for the guy" . Regards

Go Up