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);
}
}
}
// 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);
}
}
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
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
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.
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
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?
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..
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.
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