Go Down

Topic: Cancelling method in midair / jump to another method (Read 744 times) previous topic - next topic


May 04, 2013, 11:13 am Last Edit: May 04, 2013, 11:14 am by kampsu Reason: 1
Okay, i've been struggling with this one for a while now. What i try to do is this:  First all my 11 leds are out.  When i press button method 1 starts and lights start blinking in certain way.  When i press button again program moves to method2 and lights behave diffrently. Again if i press button once more the program jumps to method 3. After that it moves back to method1.

I tried multiple ways but every time i encounter same problem - program only accept my button == HIGH state when method ends. So only way my program to work is by holding the button until method ends. Then my "state" variable changes to 1 and that triggers method 2. Obiviously that cannot be the case if i'm making some christmas lights beacause the light circle would take way too long time. If you could come up idea that i can press button any time i want and it would jump method to method it would be great!

My code is this

void setup()
// all output pins are here and button that is input pin

void loop()
// i tried to make all my code here so that methods only affect light behavior

void method1()
// behavior 1

void method2()
// behavior 2

void method3()
behavior 3


May 04, 2013, 11:30 am Last Edit: May 04, 2013, 02:28 pm by Erdin Reason: 1
The way the leds blink should not be in code but the sequence should be in a table.
A table with the leds and delays.

Start with this, http://arduino.cc/en/Tutorial/BlinkWithoutDelay

There is a simular example that is exactly what you need, but I can't find it right now.....


Post your actual code. You haven't said anything about your code structure, so how do you expect us to know how to fix it? Do each of your methods run in a loop, are there lots of delays in them, how quickly do you need to return....We can't help if we are guessing the answers to these questions


program only accept my button == HIGH state when method ends.

So, don't wait for the method to end.
Have a look at the blink without delay example in the IDE, without delay.

And please use code tags when posting code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


May 04, 2013, 04:23 pm Last Edit: May 04, 2013, 10:15 pm by TanHadron Reason: 1
Another possibility would be to use an interrupt on your button input pin.  Something like this:

Code: [Select]

volatile int ButtonPushedFlag = 0;

void InterruptHandler()
  ButtonPushedFlag = 1;

void setup()
  pinMode(2, INPUT);  // Interrupt 0 - pin
  attachInterrupt(0, InterruptHandler, RISING);

void loop()
  static int State = 0;  // All lights out

  if (ButtonPushedFlag != 0)
    ButtonPushedFlag = 0;
    if (State > 3)
      State = 1;

  if (State == 1)
  else if (State == 2)
  else if State == 3)

It would finish the current method before jumping to the next method.  It would only detect one button push until the current method finished.  But you wouldn't have to hold down the button.

Go Up