Hold button for 5 seconds before making LED high (SIMPLES WAY)

Hi,

I want to hold down my button for 5 seconds before the led on output 13 turn on.
So a 5 seconds delay. If i let it go in those 5 seconds the led shouldnt turn on.

I want to avoid millis() for this one. Just a delay() function.

Is this possible?

Is this an introductory tutorial?

Yes, but why would you want to?

Im so sorry if this topic is at the wrong location!

To answer your second question:
Because it is litteraly the only function in my program i need. And im sorry, i meant 5ms! Not 5 seconds!

in the future the switch will be replaced by a sensor btw, so thats were the 5ms are coming from. 5ms is not noticable for the human eye. But in order to test it if it works i use a switch

Do you have an example?

Yes, it is possible but weird. Just check state of button. When state changes (it is pushed) start a delay. Once delay is over check state again and if still pushed then activate ledOn function.
Very strange way to do it and will have all the problems associated with delay such as it won’t be able to do anything else while the delay is running and it will ignore any state changes while the delay is running.

You could poll the button multiple times by doing eg 5 one second delays and check state each time. This could be done in a loop with a counter incrementing after each second of delay.

This would give more accuracy in picking up state changes but the whole thing is a very strange way to write in a delay when a delay is to be avoided and there is a perfectly good solution without a delay that will pick up millisecond changes in state

Your topic was MOVED to its current forum category as it is more suitable than the original

Why ?

It is better to use the right tool for the job

This is a xy problem.
You want to achieve x
You think y will do this
You don’t know how to do y
You ask how to do y when you should ask how to achieve x

Read the forum guidance at the top of each section. Provide the right information and ask the right questions and you will get where you want to go without doing weird stuff

That won't prove that the button wasn't released and re-pressed during the delay() though.

Looking at OP's history, I see they had an aversion to millis() 2 years ago, in order to do some coding on Arduino for a PIC.

That makes no sense as a reason. The code to do this delay()-lessly is trivial.

Yes, I know
That’s why I suggested polling more often

Eventually you poll so often that you are just using millis :joy:

And I was pretty sure you knew; my comment was more for the OP's benefit.

Is it even possible to press and release a button inside 5ms?

edit- ignore that, I just saw this:

There is a strangeness to this forum for newbies who are so excited they don’t want to take the time to read the guidelines and do things right. I remember when I first came on here and was annoyed that nobody would answer my question. Everyone knew my question was the wrong question but me. I just wanted the answer instead I got taught how to do it correctly and thus I learnt how to do things better and ask better questions in the future.
:wink:

The switch is not a good test as it will bounce and need debouncing code. The delay may do this function. Why not post x. I.è what you hope to achieve, a schematic and code in tags

Okay, in my previous topic i wasnt very detailed in my line of questioning.

Here is my detailed question:

I have a wiper system which uses parkingsensors (this is mounted on my boat). The sensor is 'high' as soon as the wiper touches the sensor.
Unfortunatly, the cables for the wiper motor and the parking sensor are in the same 8 wire- cable.
The PWM signal that goes trough the cables for the motor is interfering with the parkingsensor cable. I am having spikes on this cable of about 35uS (microseconds) and my microcontroller detects this spike. So it stops the motor because it thinks that the motor has reached the parking sensor.

Now, i want to make the input of the parkingsensor on the microcontroller a little bit slower. So it has to see the signal of the parking sensor for about 5ms, before registering this as a 'high' signal and stopping the motor.

I want to avoid millis() because i am not using the arduino, but a PIC, and there is no millis function in the PIC IDE (MPLAB X with MCC compiler). And Timers are messing up my program. So i figured that a 5ms delay in which the condition of the signal may not change would be the solution.

Here is the code i have for the sensors: (i have 4 wipers with 4 sensors)

 SensorState1 = SENSOR_A_PORT; 
    SensorState2 = SENSOR_B_PORT; 
    SensorState3 = SENSOR_C_PORT; 
    SensorState4 = SENSOR_D_PORT; 
    
        if(SensorState1 != lastSensorState1){
            if(SensorState1 == 1){
                RA4PPS = 0x00;
            }
        }
        lastSensorState1 = SensorState1; 
        
        if(SensorState2 != lastSensorState2){
            if(SensorState2 == 1){
                RA5PPS = 0x00;
            }
        }
        lastSensorState2 = SensorState2; 
        
        if(SensorState3 != lastSensorState3){
            if(SensorState3 == 1){
                RA6PPS = 0x00;
            }
        }
        lastSensorState3 = SensorState3; 
        
        if(SensorState4 != lastSensorState4){
            if(SensorState4 == 1){
                RA7PPS = 0x00;
            }
        }
        lastSensorState4 = SensorState4;

I am testing the setup with a switch, which will be replaced by a sensor
Unfortunatly i cannot upload pictures, then you could see the spikes on the oscilloscope

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

I know Microchip now own Atmel, but I didn't realise there were PIC Arduinos.

LOL, it isnt. This comment is the reason i didnt gave to much details in the first post. It is a programming question, it doesnt really matter if i am using a PIC or Arduino or ATMEL etc. etc. That is also the reason i want to avoid millis(), because i cannot use it :slight_smile: due to the fact that im not using an arduino.

I understand that this is the arduino forum, but general programming questions like this are helpful for every programmer, doesnt matter which brand they are using

But PICs (you didn't say which one) have timers, don't they?

Right so, not sure I have this right but,
You have a hardware problem (interference on a sensor line) and your way of dealing with this is to obfuscate the problem by stating a different problem on a forum to achieve a solution to a problem you don’t have??

Doesn’t sound overly sensible to me. Why don’t you just clean up the signal on the line?

a simple low-pass-filter should be able to filter out these short spikes.

35 µseconds are afrequency of 1/35µs = 28571Hz

5 milliseconds is 200 Hz
This means at 200 Hz the frequency should pass through the low-pass-filter with almost no loss

https://www.trance-cat.com/electrical-circuit-calculators/en/rc-low-pass-filter-calculator.php


This is a lowpass-filter with border-frequency around 400 Hz
If you have an oscilloscope handy you can take a look at the output-signal

write a testprogram that creates 35µsec long single pulses and then watch the damped signal on the output

different question:
what would be so hard to use some kind of Arduino-compatible microcontroller ?

best regards Stefan