how to stop a for loop when loop trigger button is held in

Hi all.

done some coding today and have come to a wall!!
my code is working fine, but when it completes its loop and the button that has started the loop is still pressed in i want it to stop until the button has been let go. at the moment when the loop is triggered by the button the loop works correctly but then once it ends and the button is still pushed it obviously keeps going around in the loop as to be expected,

ive tried finding ways around it but this is as far as my know how goes, and dont really know where to go now.

any pointers would be great, prob something simple to most of you but im not really a software guy.

steve

RM_DELAY.ino (1.06 KB)

Look at the state change detection example. It shows how to trigger an action when a switch BECOMES pressed. As long as the switch stays pressed, the action won't trigger again, because the state doesn't change to pressed again, until it changed to not pressed first.

AWESOME THANKS!!!

i will search for state change.

cheers

ok so i have looked at the example and admittidly not fully understood it.
i have copied and pasted and modified the code as i thought best but it is still not ignoring my high input once the loops finished.

also when powering up the attiny85 it takes a button push, and a long wait (assuming its running one of the delays first) before the led turns on, where as my original code as soon as i powered it up it would spring into life as soon as power was applied.

i have no idea where i am going wrong.

steve

modded code below

RM_DELAY_test.ino (1.84 KB)

You really should have a look at the Tools menu, especially the Auto Format item.

Judicuous use of white space (one blank line before each function) would be better.

The type of each function should start in column 1.

   unsigned long   tmDelay2 = map(valPot2, 0L, 1023L, 5000L, 30000L);

{  
  
       buttonState = digitalRead(pinRM);

What is that { for?

      buttonPushCounter++;

The example counter presses. There is no need for you to.

How IS the switch wired? Why are you not using the internal pullup resistor which makes for simpler wiring?

Serial prints are conspicuously missing from your code. I'd suggest you remedy that.

hi paul.

thanks for your input. as i said im getting into this and im not an avid coder, hence why there are probably lots of issues that to me mean nothing at the moment, but this is why im asking for a bit of guidance to make me aware of these issues. as for what is this below, i dont know! as i mentioned i copied and pasted what i thought i would need based on my limited knowledge on this subject.

What is that { for?

      buttonPushCounter++;

The example counter presses. There is no need for you to.

i will remove the above and go through it again, but my dyslexia doesn't help me understand this stuff.

the switch is actualy a 5v input from a seperate device, and i am using a pull down resistor going to -.

adding serial prints are things that i am not using in this project to i have no use for them (i figured the code would be simple enough to not need to debug it).

steve

adding serial prints are things that i am not using in this project to i have no use for them

So, your preferred method of debugging is guessing. I HATE that method, and won't use it unless there is absolutely no other way.

In your case, there IS another way.

i am currently not that familiar with using them, nor no how to use them. i do however no what they are used for.

i have come here asking for some guidance and someone to help me through it, not a lot of criticism as to how and why im doing things, i pick up an Arduino maybe once a year when a project calls for one. which is exactly why i need help with it, and exactly why im asking for help to get this quick fix, as im sure someone of you knowledge it would be,

If you add a statement

   Serial.println("The pin changed state");

inside the body of

   if (buttonState != lastButtonState) {      // if the state has changed, increment the counter

that statement should be printed every time the switch changes state, and only when the switch changes state.

If that happens, you've learned something (the switch is wired correctly). If it doesn't, you've learned something else (there's a hardware problem).

If you keep adding Serial.print() statements ("The switch became pressed", "The switch became released", etc.), you'll quickly (less than 10 minutes) narrow the problem down to less than 10 lines of code, or you'll discover that you have a hardware problem that no amount of code will solve.

Let us know when you have narrowed down the problem.

i have narrowed down the problem, the problem being lack of knowledge :D

its not a hardware issue at all as my other code works fine apart from it doesn't ignore mt constant button push. and the attiny85 is in a zif socket and i have to keep removing it to load code to it, so isnt that easy, i will see if i have a nano around i can add the serial commands too but for this to work in my eyes i need a better understanding of how state change works.

i need a better understanding of how state change works.

If the switch is pressed this time around, and was releasedlast time, a (state) change occurred. If the switch is pressed this time around, and was pressed last time, too, no (state change occurred. If the switch is released this time around, and was released last time, too, no (state) change occurred. If the switch is released this time around, as was pressed last time, a (state) change occurred.