Adding Hex numbers from 00 to FF in Arduino C++

Thread Starter

john2k

Joined Nov 14, 2019
219
I'm new to Arduino C++ programming. Inside my voice loop function I want to add to the previous hex number value. So for it starts with 00 and adds 1 each time in the loop the sequencing all the way through to FF and then back to 00 and all over again and again. Can anyone kindly tell me how this can be done? Thanks.
 

Thread Starter

john2k

Joined Nov 14, 2019
219
hi john,
Any reason you are counting in Hex and not Decimal.?
E
Because I need to put a sequential hex counter into a canbus message which is transmitted in the loop message. So basically a can bus message is transmitted in each loop but each time it needs a hex counter. If I count in decimals then i would have to convert it to hex to put it into the can bus transmit message
 

Ian Rogers

Joined Dec 12, 2012
1,136
200 or 11001000 or 0xC8 are exactly the same... They are representing integer , whole numbers
The radix has nothing to do with addition...

Are you talking about BCD binary coded decimals?? This is a different counting system all together..
 

WBahn

Joined Mar 31, 2012
29,976
I'm new to Arduino C++ programming. Inside my voice loop function I want to add to the previous hex number value. So for it starts with 00 and adds 1 each time in the loop the sequencing all the way through to FF and then back to 00 and all over again and again. Can anyone kindly tell me how this can be done? Thanks.
How are these numbers being represented? In an int variable? In a string? As a BCD variable?

How are they being represented in the CANBUS packets?
 

Thread Starter

john2k

Joined Nov 14, 2019
219
How are these numbers being represented? In an int variable? In a string? As a BCD variable?

How are they being represented in the CANBUS packets?
All I know is that there are 8 data bytes in the message being sent. I am able to then put a value anything from 00 to FF in these bytes. But I am only interested in putting the counter in byte 5 for example. I can hard-code anything from 00 to FF as long as it's 2 characters long it accepts it. It's just that I need the values to be sequential. I can get a interger to count from 1 upwards but I need it to be a 2 character hex
 

Thread Starter

john2k

Joined Nov 14, 2019
219
So I got it working using the following code. However, it doesn't seem to stop at 255 after FF the counter seems to keep trying to go over 255

byte counter = 1;

void loop() {
print(counter, HEX);
counter = counter + 1;
}
 

dl324

Joined Mar 30, 2015
16,841
However, it doesn't seem to stop at 255 after FF the counter seems to keep trying to go over 255
You don't have any code to prevent wraparound.

BTW, you can simplify the increment to "counter++". That's easier to read and helps avoid problems with typing mistakes.
 

Thread Starter

john2k

Joined Nov 14, 2019
219
BTW, you can simplify the increment to "counter++". That's easier to read and helps avoid problems with typing mistakes.
I've put counter++ instead of counter = thanks

hi john,
Your program clip works for me.
hi-jacked the Blink.ico
E
I've figured out why mine wasn't working, I have to put println instead of print. What is the difference between the two prints and when it returns to 0 to 9, is there any way to have it be 00, 01, 02 etc so that it's always two characters long?
 

dl324

Joined Mar 30, 2015
16,841
is there any way to have it be 00, 01, 02 etc so that it's always two characters long?
Use a formatted print statement (printf). Be aware that printf is a costly command for Arduino's and that it doesn't support floating point numbers (silently, so you need to read the documents).
 

Thread Starter

john2k

Joined Nov 14, 2019
219
how are they used and sent to the OSWD.?
OSWD? sorry still very new to all the :) Haven't figured next step out yet but going to play around with it

Be aware that printf is a costly command for Arduino's
Talking of costly commands for Arduinos, is there a list of commands that are very intensive that should ideally be avoided? Also once I compile my code, is there a way to check if it's not too intensive and going to stress out the arduino?
 

dl324

Joined Mar 30, 2015
16,841
Also once I compile my code, is there a way to check if it's not too intensive and going to stress out the arduino?
The IDE will tell you how much FLASH and RAM you're using.

