Offline
Newbie
Karma: 0
Posts: 10
|
 |
« on: November 08, 2012, 03:20:24 pm » |
Hello, thanks for taking the time to even read my post with the expectation of offering help!
I am trying to simply light 10 LEDs in sequence from low to high, DONE I am trying to simply light 10 LEDs in sequence from high to low, DONE I am trying to change the timing/delay of each afore mentioned events, DONE I am trying to make any part of this stop after n times (n is the number of times I will choose) FAIL -also, how do I get more than one LED to light simultaneously?
So, I got the sequencing but cannot get a DO, WHILE, DO WHILE, or FOR to stop the looping, EVER.
I declared an integer [loops] and tried to increment/decrement using loops++, loops--, ++loops, --loops, loops +1, loops - 1, loops = loops -1, loops = loops +1, loops != 1, loops !0 (I read that when the WHILE becomes FALSE is when the event is recognized)
here is the last attempt with some of the bad code commented out
//int thisPin = 0; int timer = 100; // The higher the number, the slower the timing. int pinCount = 10; // using 10 LEDs // int loops = 3; //FAIL // int thisPin; void setup() { // use a for loop to initialize each pin as an output: for (int thisPin = 1; thisPin < pinCount; thisPin++) { pinMode(thisPin, OUTPUT); } }
void loop() { int loops = 3; //FAIL while ( loops != 1 ){ //fAIL // loops - 1; for (int thisPin = 1; thisPin < pinCount; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #1 on: November 08, 2012, 03:35:23 pm » |
Please edit your post, select the code, and put it between [code] ... [/code] tags.
You can do that by hitting the # button above the posting area.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #2 on: November 08, 2012, 03:37:23 pm » |
This will certainly fail: int loops = 3; //FAIL while ( loops != 1 ){ //fAIL
What about: for (int loops = 0; loops < 3; loops++) { // do something }
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 87
Posts: 9371
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #3 on: November 08, 2012, 03:40:55 pm » |
use CTRL-T in the IDE to auto layout the code void loop() { int loops = 3; while ( loops != 1 ) { loops = loops - 1; for (int thisPin = 1; thisPin < pinCount; thisPin++) { digitalWrite(thisPin, HIGH); delay(timer); digitalWrite(thisPin, LOW); delay(timer); } } while(1); // block forever }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #4 on: November 08, 2012, 03:52:15 pm » |
Nick Gammon Global Moderator
This will certainly fail:
Code:
int loops = 3; //FAIL while ( loops != 1 ){ //fAIL Thanks for pointing that out, but why will it certainly fail?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #5 on: November 08, 2012, 03:53:24 pm » |
robtillaart Netherlands,
thank you! i will compare what you did to what i have tried.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #6 on: November 08, 2012, 04:04:25 pm » |
robtillaart Netherlands
I tried what you did and that does not work for me either. your code runs the method 1 time then stops, and I don't understand what you are trying to do with what you posted.
SO, I still do not have a way to stop the sequence after n times, any help????
Thanks all
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 37
Posts: 1828
|
 |
« Reply #7 on: November 08, 2012, 04:22:54 pm » |
I tried what you did and that does not work for me either. your code runs the method 1 time then stops, and I don't understand what you are trying to do with what you posted.
SO, I still do not have a way to stop the sequence after n times, any help????
Change the loops #.
|
|
|
|
« Last Edit: November 08, 2012, 04:25:32 pm by Arrch »
|
Logged
|
|
|
|
|
Boston
Offline
God Member
Karma: 3
Posts: 520
Arduino rocks
|
 |
« Reply #8 on: November 08, 2012, 04:23:20 pm » |
See if this is easier to understand. Code is not tested. int count = 0; void setup(void) { }
void loop(void) { count++; if (count <=8) // change 8 to number you want { ledSequence(); } }
void ledSequence() { //do stuff here }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #9 on: November 08, 2012, 04:25:01 pm » |
Arrch California
i tried to change the integer and that did not work either...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #10 on: November 08, 2012, 04:25:31 pm » |
Try this: void loop() { static int loops = 3; //FAIL - stops after the 3rd time loops = loops - 1; if ( loops){ //fAIL for (int thisPin = 1; thisPin < pinCount; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #11 on: November 08, 2012, 04:36:50 pm » |
Pauly, Thanks but no go! That did not work either. How something so simple be so difficult??? Still n o solution... int count = 0; int timer = 100; void setup(void) { // use a for loop to initialize each pin as an output: for (int thisPin = 1; thisPin < 10; thisPin++) { pinMode(thisPin, OUTPUT); } }
void loop(void) { count++; if (count <=8) // change 8 to number you want { //do stuff here for (int thisPin = 10; thisPin >= 1; thisPin--) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #12 on: November 08, 2012, 04:39:14 pm » |
Moving one line: void loop() { static int loops = 3; //FAIL - stops after the 3rd time if ( loops){ //fAIL loops = loops - 1; for (int thisPin = 1; thisPin < pinCount; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #13 on: November 08, 2012, 04:40:57 pm » |
void loop(void) { count++; if (count <=8) // change 8 to number you want { What happens here is that as count gets incremented passing 8, the "if" section will not be executed. However, count will roll-over and the "if" section gets executed. Instead, if you put the "count++" inside the "if", you will be fine.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #14 on: November 08, 2012, 04:48:48 pm » |
dHenry, thanks!!! I tweaked what you posted and got this which works... Thanks all! int timer = 100; // The higher the number, the slower the timing. int pinCount = 10; void setup() { // use a for loop to initialize each pin as an output: for (int thisPin = 1; thisPin < 10; thisPin++) { pinMode(thisPin, OUTPUT); } }
void loop() { static int loops = 6; //FAIL - stops after the 3rd time if (loops > 0){ //fAIL loops = loops - 1; for (int thisPin = 1; thisPin < pinCount; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } } }
|
|
|
|
« Last Edit: November 08, 2012, 04:54:17 pm by yost87 »
|
Logged
|
|
|
|
|
|