# Very stupid code question regarding millis()

When run, will it not return:

``````True
True
True
True
``````

?

What is "it", and where do you expect "it" to return to?

By reference I mean something the Arduino can compare something else to. If not a reference, what is it for?

From what I can see, the Arduino runs the `gesture` through the `if` and returns a value of either true or false. What I'm trying to figure out is what would be considered "true" coming out of the loop.

EDIT - Wait, it looks like it actually sends out multiple values. I'm less confused, but I still don't know what it does.

It's a comparison function! If `gesture` equals `inputMatchup` then it returns true, and if it doesn't, then it returns false. And then if it returns true, it executes that first `if()`.

EDIT - Right?

A reference has a very specific meaning in C++; a reference is a little like a pointer, but is considered safer than a pointer

I know what a reference is in code. I just forgot they existed when I wrote that.

No, it returns either false (as soon as it finds a mismatch) or true.

I was thinking aloud.

Maybe think to yourself for a little longer before committing to keyboard.

1 Like

Would it help if I put in some redundant curly-brackets?

``````boolean matches(int a[4], int b[4])
{
for (int i = 0; i < 4; i++)
{
if (a[i] != b[i])
{
return false;
}
}

return true;
}
``````

And turned the 'for' loop into a 'while' loop?

``````boolean matches(int a[4], int b[4])
{
int i = 0;
while ( i < 4)
{
if (a[i] != b[i])
{
return false;
}

i++;
}

return true;
}
``````

And use just basic integer operations?

``````int matches(int a[4], int b[4])
{
int i = 0;
while ( i < 4)
{
if (a[i] != b[i])
{
return 0;
}

i = i + 1;
}

return 1;
}
``````
1 Like

@johnwasser maybe write it again using goto.

Or unroll the loop.

a7

``````boolean matches(int a[4], int b[4])
{
int sum = 0;
for (int i = 0; i < 4; i++)
{
sum += abs (a[i] - b[i]);
}
return sum == 0;
}
``````

I suggest

``````boolean matches(int a[4], int b[4])
{
unsigned long long longLongA = * (unsigned long long *) a;
unsigned long long longLongB = * (unsigned long long *) b;

return longLongA == longLongB;
}
``````

a7

...it won't work on an ESP32, for example.

Good idea!

``````int matches(int a[4], int b[4])
{
if (a[0] != b[0])
{
return 0;
}

if (a[1] != b[1])
{
return 0;
}

if (a[2] != b[2])
{
return 0;
}

if (a[3] != b[3])
{
return 0;
}

return 1;
}
``````

Your first revision makes the most sense to me.

Just remember that 'for', 'if/else', 'while', and 'do/while' statements can only contain one statement, but that statement can be a block statement (list of statements surrounded by '{' and '}').

Do they end up the same in the compiler? With and without the redundant brackets, I mean.

I would expect the generated code to be the same.

1 Like

Okay. I used your code and modified it for all of the hand gestures I want to use in my wearable computer. Anything wrong?

``````int gesture[4];
int inputMatchupRightClick[4] = {HIGH, LOW, HIGH, HIGH};
int inputMatchupLeftClick[4] = {LOW, HIGH, HIGH, HIGH};
int inputMatchupSpace[4] = {HIGH, HIGH, LOW, HIGH};
int inputMatchupEnter[4] = {HIGH, LOW, LOW, HIGH};
int inputMatchupCaps[4] = {LOW, LOW, HIGH, HIGH};

boolean matches(int a[4], int b[4])
{
for (int i = 0; i < 4; i++)
if (a[i] != b[i])
return false;

return true;
}

void setup()
{
Serial.begin(9600);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
}

void loop()
{
static unsigned long lastUnmatchTime = 0;

if (matches(gesture, inputMatchupRightClick))
{
if (millis() - lastUnmatchTime >= 50)
{
Serial.println("right click detected");
}
}
else if (matches(gesture, inputMatchupLeftClick)
{
if (millis() - lastUnmatchTime >= 50)
{
Serial.println("left click detected");
}
}
else if (matches(gesture, inputMatchupSpace)
{
if (millis() - lastUnmatchTime >= 60)
{
Serial.println("space detected");
}
}
else if (matches(gesture, inputMatchupEnter)
{
if (millis() - lastUnmatchTime >= 60)
{
Serial.println("enter detected");
}
}
else if (matches(gesture, inputMatchupCaps)
{
if (millis() - lastUnmatchTime >= 60)
{
Serial.println("caps detected");
}
}
else{
lastUnmatchTime = millis();
}
}
``````