Is there a way to code a recheck without milis

I have a program that works well…. But…. It will have occasional mistriggers. These seem to be related to noise from relays and from power wires inside of the same insulated jacket running parallel to signal. I know that’s against best practices but I simply can’t isolate them for this project. Is there a way to write code that upon triggering delay 300 and recheck? What would that look like? Sample code line would be
if (digitalRead (9) == low);
“In between”
digitalWrite (5, low);

If someone could help with how to best write the “in between” would be greatly appreciative.

Thanks!!!

Yes.

unsigned long lastMicros = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  static unsigned long currentMicros = micros();

  if (currentMicros - lastMicros > 300000L) {
    lastMicros = currentMicros;
    Serial.println("Look at me, I'm Mr. Meeseeks!");
  }
 
}

Sorry, I couldn't resist.

1 Like

So this is a joke? Do you have a helpful answer also?

Do you have anything that can trigger an interrupt? If not, then your best possible option would to have a (semi) controlled delay with either millis or micros. (You could also use a For loop of NOP in assembly...) Is there any reason why you don't want to use millis?

Are your wires properly shielded? Can you move the relays further away from the wires to prevent the noise?

I’ve spent about 20 hours trying to combat this using hardware.

I don’t understand milis very well and would prefer a simple copy and paste digitalRead delay recheck then trigger, code solution. Is this not as simple as I think it would be? I’m really more of a code modifier than writer. The more I modify the more I learn. I’m under the gun on making this work well and I am confident that my requested solution would work well. I am not trying to skin the cat the way others want it skinned. I’d prefer to skin it the way I would understand.

Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

2 Likes

The code I gave was both a joke and a viable solution to your problem. It constantly prints out "Look at me, I'm Mr. Meeseeks!" every 300 milliseconds. (or 300,000 microseconds). You can change this value to suit your needs. All you would need to do is replace the Serial.println() with the code you need to actually check N many milliseconds. Simple as that. Now if there are hardcoded delays in your code (please post what you have btw), this will not allow the check to properly trigger when needed.

Is there some way that this would change my request? I’m not looking to reengineer everything. I know enough to know that what I am asking for would accomplish the desired effect. I would very much appreciate help with the exact question asked rather than trying to spend 12 hours showing the complexities of my hardware and more than 400 wires and more than 30 pieces of hardware. I’m not trying to be difficult. I’m trying to keep it simple. I just want to add a delay and recheck. The noise is gone rather quickly and I know my solution will work. In two weeks when the dust settles… I’ll gladly come back and post pictures and cut sheets for every component I’m using. In the mean time I would greatly appreciate assistance in adding the requested delay and recheck.

I appreciate your help. I don’t really understand how to implement your solution in my circumstance. Is there any way you could literally write it in a way that is in between my read and write aspects? I don’t understand how to use the proper syntax to accomplish read pause recheck trigger. Please can we just keep it a simple delay? My brain fully comprehends a simple delay.

  • We are sure you have a great project here, however, we cannot really give much advice when we don’t have an idea what hardware is involved, if you have properly powered modules, if you have SWC and inductive kickback protection and a myriad other common things that people leave out of their wiring.

Good luck.

1 Like

It’s a simple question. Can you help me write in a delay and recheck or not? If this isn’t possible say it isn’t possible. If outside of your abilities. Say that. To spend hours showing and explaining every aspect of my project seems like a large investment for what seems like a simple request. I’m not holding anyone liable if it doesn’t solve my problem. No one is going to be sued over any advice given. Does it have to be a huge ordeal?

How about this?


unsigned long lastMillis = 0;
unsigned long delayInMilliseconds = 300;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(9, INPUT_PULLUP); // Assuming you have it wired with a pull up resistor
  pinMode(5, OUTPUT);
}

void loop() {
  static unsigned long currentMillis = millis();
  digitalWrite (5, HIGH); // probably best to have it be something other than LOW all the time

  if ((currentMillis - lastMillis > delayInMilliseconds) && digitalRead(9) == LOW) {
    digitalWrite (5, LOW);
    lastMillis = currentMillis;
  }

}

Also LarryD is correct, we can only do so much with the data we are given.

1 Like

I get that. But this seems like a simple variation of a button debounce that doesn’t require intimate knowledge of every aspect of a project that contains hundreds of wire connections and dozens of pieces of hardware. I’m not asking for help diagnosing. I’m simply asking for a specific solution in a way that I can understand best. That way uses delays. Which again. I would highly prefer because milis simply makes my brain hurt. Could you humor me and try to use a delay instead of milis?

What I gave IS a nonblocking delay.

If you want a blocking delay, try this.

if (digitalRead (9) == LOW) {
  delay(300); // This will block the rest of your code from doing anything else for 300 milliseconds.
  digitalWrite (5, LOW);
}
1 Like

Also. I’m on my phone. So copy and pasting my code isn’t really an option or I would do that. Thanks again for helping and not being a troll like I’ve seen so many people on here be.

I do understand that delay literally ceases all action's. But. What you wrote simply adds a delay to the event of the trigger. I don’t need a delay between the low and the trigger. I need a read then a delay… then a recheck to make sure the signal is real. Then if it is in fact real… then trigger. Think of it like reading signals from space. The first thing they have to do is turn the satellite dish away and then back to confirm it’s coming from space… same kinda thing I’m trying to do. Avoid a false positive.


unsigned long lastMillis = 0;
unsigned long delayInMilliseconds = 300;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(9, INPUT_PULLUP);
  pinMode(5, OUTPUT);
}

void loop() {
  static unsigned long currentMillis = millis();
  static bool initialRead = digitalRead(9);

  if (currentMillis - lastMillis > delayInMilliseconds) {
    if (digitalRead(9) == initialRead) { // if after the delay and the two reads match, do something
      functionToCheck();
    }
    lastMillis = currentMillis;
  }

}

void functionToCheck() {
    // Do something here
}

Fixed the compile issue...

Maybe yes maybe no, without knowing what you have that is the best answer you will get. From your description you have a design, layout and assembly problems and a lack of understanding of electrical/electronic interference. Your fix is a kludge that will still give you problems over the long term.

The best thing you can do is start from scratch on the hardware side and do it properly. Your problem could be very simple or very complex but you are not in a position to properly make that decision. If you post your secret annotated schematic you might get some help fairly quickly. Without that we will be doing as you are just guessing and hope something sticks.

1 Like

That if-statement is unhealthy because of the following ";" -- if it's false, it skips to the next line, and if it is true, it also skips to the next line. And B: 'low' should probably be "LOW".

What does recheck mean? What's it supposed to do if it fails the recheck?

From the words you say, the best I understand is that it is supposed to recheck for 300ms and then make a decision.

How about a loop full of rechecks and then a decision like this completely untested snippet:

if (digitalRead (9) == LOW){
  int count = 0; 
  for(int ii = 0; ii < 300 ++i){ // recheck for 300ms
    if(digitalRead (9) == LOW){
      ++count;
      delay(1);
    }
  }
  if(count > 150) { // pass or fail?
    digitalWrite (5, low);
  }
}

I’m not asking for anyone to guess. I’m asking for

Read
Delay
Recheck
THEN trigger

Everyone on here always seems really interested in showing their superiority. Prove you are superior by writing a code that if it receives a low (call it a button press) that it will shut itself down for 300 milliseconds and then recheck to see if that same button is pressed. Then, and only then, it will trigger a high on pin 5. Is that code something that can be written without milis? Are you smart enough to find a way to accomplish that code?