running second loop on button press

Good evening all,

I've been racking my brain for weeks and thought it was about time that i gave in and asked for help and advice.

im trying to make an led light bar (like the rac/aa/greenflag/ect), ive managed to source and alter some code to flash the leds in the order and pattern that i want and ive also managed to follow a tutorial to get a momentary switch on a pull up setup to turn a led on and off with each press, but i am struggling to combine the two. so that on one press the system runs the routing until the same button is pressed again to stop

const int buttonPin = 12; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
{
DDRD = B11111111; // set PORTD (digital 7~0)
// to output
}

byte a = B11111111;
byte b = B00000001;
byte c = B10000000;
byte e = B10101010;

void krider()
{
for (int k=0; k<2; k++)
{
for (int z=0; z<8; z++)
{
PORTD = b << z;
delay(100);
}

for (int z=0; z<8; z++)
{
PORTD = c >> z;
delay(100);
}
}
}

void onOff()
{
for (int k=0; k<10; k++)
{
PORTD = a;
delay(100);
PORTD = 0;
delay(100);
}
}

void invBlink()
{
for (int z=0; z<20; z++)
{
PORTD = e;
delay(100);
PORTD = ~e;
delay(100);
}
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 2 == 0) {
digitalWrite(run , loop2);
} else {
digitalWrite(run , loop);
}
}

void loop2()
{
invBlink();
delay(50);
krider();
delay(100);
onOff();
}

i also have to mention Tronixstuff.com as without his website i would have next to no code.
(hope the mention is allowed)

It's no different than the 1 LED.

All you're doing is where you had

digitalWrite(ledPIN, LOW);

you throw in all of the actions you want done within curly brackets {}

Please show code in code tags </>, not in quote tags.

Please specify what should happen in the combined sketch. The two sketches do different things, independent from each other, and I guess that you don't want to simply run both at the same time.

Of course you can combine the sketches, when you rename loop() into loop1(), and add

void loop() {
 loop1();
 loop2();
}

but you'll see that this is not what you want.

To run loop2() while the button is pressed, you can modify

  if (buttonPushCounter % 2 == 0) {
    digitalWrite(run , loop2);
  } else {
    digitalWrite(run , loop);
  }

into

  if (buttonPushCounter % 2 == 0) {
    loop2();
  } else {
    //digitalWrite(run , loop);
  }

DrDiettrich:
Please show code in code tags </>, not in quote tags.

Please specify what should happen in the combined sketch. The two sketches do different things, independent from each other, and I guess that you don't want to simply run both at the same time.

Of course you can combine the sketches, when you rename loop() into loop1(), and add

void loop() {

loop1();
loop2();
}



but you'll see that this is not what you want.

To run loop2() while the button is pressed, you can modify


if (buttonPushCounter % 2 == 0) {
    digitalWrite(run , loop2);
  } else {
    digitalWrite(run , loop);
  }



into


if (buttonPushCounter % 2 == 0) {
    loop2();
  } else {
    //digitalWrite(run , loop);
  }

Thank you for your responses. For some reason the software won't accept the
[/code]
} else {
dititalWrite(run , loop);
[/code]

The error I am getting is "run" was not declared in scope.

Many thanks
Patrick

You missed my comment mark // in front of this invalid statement.

Hi, Patrickc999

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.... :slight_smile:

DrDiettrich:
You missed my comment mark // in front of this invalid statement.

good evening, i have tried entering the code both with and without the comment mark //

with the comment mark the arduino just sits there and does nothing on the press of the button and without the coment mark i am getting the error :

exit status 1
'run' was not declared in this scope

not quite sure whats going on.

many thanks

patrick

Have you ever tried to understand the C language? As it looks right now, you sit and wait for somebody presenting the solution, which you can neither write yourself nor understand. That's not a good base for playing with microcontrollers :frowning:

Hi,
Can you tell us what you want

digitalWrite(run , loop2);

to do?

I'm sorry to say this, but if you have spent weeks trying to modify someone else's code, you could have spent that time using the MANY Arduino tutorials on the inet, including youtube to learn the basics of C++ programming.

Can you tell us your electronics, programming, arduino, hardware experience?

Is this a school/college/university project?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
Can you tell us what you want

digitalWrite(run , loop2);

to do?

I'm sorry to say this, but if you have spent weeks trying to modify someone else's code, you could have spent that time using the MANY Arduino tutorials on the inet, including youtube to learn the basics of C++ programming.

Can you tell us your electronics, programming, arduino, hardware experience?

Is this a school/college/university project?

Thanks.. Tom.. :slight_smile:

Good evening.

what im trying to do is set up a program for controlling an led light bar.
I have been trolling through the internet for the last 2 months trying to find tutorials which covered what i have been aiming for to no success (only finding snippets of what im after, and just working out and piecing together the bits i can, i no its not neat, pretty or ideal, which is why i left it so long before giving in and asking for help.

ive worked out how to make the routine run on the press of the button, now im trying to work out how to get it to stop.

thank you for your help

Patrick

First digitalWrite(run , loop2); is not what you want to do.
The operation digitalWrite() is an I/O operation that uses
the first value ( in your case run ) as a pin number ( which it is not )
and tries to use the value returned by loop as a leve ( you set it void
and it would fail anyway because it has no () )l.
The arduino expects to have two default functions one is init() and the
other is loop().
The other piece you have is loop2(). This is an arbitrary named
piece. To do it would require and explicit call ( in other words
loop2(); ).
You don't need any addition else statement to execute loop() as
it is like it was a do ...while ( true );
Use the Learning pulldown and the Reference to see what this all
means.
Also, When looking for the press of a switch, it is also often
useful to look for the release of the switch at some point.
Where you look for the release depends on how you want your
code to flow.
Two ways you might do this.
One is blocking with a while() {}; statement ( fill in the missing pieces )
or a non-blocking if() {} that sets a flag to latter watch for the release
with another if() {} statement.
I'm also kind of new to C but I found that going through the reference
section of the Learning pulldown was the way to go.
Look at each of the things there and how they are used. It will
make writing, modifying and understanding programs a lot easier.
Dwight