I have completed a small project I did to challenge myself to learn more about my arduinos programming. In this project, inside a for loop I have to check whether a MyVar byte fetched from an array of bytes, is "empty" (or 0
, or B0000000
what applies best). Rough description of the program in note 1.
I did a lot of trial and error, and quickly realized that just asking
byte MyVar = B0001111
if {MyVar = 0 ; DoSomethingSpecial() } else {ProceedLoop()}
did not produce anything near the desired state. I deduced that testing bytes probably need to happen bitwise, or something clever (=beyond my understanding) is going on in how the compiler interprets the conversion.
So I created a dummy variable byte
byte EmptyValue = B0000000
and then did a logical AND
between the MyVar and the EmptyValue
if (MyVar&EmptyValue) {DoSomethingSpecial(); } else {ProceedLoop(); }
which seemed to do the trick - it was getting late and my ability to find useful Serial.print() to capture the different values was getting low, but the results running the program were at the time indistinguishable to me from the expected way.
My question, firstly - is this a reasonable way to check if a byte is zero? Does it actually do that? I have been reading up on these bitwise calculations and I am truly confused to how it compares a final TRUE
or FALSE
from comparing.
Secondly, what would be a more processing-time-optimal way of doing this comparison? Is there a null-test of sorts? Should I have cast it into integer and tested against integer 0?
const int interval=400;
const byte ByteArrayMakeSound[11]={
B0001111, //H - length 4
B0001111, //E - length 4
B0001111, //L - length 4
B0001111, //L - length 4
B0000111, //O - length 3
B0000000, //
B0000111, //W - length 3
B0000111, //O - length 3
B0000111, //R - length 3
B0001111, //L - length 4
B0000111 //D - length 3
};
const byte ByteArrayLongShort[11] = {
B0001111, //H = --- --- --- ---
B0000001, //E = o o o ---
B0001011, //L = --- o --- ---
B0001011, //L = --- o --- ---
B0000000, //O = o o o
B0000000, // (pause 7 intervals)
B0000100, //W = --- o o
B0000000, //O = o o o
B0000101, //R = --- o ---
B0001011, //L = --- o --- ---
B0000011 //D = o --- ---
};
void setup()
{
pinMode (LED_BUILTIN, OUTPUT);
}
void loop()
{
for (byte x = 0; x < 10; ++x) {
byte sound = ByteArrayMakeSound[x];
byte longshort = ByteArrayLongShort[x];
byte emptyvalue {B0000000};
if (sound&emptyvalue) {
delay(7 * interval);
} else {
for (byte y = 0; y < 7; ++y) {
if (sound & (1<<y)){
if (longshort & (1<<y)) {
MakeSound(3);
delay(interval);
} else {
MakeSound(1);
delay(interval);
}
} else {}
}
}
delay(3 * interval);
}
}
void MakeSound(int del) {
digitalWrite(LED_BUILTIN, HIGH);
delay(interval * del);
digitalWrite(LED_BUILTIN, LOW);
}
Note 1: Program purpose
The program started as a hello world program with me trying to write "hello world" in morse code with the builtin led. The two byte arrays store the "letter length" of the morse and the "daa" / "dit" assignment. I use empty bytes to separate words, which means a delay of 3 intervals (= length of "daa") should happen. Then including the entire letter-table of morse and a string to byte table conversion (to create the byte arrays). It is not very quick... But I am tinkering with moving the byte arrays into the flash memory. Just for Hello World it is fast enough, but the "daas" and the "dits" are slightly uneven so programming time is impacting performance.