Go Down

Topic: HEX Split (Read 1 time) previous topic - next topic


OK.  It seems an odd thing to do but if that is what you want, so be it.

What type of variable is hexString in your example ?  I know what the name is, but that does not mean anything.
How is hexString created ?

Please post the code that you are using to make it easier to help you
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


There is a code, it is IRRemote

This example just turn on and off led if is received by IR right HEX and it is 0x111ABC in my example

But i would like to seperate this HEX to asign every character to variable for to do something with it

1_Value = (firs hex number of my code      --> 1)
2_Value = (second hex number of my code --> 1)
3_Value = (third hex number of my code    --> 1)
4_Value = (fourth hex number of my code  --> A)
5_Value = (fifth hex number of my code    --> B)
6_Value = (sixth hex number of my code   --> C)

example what does do every value:
if(1_Value == '1')  ----> turn ON the LED
if(1_Value == 'A')  ----> turn OFF the LED 
if(6_Value == '1')  ----> turn ON the ...
if(6_Value == 'A')  ----> turn OFF the ...

Code: [Select]

#include <IRremote.h>
int RECV_PIN = 11;
int OUTPUT_PIN = 13;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
  irrecv.enableIRIn(); // Start the receiver
int on = 0;
unsigned long last = millis();

void loop() {
if (irrecv.decode(&results)) {
  Serial.println(results.value, HEX);
    if (results.value == 0x111ABC) {     
        if (millis() - last > 100) {
        on = !on;
        digitalWrite(OUTPUT_PIN, on ? HIGH : LOW);
      last = millis();
    irrecv.resume(); // Receive the next value


Try this:
Code: [Select]

uint8_t read_nibble(uint32_t data, uint8_t ix)
  return ((data >> (ix * 4)) & 0xf);
  for (uint8_t i = 0; i < 8; i++)
    Serial.println(read_nibble(0x12345678, i), HEX);

Please not that the nibbles are numbered from 0..7, from right to left. Changing the order I leave as homework.



I am not a very good programmer.
Can you please show in my example where i need to put your solution?

But i still doesn't understand how this will asign my HEX every char to Variable?
May be you can place all code, please?


then i can use it like this:
if (5_HexNumber == D)
   digitalWrite(LedPin, HIGH);
   digitalWrite(LedPin, LOW);

Now you can write this as:
Code: [Select]

if (read_nibble(results.value, 5) == 0xD)

You will just have to figure out the numbering or change read_nibble() to the number scheme you want for your code. So I think we are done here?

Go Up