Go Down

Topic: Help with code for button behavior (Read 348 times) previous topic - next topic

Fabius_rex

Sep 13, 2018, 06:59 pm Last Edit: Sep 13, 2018, 07:00 pm by Fabius_rex
Hi there

I have this piece of code for a button

The idea is that when two conditionals meet (BYPASS = true && button press = true) the affected pin goes HIGH for X amount of time, then OFF. REGARDLESS IF THE BUTTON IS STILL PRESSED OR HAS BEEN REALEASED

This button needs to charge an inductor for the sufficient time then go LOW again so that the magnetic field can colapse. But only ONCE for EACH button PRESS

Here is what I got, but I cannot make it any further

Code: [Select]


pinMode(SPARK_BUTTON, INPUT_PULLUP);
pinMode(EST, OUTPUT);//This is the pin that I need to go HIGH for a "X" amount of time each time the button is pressed and only once for each press.
pinMode(BYPASS, OUTPUT); //If this pin is HIGH, then I can press the button to do something useful, if this pin is LOW then even if we press the button nothing will happen

void Spark_Button()
{

  bool button_status = digitalReadFast(SPARK_BUTTON);
  bool EST_status = digitalReadFast(BYPASS); //This enables or disables the use of the button separate part of the rig

  // check if the pushbutton is pressed.

  if (button_status == LOW && EST_status == HIGH)  // if it is, the buttonState is LOW:
  {

    digitalWriteFast(EST, HIGH);
//Wait for example 2ms, then go low, and do not repeat until next press (button goes high, then low again)
   
 
  }
}


Since this sketch is time sentive, I cannot use DELAY()

Regards

xl97

Your code doesnt compile..  looks like you missing quite a bit.

Also.. what is:  'digitalWriteFast'?  And where does it come from?  I dont see it declared anywhere either?

Checkout Blink without Delay example.. and the switch state examples in the IDE..

This should get you were you need to be without issue.



Fabius_rex

#2
Sep 14, 2018, 03:24 am Last Edit: Sep 14, 2018, 03:25 am by Fabius_rex
Your code doesnt compile..  looks like you missing quite a bit.

Also.. what is:  'digitalWriteFast'?  And where does it come from?  I dont see it declared anywhere either?

Checkout Blink without Delay example.. and the switch state examples in the IDE..

This should get you were you need to be without issue.



The code is quite a lot and do bunch of other things not affected by this function


digitalwritefast is a library to cut down drastically the time it takes for the pin to read/write

I already checked the examples, but cannot put it all toghether

Maybe I need to set a flag for last button state, and if it is the same as actual don´t run the rest of the code

But I still am truncated with the timing conditional. I do not see how I can implement the (millis() - elapsed >= threshold) in this scenario

dougp

In the IDE, check out also file/examples/digital/state change detection.  You need to know not only when the switch is low, but also that it just changed  from high to low.  When the switch changes  start your timer.
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

Slumpert

I would program it like this.

Use 5 variables to track the button. Say A B C D E
Setup A as true
Loop
Button pressed down Checks if A is true then set A to False and Set B C D to true and E to current millis

If B is true, set output pin high, set B false.

If C is true check E to current time, if duration is past, set output pin low, Set C false

If  D is true and C is False And button is not pushed down Set A to true.

Rest of your loop


TomGeorge

The code is quite a lot and do bunch of other things not affected by this function
Do you have some working code that JUST does this function you need, forget the rest and get the button press and output sequence working.
It will make it easier to concentrate on the problem, without other stuff clouding the code.
Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

Robin2

Since this sketch is time sentive, I cannot use DELAY()
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

You can use the same techniques with micros() if you need shorter intervals.

And please post your complete program. If you had done that it would have avoided someone having to ask about digitalWriteFast().

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Fabius_rex

#7
Sep 15, 2018, 12:01 am Last Edit: Sep 15, 2018, 12:06 am by Fabius_rex
Finally after studying the state change detection example and using one of the libraries on my project (everytime.h) I came up with this

Code: [Select]


#include <digitalWriteFast.h>
#include <everytime.h>

bool lastButton_status;
pinMode (EST, OUTPUT);
pinMode (BYPASS, OUTPUT);
pinMode (SPARK_BUTTON, INPUT_PULLUP);

void Spark_Button()
{

 bool button_status = digitalReadFast(SPARK_BUTTON);
 bool EST_status = digitalReadFast(BYPASS);
 // check if the pushbutton is pressed.

 if (button_status == LOW && EST_status == HIGH && button_status != lastButton_Status)  // if it is, the buttonState is LOW:
 {

   digitalWriteFast(EST, HIGH);

 }
 else
 {
   every(300) //When 300ms have passed make the pin go low
   {
     digitalWriteFast(EST, LOW);
   }

 }//Cierra else
 lastButton_Status = button_status;
}


The everytime library works just as the same as "if (millis() - lastmillis >= elapsed)" but way much simpler.

Seems to work just fine

Thanks guys for the insight and motivation.

Regards

Paul__B

I find the conjunction of "digitalWriteFast" and a pushbutton to be an oxymoron!  :smiley-roll:

Since this sketch is time sensitive, I cannot use DELAY()
For waiting 300 ms and doing nothing else, delay() is just perfect.

It is true that delay() is not very useful where more than one thing needs to be done at the same time, but you seem to be very confused about what you are doing!  Mind you,

The code is quite a lot and do bunch of other things not affected by this function
Snippets do nothing to bolster your credibility.  :smiley-eek:

Fabius_rex

#9
Sep 15, 2018, 05:16 pm Last Edit: Sep 15, 2018, 05:22 pm by Fabius_rex
I find the conjunction of "digitalWriteFast" and a pushbutton to be an oxymoron!  :smiley-roll:
For waiting 300 ms and doing nothing else, delay() is just perfect.

It is true that delay() is not very useful where more than one thing needs to be done at the same time, but you seem to be very confused about what you are doing!  Mind you,
Snippets do nothing to bolster your credibility.  :smiley-eek:

The rig need to make polling of 4 different sensors, to determine the time another pin needs to go HIGH
That window varies from 800uS to 1500uS

Also is waiting for an ISR() function to trigger the pin going HIGH  for the specified time above, the LOW until the next ISR() call.

I already made test of how much time each block of code took

First the whole sketch took 3.6 ms for each loop. (From wich 1.6ms are from the LCD display code, and nothing can be done to speed it up)

With "digitalFast" I was able to reduce the 625uS it takes for the processor to read the state of a pin to 10uS (for each digital read)

Then with a similar library but for faster "analog read", I was hable to reduce another 450uS to 70uS (for the 4 analog input sensors in total, so 112us to 17uS reduction for each pin reading )

(With all that, I took a whole 1ms of time from the processor)

Also there is 300 ms as test, I need it to be even less, because the coil would not take 300ms to build up the magnetic field, It will be much less.

And since I already was using "digitalReadFast" and "digitalWriteFast" why should I put in a button detection "digitalRead" who takes 625uS to execute? where is the harm doing as I did?

If my last approach was right and did the job, why criticize the half part you don´t know about?

Thanks again for all the effort guys


xl97

"why criticize the half part you don´t know about?"

BECAUSE YOU DIDNT SHARE IT!..   How is anyone to know?  Standalone it didnt make sense... hence your feedback.

TomGeorge

Hi,
Can you post a timing diagram of your inputs and output requirements, so that we can understand the scope of your project?

You have two buttons for safety, but why the precise timing, just to get a coil into saturation and then release its energy?
What is the energy released being used for?

Thanks... Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Slumpert

"First the whole sketch took 3.6 ms for each loop. (From wich 1.6ms are from the LCD display code, and nothing can be done to speed it up)"

Pretty silly to update the LCD each loop............

Fabius_rex

"First the whole sketch took 3.6 ms for each loop. (From wich 1.6ms are from the LCD display code, and nothing can be done to speed it up)"

Pretty silly to update the LCD each loop............
LCD is updated each 500ms... But each time it runs, takes 1.6ms

Fabius_rex

#14
Sep 15, 2018, 06:59 pm Last Edit: Sep 15, 2018, 07:02 pm by Fabius_rex
"why criticize the half part you don´t know about?"

BECAUSE YOU DIDNT SHARE IT!..   How is anyone to know?  Standalone it didnt make sense... hence your feedback.
I told that I cannot use the delay() function (for any particular reason, but I already stated that "delay" cannot be a solution)

Then, it´s say "for 300ms use delay()...

Also I came up with the code seeing the state change detection as a user told me, then I runned trial and error and came up with the rest and posted it. Instead of telling if was good or wrong, it is questioned why for a 300 ms (wich is not, is less and i didn´t told, my mistake) I didn´t use delay. When in the first time I told that for time sentive reasons (in the rest of the code) I cannot...

I know most of the time people does not give the full information (and that information help the comunity to give better answers and don´t waste anybody´s time). But i think I gave all the info needed, the full sketch has around 330 lines dispositioned in 4 tabs, would you like to take a look in all that just to know how a button that does a SINGLE thing, and also need to take the less amount of time, behave? Ok..

Go Up