Talking of costly commands for Arduinos, is there a list of commands that are very intensive that should ideally be avoided?
printf is the only command I noticed being costly.

Memory is also limited, so you want to use the smallest primitives you can for variables. I've seen many examples where byte or integer arrays were used to hold data that could be packed to use less space. One example I saw was an array used as a look up table for a seven segment decoder. Some used an array of ints, some used an array of chars; I chose to pack the data into chars, so I only needed 16 bytes vs 112 bytes if I had use a byte for each bit.

This is for a binary to hexadecimal decoder.
Code:
unsigned char ary[16] = {
  0x7E, 0x30, 0x6D, 0x79,
  0x33, 0x5B, 0x5F, 0x70,
  0x7F, 0x7B, 0x77, 0x1F,
  0x4E, 0x3D, 0x4F, 0x47
};
Another optimization is to use #defines instead of variables for "variables" that don't change:
Code:
#define WARMPIN 14
#define COOKPIN 13
#define COOKLED 13
#define LDIG 12
#define RDIG 11
#define TIMEPIN 5
For portability, you'd use digitalWrite() to write to the I/O's, but it's faster if you use the PORTs directly:
Code:
// PORTD 0-7, PORTB 8-13, PORTC analog
#define WRITEB(dig)     \
  PORTB &= ~0x07;       \
  PORTB |= (dig) & 0x07;

#define WRITED(dig)     \
  PORTD &= ~0xF0;       \
  PORTD |= (dig) & 0xF0;

void show(int num) {
  WRITEB(ary[num]>>4)
  WRITED(ary[num]<<4)
}
I had to spread my connections to the seven segment display across two ports because I wanted to use serial I/O and that takes 2 bits from the only 8 bit PORT on Uno.
 
Last edited:

dl324

Joined Mar 30, 2015
16,841
Talking of costly commands for Arduinos, is there a list of commands that are very intensive that should ideally be avoided?
I don't think printf() is supported in the IDE because I always used Serial.println(). It's sprintf() that's expensive.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi john,
OSWD is Outside World, basically I am asking how is the HEX value going to be used ie: within the confines of the program or to some external device.?
If externally, how is the HEX going to be communicated to the external device, ie: 8 Bit parallel, Bit Serial , etc.

E
 
All I know is that there are 8 data bytes in the message being sent. I am able to then put a value anything from 00 to FF in these bytes. But I am only interested in putting the counter in byte 5 for example. I can hard-code anything from 00 to FF as long as it's 2 characters long it accepts it. It's just that I need the values to be sequential. I can get a interger to count from 1 upwards but I need it to be a 2 character hex
There is a persistent misunderstanding here about number representation. If you have a single byte variable then that variable can only hold a value between 0x00 and 0xFF (restricting the conversation to positive numbers only). If the number is represented in hexadecimal it will look like 0x0 to 0xFF but have exactly the same value if represented in decimal (0 to 255), octal (0 to 377) or binary (0 to 1111,1111) even though those radix mean it will look different if, for example you printed the number out. The 1's and 0's pattern in the memory of the uC will be exactly the same in every case. And in hex, the representation is two characters but the number is not two 'characters'. In the same way, if it were decimal it would not be 3 characters. It is an 8 bit number and is stored and used as an 8 bit value.
The statement (0xFF = 255) is always true as they are the same value and both are simply 8 bits each.
The 8 bit 'container' is defined when you declare the variable as a 'byte'. And if you continue the count past 0xFF it will simply wrap around (the overflow is discarded) as 0b1111,1111 increments to 0b0001, 0000,0000 but as the thing is a 'byte' that extra nibble with the value 0b0001 does not exist so it is registered as an 'overflow' in the uP status register (probably) which is ignored anyway so you are left with just your original byte holding the value 0b0000,0000 which in hex would be 0x00 which is exactly what you want.
Try not thinking about the numbers as a value range but rather as the container size, be it a byte, a word (2 bytes) etc. Don't even contemplate signed numbers until you have this sorted out in your head. I fear a brain explosion for you otherwise ;-)
 
Top