Pages: [1]   Go Down
Author Topic: Converting byte to nibbles  (Read 1162 times)
0 Members and 1 Guest are viewing this topic.
Venezuela
Offline Offline
God Member
*****
Karma: 16
Posts: 515
Ground, ground, always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33448
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Venezuela
Offline Offline
God Member
*****
Karma: 16
Posts: 515
Ground, ground, always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: