I have 20 variables that are encoded as WORD data types in Arduino. I am trying to represent them as 20 tags. I don't need the actual data in the WORDs, I just need to differentiate between them, so I could use BYTE, ASCII or INT values 0 - 19. I have been trying to do something like
if (wordVariable = 1){
byteVariable = 1
}
I can't get it to work though because I don't know the structure of a WORD variable. How are they formatted? 16 binary numbers? or something like B10010B10010? Is there a function that will convert the word to INT or a SHORT to make it easier to work with?
"wordVariable = 1" will put the value 1 into wordVariable
It will always be true because 1 is true, so it will always do "byteVariable = 1"
[edit]= means assign the value to,
== means test for equality[/edit]
What are the values of wordVariable?
I'm not dealing with words as in parts of a sentence. I'm dealing with a data type called a WORD, which is apparently the same as an unsigned int (thanks Paul S).
I'm reading the reference on unsigned int now, but I'm still a little confused. If my WORD variable is printing ASCII "1" to the serial monitor, how is it structured as an unsigned int or word? In the reference it says it stores a range of 0 to 65,535, but what is the actual format? I know ASCII 1 doesn't equal unsigned int 1. And it's not 49 (the ASCII value of 1) either. I just want to take WORD (unsigned int) values and label them as bytes 0 through 20.
koleeko - would you please post code, a very small example that compiles correctly would be fine, that uses these data types and variables?
You keep writing WORD, but AFAIK there is no WORD data type in the Arduino libraries on the Arduino.cc site.
A word (not WORD) is 16 bits stored as an unsigned binary number.
(As opposed to a signed, 2's complement, or sign magnitude form)
if you code:
unsigned int foo = 1;
Serial.println(foo);
you will see an ASCII '1' printed in the Serial monitor.
By default, Serial.print/println assume you want an unsigned int printed as a decimal number representation. I does the work to translate to the ASCII string. It may be helpful to look at Serial.print() - Arduino Reference to see if there is a format which would be more helpful (e.g. Hexadecimal)
So, if the value is of type word (not WORD), which are unsigned int, then the conversion from unsigned int to byte is:
unsigned int w = ...;
...
byte b = (byte)w;
Paul S.
I'm actually using the bitRead function to get the bits into openFrameworks. The problem is that by sending from Arduino through the serial port, the values become ASCII. This means that a value of 1 is received as 49, but a value of 256 is received as 50/53/54. I want to have a consistent number of bits coming into oF. And since I only have 21 different values, I'm trying to relabel them as values from 0 - 20. I tried to do this as a series of if statements ( I realize I could also use an array or a for loop). For some reason when I say print Serial.print (WORD); I get numerical values of 0,1,2,4,8,16,32,64 etc, depending on what data is coming from the sensors. But if I say
if (WORD = 16){
Serial.print (5)
}
or
if(WORD = 4){
int newVariable = 3;
print (newVariable);
}
Thanks gbulmer. Once I changed my if statements to have "==" instead of "=", i was able to easily reassign all my unsigned ints to new values 0 -20. Then I Serial.print them as BYTEs and they show up in openFrameworks as single byte values.