Very stupid code question regarding millis()

I need to make a mouse input if a set of data is a certain way for a certain amount of time.

Here's an example of what I don't want:

int gesture[3];
int inputMatchup[3] = {1, 0, 1, 1}

gesture[0] = digitalRead(3);
gesture[1] = digitalRead(4);
gesture[2] = digitalRead(5);
gesture[3] = digitalRead(6);

if (gesture[] = inputMatchup[]}{
 <do a thing>
}

This is for gesture control, so with this code, the Arduino could take in input accidentally by me moving my hand.

So what I want is for the Arduino to only do a thing if gesture[] = inputMatchup[] for 50ms or more.

How do I do this? Use millis()? How?

Check this out: millis() - Arduino Reference

I have looked there.

Be careful when comparing values... usually you need == rather than =.

millis() returns the number of milliseconds since startup. You'll need to store this value when your condition is true... then keep checking it ... and compare to your original value. When more than 50ms... then "do a thing".

So what's the problem?

I don't have the IDE open, just wrote in the forum. Also, the parentheses types in the if() are mismatched.

So how would I take a "snapshot" of a millis() value?

1 Like

Code is my crappiest quality as a maker. I'm still only partway through learning Arduino.

By saving the return value to a uint32_t

When the required condition becomes true (not is true) save the value of millis() then each time through loop() if the condition is still true check using the current value of millis() minus the start value whether the required period has passed

unsigned long time = millis();
uint32_t snapshot;
uint32_t compare = 50;
if (gesture[] = inputMatchup[]}{
 uint32_t snapshot = millis();
  if (snapshot - time > compare){
   <do a thing>
  }
 }

This is wrong isn't it?

Yes

You need to detect when the condition becomes true not when it is true

See the StateChangeDetection example in the IDE

Yes. Partly because you can't compare two arrays with a comparison operator.

Well- this is a theoretical.

unsigned long time = millis();
uint32_t snapshot;
uint32_t compare = 50;

if (gesture != lastGesture){
 if (gesture == inputMatchup){
  snapshot = millis();
  if (snapshot - time > compare){
   <do a thing>
  }
  else{
   <don't do a thing>
  }
 }
}
else{
 <don't do a thing>
}

Then it is wrong because 'time' is getting set to millis() just before checking to see if (millis() - time > 50). It will not be.

Try something like this:

int gesture[4];
int inputMatchup[4] = {HIGH, 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()
{
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
}

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

  gesture[0] = digitalRead(3);
  gesture[1] = digitalRead(4);
  gesture[2] = digitalRead(5);
  gesture[3] = digitalRead(6);

  if (matches(gesture, inputMatchup))
  {
    if (millis() - lastUnmatchTime >= 50)
    {
      // Do a thing
    }
  }
  else
    lastUnmatchTime = millis();
}
1 Like

So what does the for() loop do?

I don't like the just downloading code without knowing what it does.

EDIT - The only time I've used for() loops in code before this was when I learned QBASIC in 6th grade. I wish they still used it instead of this "Scratch" thing.

Yes, it seems you have a theory.

My theory is that you should just stop flailing and go learn some programming.

It will take less time and leave you better prepared than asking "very stupid question[s]" and getting information a bit and piece at a time over many hours and many ppl trying to help you.

There are plenty of examples, tutorials, videos, books &c.

The techniques for dealing with your issue are widely used, find some good examples and bend them to your will. Easier if you can program confidently, which includes reading and writing. Code.

a7

Is there a clue in the name of function that includes the for loop?

Ohhhhh, it's a reference. Is this the correct way to compare arrays?

I can't see a reference.
I don't know what leads you to conclude that it is.