Pages: [1] 2   Go Down
Author Topic: HEX Split  (Read 1372 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to split or seperate a HEX, for example I have HEX value received from IR Remote and value what i get is 1FE3DC
So I need to split every hex. How I can do that? I would like something like this.

I have this:
hexString = 1FE3DC;

I need asign like this!!:
1_HexNumber = 1    (first number of my hex)
2_HexNumber = F    (second number of my hex)
3_HexNumber = E    (third number of my hex)
4_HexNumber = 3    (fourth number of my hex)
5_HexNumber = D    (fifth number of my hex)
6_HexNumber = C    (sixth number of my hex)

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

0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hexString[ 0 ] has value '1'
...
hexString[ 5 ] has value 'C'
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for answer!
But can someone write full code please?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4255
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you need to actually split the hex string or just perform actions based on the value of a particular digit ?
« Last Edit: March 10, 2013, 06:53:49 am by UKHeliBob » Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because I want to do something depend from values.

if i have HEX ----> 123ABC

Asign each HEX number to value
String1 = '1'
String2 = '2'
String3 = '3'
String4 = 'A'
String5 = 'B'
String6 = 'C'

then i can use every sended HEX number to do something

if(String1 == '1') -----> turn on a RED led
if(String1 == '2') -----> turn on a GREEN led
if(String1 == '3') -----> turn on a BLUE led
....
 
if(String2 == '1')  ----> turn on a motor 
if(String2 == '2')  ----> turn off a motor   
if(String2 == '3')  ----> turn .....
....

Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4255
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#include <IRremote.h>
int RECV_PIN = 11;
int OUTPUT_PIN = 13;
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup()
{
  Serial.begin(9600);
  pinMode(OUTPUT_PIN, OUTPUT);
  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
  }
}
Logged

Sweden
Offline Offline
Sr. Member
****
Karma: 11
Posts: 451
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:
Code:
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.

Cheers!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Sweden
Offline Offline
Sr. Member
****
Karma: 11
Posts: 451
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

then i can use it like this:
if (5_HexNumber == D)
   digitalWrite(LedPin, HIGH);
else
   digitalWrite(LedPin, LOW);
Now you can write this as:
Code:
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?
Cheers!

Logged

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

OP, I think you are barking up the wrong tree. Each button on the remote sends a unique code. There is no discernable pattern to the value sent, though. So, you won't be able to predict which button to press to cause a specific LED to light up.

Explaining WHAT you want to do with the remote, rather than HOW you think you should do it, would be useful.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So i don't understand where i need to put this

uint8_t read_nibble(uint32_t data, uint8_t ix)
{
  return ((data >> (ix * 4)) & 0xf);
}

may be you can stisk your solution with my code what i post?
I would like all code sticked together not only peace of code. I am simply a newbie.

May be there is some easier way without uint8 ....
I think all that could be done with Array[] function, or may be i wronk?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My major wjat i need is split HEX to peaces

for example i receive  AC5D7A HEX code
i need to asign every leter like unique

So, if i receive for example code AC5D7A
then i want use every peace of it seperately
Value1 = A
Value2 = C
Value3 = 5
Value4 = D
Value5 = 7
Value6 = A

ok, it's doesn't metter or it is HEX or DEC
May be i can use a DEC
if i receive 123976
then asign this values as seperated
Value1 = 1
Value2 = 2
Value3 = 3
Value4 = 9
Value5 = 7
Value6 = 6






Logged

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

You can use sprintf() to convert a numeric value to a string, in whatever format you like, including HEX.

Then, you can pick apart the characters in whatever way you want. Given that you may get AC5D7A when pressing the '1' button and 123976 when pressing the '2' button, how is that useful, though? You'd have to create a table showing the button to press and the resulting hex code, in order for the user to know which button to press to make, for instance, pin 7 go high.

The result on the Arduino side will be a series of 6 to 10 actions that happen when any given button on the remote is pressed. How converting the value to a HEX string and performing each action individually is better than performing the set of actions because the value was AC5D7A is what I am having trouble understanding.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wnat something like this but on Infra red
http://www.pjrc.com/teensy/td_libs_VirtualWire.html

If it is send "Hello"

Then it receive Hello but splited
using buf


for example if it will be a remote control car, where every character has something meaning.
First sended char is changed tepend of joistic value 0-255 for speed
If joistic is at midl the value is 127, if full forward 255, if full backward then 0.

Second sended char is changed tepend of joistic value 0-255 for steering
left 0, midl 127, right 255


then receiver need to receive every char seperately for control of speed by first char (forward speed and backwardspeed)
Second char (Steering Left and steering right)
Code:
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
int i;

        digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
// Serial.print("Got: ");

for (i = 0; i < buflen; i++)
{    
             c = (buf[i]);
}

      joystic_y= (buf[0]);
      joystic_x = (buf[1]);
      light_1 = (buf[3]);

I want the same but in IRRemote
Idon't know how to make buf of my received char or dec or hex to asign it to Variable, this is my major problem.
Logged

Pages: [1] 2   Go Up
Jump to: