Pages: [1]   Go Down
Author Topic: Cancelling method in midair / jump to another method  (Read 303 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
}



« Last Edit: May 04, 2013, 04:14:43 am by kampsu » Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.....
« Last Edit: May 04, 2013, 07:28:28 am by Erdin » Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"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.

Offline Offline
God Member
*****
Karma: 25
Posts: 529
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another possibility would be to use an interrupt on your button input pin.  Something like this:

Code:
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;
    ++State;
    if (State > 3)
      State = 1;
  }

  if (State == 1)
    Method1();
  else if (State == 2)
    Method2();
  else if State == 3)
    Method3();
}

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.
« Last Edit: May 04, 2013, 03:15:10 pm by TanHadron » Logged

Pages: [1]   Go Up
Jump to: