Central Illinois
Offline
Newbie
Karma: 0
Posts: 9
trying to understand programming the arduino
|
 |
« on: November 06, 2012, 11:11:57 pm » |
I am somewhat new at programming currently...I have done some in the past, but it has been a while. I'm sure this is an easy fix, but I'm not sure exactly how to write it. Basically, I am going to have 3 switches. When I push switch "A" I want one set of code to execute; if I push switch "B" another set of code will execute; and if I were to push switch "C" a different set of code will execute. I believe I have this figured out for the most part. The part I am not sure about is how to make it so that if I had pushed switch "A" and that code has executed, now I want to push switch "C" and have that code execute. When I try this, it doesn't always work like I want it to and I think it has something to do with the code. I have the code listed below and any help would be appreciated. On a side note, to stop the code I have been planning on pressing the corresponding switch a 2nd time; it would be nice to have maybe a 4th switch I could push to end the code, but then I run into the problem of the controller reading the input from that switch while other code is being executed. I may just end up cutting the power to stop everything. I am open to ideas and suggestions. /* * Working on my directional arrows...draft */
int switchLPin = 2; // switch for Left connected to pin 2 int switchCPin = 3; // switch for Center connected to pin 3 int switchRPin = 4; // switch for Right connected to pin 4 int led1Pin = 12; int led2Pin = 11; int led3Pin = 10; int led4Pin = 9; int led5Pin = 8; int led6Pin = 7;
int valL; // variable for reading the pin status int valL2; // variable for reading the delayed status int buttonLState; // variable to hold the button state
int valC; // variable for reading the center pin status int valC2; // variable for reading the center delayed status int buttonCState; // variable to hold the button state
int valR; // variable for reading the right pin status int valR2; // variable for reading the right delayed status int buttonRState; // variable to hold the button state
int lightMode = 0; // What mode is the light in?
void setup() { pinMode(switchLPin, INPUT); // Set the left switch pin as input pinMode(switchCPin, INPUT); // Set the center switch pin as input pinMode(switchRPin, INPUT); // Set the right switch pin as input
pinMode(led1Pin, OUTPUT); pinMode(led2Pin, OUTPUT); pinMode(led3Pin, OUTPUT); pinMode(led4Pin, OUTPUT); pinMode(led5Pin, OUTPUT); pinMode(led6Pin, OUTPUT); Serial.begin(9600); // Set up serial communication at 9600bps buttonLState = digitalRead(switchLPin); // read the initial state Left switch buttonCState = digitalRead(switchCPin); // read the initial state Central switch buttonRState = digitalRead(switchRPin); // read the initial state Right switch }
//void loop(){ // valL = digitalRead(switchLPin); // read input value and store it in val // delay(10); // 10 milliseconds is a good amount of time // valL2 = digitalRead(switchLPin); // read the input again to check for bounces // if (valL == valL2) { // make sure we got 2 consistant readings! // if (valL != buttonLState) { // the button state has changed! // if (valL == LOW) { // check if the button is pressed // if (lightMode == 0) { // if its off // lightMode = 1; // turn lights on! // } else { // if (lightMode == 1) { // if its all-on // lightMode = 2; // make it blink! // } else { // if (lightMode == 2) { // if its blinking // lightMode = 3; // make it wave! // } else { // if (lightMode == 3) { // if its waving, // lightMode = 0; // turn light off! // } // } // } // } // } // } // buttonLState = valL; // save the new state in our variable // } void loop()
// turn lights on { valL = digitalRead(switchLPin); // read input value and store it in val delay(10); // 10 milliseconds is a good amount of time valL2 = digitalRead(switchLPin); // read the input again to check for bounces if (valL == valL2) { // make sure we got 2 consistant readings! if (valL != buttonLState) { // the button state has changed! if (valL == LOW) { // check if the button is pressed if (lightMode == 0) { // if its off lightMode = 1; // turn lights on! } else { lightMode = 0; // turn lights off } } } buttonLState = valL; // save the new state in our variable }
// blink lights { valC = digitalRead(switchCPin); // read input value and store it in val delay(10); // 10 milliseconds is a good amount of time valC2 = digitalRead(switchCPin); // read the input again to check for bounces if (valC == valC2) { // make sure we got 2 consistant readings! if (valC != buttonCState) { // the button state has changed! if (valC == LOW) { // check if the button is pressed if (lightMode == 0) { // if its off lightMode = 2; // blink lights } else { lightMode = 0; // turn lights off } } } buttonCState = valC; // save the new state in our variable } } // wave lights { valR = digitalRead(switchRPin); // read input value and store it in val delay(20); // 10 milliseconds is a good amount of time valR2 = digitalRead(switchRPin); // read the input again to check for bounces if (valR == valR2) { // make sure we got 2 consistant readings! if (valR != buttonRState) { // the button state has changed! if (valR == LOW) { // check if the button is pressed if (lightMode == 0) { // if its off lightMode = 3; // wave lights } else { lightMode = 0; // turn lights off } } } buttonRState = valR; // save the new state in our variable } } // Now do whatever the lightMode indicates if (lightMode == 0) { // all-off digitalWrite(led1Pin, LOW); digitalWrite(led2Pin, LOW); digitalWrite(led3Pin, LOW); digitalWrite(led4Pin, LOW); digitalWrite(led5Pin, LOW); digitalWrite(led6Pin, LOW); }
if (lightMode == 1) //arrow to the left { digitalWrite(7, HIGH); delay(300); digitalWrite(8, HIGH); delay(300); digitalWrite(9, HIGH); delay(300); digitalWrite(10, HIGH); delay(300); digitalWrite(11, HIGH); delay(300); digitalWrite(12, HIGH); delay(750); digitalWrite(7, LOW); digitalWrite(8, LOW); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); delay(1000); }
if (lightMode == 2) //arrow to the outward { digitalWrite(10, HIGH); digitalWrite(9, HIGH); delay(300); digitalWrite(11, HIGH); digitalWrite(8, HIGH); delay(300); digitalWrite(12, HIGH); digitalWrite(7, HIGH); delay(750); digitalWrite(7, LOW); digitalWrite(8, LOW); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); delay(1000); } if (lightMode == 3) //arrow to the right { digitalWrite(12, HIGH); delay(300); digitalWrite(11, HIGH); delay(300); digitalWrite(10, HIGH); delay(300); digitalWrite(9, HIGH); delay(300); digitalWrite(8, HIGH); delay(300); digitalWrite(7, HIGH); delay(750); digitalWrite(7, LOW); digitalWrite(8, LOW); digitalWrite(9, LOW); digitalWrite(10, LOW); digitalWrite(11, LOW); digitalWrite(12, LOW); delay(1000); } }
|
|
|
|
|
Logged
|
Sincere thanks for all help given.
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35590
Seattle, WA USA
|
 |
« Reply #1 on: November 06, 2012, 11:36:47 pm » |
You need to get rid of all those delay()s. They are why your switches are not responsive. The blink without delay example is the place to start.
|
|
|
|
|
Logged
|
|
|
|
|
Central Illinois
Offline
Newbie
Karma: 0
Posts: 9
trying to understand programming the arduino
|
 |
« Reply #2 on: November 06, 2012, 11:39:39 pm » |
Okay, so if I were to change the code utilizing the "blink without delay" code as an example, will that make this work as I desire it to?
|
|
|
|
|
Logged
|
Sincere thanks for all help given.
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35590
Seattle, WA USA
|
 |
« Reply #3 on: November 06, 2012, 11:44:24 pm » |
Okay, so if I were to change the code utilizing the "blink without delay" code as an example, will that make this work as I desire it to? That's a mighty big if. If you change the code correctly, yes. If not, then no.
|
|
|
|
|
Logged
|
|
|
|
|
Central Illinois
Offline
Newbie
Karma: 0
Posts: 9
trying to understand programming the arduino
|
 |
« Reply #4 on: November 06, 2012, 11:58:41 pm » |
Well, I looked at the "blink without delay" example and I'm not sure how I would incorporate it in this code. I am wanting led1 to come on, then 1/4 second later led2 to come on, 1/4 second later led3 to come on, etc. Then once all 6 leds are on, for them to all go off for about 1 second, then repeat. If I was just blinking leds, I think I could do that, but making them happen in sequence like this each time...I'm not sure. Can someone point me in the right direction???
|
|
|
|
|
Logged
|
Sincere thanks for all help given.
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #5 on: November 07, 2012, 12:57:48 am » |
Well, I looked at the "blink without delay" example and I'm not sure how I would incorporate it in this code. I am wanting led1 to come on, then 1/4 second later led2 to come on, 1/4 second later led3 to come on, etc. Then once all 6 leds are on, for them to all go off for about 1 second, then repeat. If I was just blinking leds, I think I could do that, but making them happen in sequence like this each time...I'm not sure. Can someone point me in the right direction???
It's going to take more than 15 minutes of "looking at" the example. Play with it. Change things. When you fully understand how it works, you should be able to draw out the logic on what you want to do, and adapt the concepts to fit said logic.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35590
Seattle, WA USA
|
 |
« Reply #6 on: November 07, 2012, 05:52:14 am » |
Well, I looked at the "blink without delay" example and I'm not sure how I would incorporate it in this code. Time to "play Arduino", then. Pretend you are an Arduino. Your job is to turn some LEDs on and off, and regular, but different, intervals. You have a watch with no controls - all you can do is note when things happen. You have a pad of paper on which you can scribble notes (like the last time you turned a pin on or off). For one pin, it's pretty simple. Note when you changed the state of a pin. Periodically, see if it is time to change the state again. If so, make the appropriate change, and record when you made the change. For two pins, with different intervals, the process is really not more complicated. You just have twice as much work to do. You can see then that 3 pins or 10 pins or 100 pins doesn't make the process more complicated. You need to determine if it is time, for any given pin, to change its state, and, if it is, change the state and record when the change occurred. Your requirement is complicated by the fact that determining whether to diddle a pin, or not, is not just a matter of time. It is also a matter of whether it's this pin's turn to be diddled. But, it isn't that much more complicated.
|
|
|
|
|
Logged
|
|
|
|
|
Central Illinois
Offline
Newbie
Karma: 0
Posts: 9
trying to understand programming the arduino
|
 |
« Reply #7 on: November 13, 2012, 05:14:14 pm » |
Alright, I had some time to play with the code today and this is what I came up with to have the LEDs light at specified times without using all of the delays. Here is that code, but I have a problem. // test code utilizing blink without delay. // written 11/13/12.
const int ledAPin = 7; // the number of the LED pin const int ledBPin = 8; // the number of the 2nd LED pin const int ledCPin = 9; // the number of the 3rd LED pin const int ledDPin = 10; // the number of the 4th LED pin const int ledEPin = 11; // the number of the 5th LED pin const int ledFPin = 12; // the number of the 6th LED pin
// Variables will change:
long previousMillis = 0; // will store last time LED was updated
long ledAOn = 2000; // total time ledA is on long ledBOn = 300; // the time ledB will come on long ledBOff = 1700; // the time ledB goes off long ledCOn = 600; long ledCOff = 1400; long ledDOn = 900; long ledDOff = 1100; long ledEOn = 1200; long ledEOff = 800; long ledFOn = 1500; long ledFOff = 500; long totalTime = 2500; // the total time for the sequence
void setup() { // set the digital pin as output: pinMode(ledAPin, OUTPUT); pinMode(ledBPin, OUTPUT); pinMode(ledCPin, OUTPUT); pinMode(ledDPin, OUTPUT); pinMode(ledEPin, OUTPUT); pinMode(ledFPin, OUTPUT); }
void loop() {
unsigned long currentMillis = millis(); // if(currentMillis - previousMillis > interval) { // // save the last time you blinked the LED // previousMillis = currentMillis; // // // if the LED is off turn it on and vice-versa: // if (ledState == LOW) // ledState = HIGH; // else // ledState = LOW; // // // set the LED with the ledState of the variable: // digitalWrite(ledPin, ledState);
if(currentMillis - previousMillis > totalTime) { previousMillis = currentMillis; }
if(currentMillis - previousMillis > ledFOn){ digitalWrite(ledFPin, HIGH); }
if(currentMillis - previousMillis > ledEOn){ digitalWrite(ledEPin, HIGH); }
if(currentMillis - previousMillis > ledDOn){ digitalWrite(ledDPin, HIGH); }
if(currentMillis - previousMillis > ledCOn){ digitalWrite(ledCPin, HIGH); } if(currentMillis - previousMillis > ledBOn){ digitalWrite(ledBPin, HIGH); }
if(currentMillis - previousMillis < ledAOn){ digitalWrite (ledAPin, HIGH); } if(currentMillis - previousMillis > ledAOn){ digitalWrite (ledBPin, LOW); digitalWrite (ledAPin, LOW); digitalWrite(ledCPin, LOW); digitalWrite(ledDPin, LOW); digitalWrite(ledEPin, LOW); digitalWrite(ledFPin, LOW); } if(currentMillis - previousMillis > totalTime) { previousMillis = currentMillis; } }
The LEDs light just as I want them, but only the 1st LED turns off like it is supposed to, the others just dim, until right before the loop repeats. I'm not sure why the LEDs don't turn off like I was hoping they would. Any suggestions???
|
|
|
|
|
Logged
|
Sincere thanks for all help given.
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: November 13, 2012, 05:20:20 pm » |
the others just dim This usually indicates that you're turning them on and off, very rapidly. Can you see why that might be?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Central Illinois
Offline
Newbie
Karma: 0
Posts: 9
trying to understand programming the arduino
|
 |
« Reply #9 on: November 13, 2012, 05:35:13 pm » |
I'm not sure it is the case of them being turned on/off quickly when they turn on like they are supposed to, they just don't turn off correctly. I am wondering if it is because I try to turn them all off at once...but I'm thinking that shouldn't matter.
Can you see the problem in my code??
|
|
|
|
|
Logged
|
Sincere thanks for all help given.
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #10 on: November 13, 2012, 05:46:54 pm » |
I'm not sure it is the case of them being turned on/off quickly when they turn on like they are supposed to It is, I can see it in the logic. if(currentMillis - previousMillis > ledFOn){ digitalWrite(ledFPin, HIGH); } ... if(currentMillis - previousMillis > ledAOn){ digitalWrite (ledBPin, LOW); digitalWrite (ledAPin, LOW); digitalWrite(ledCPin, LOW); digitalWrite(ledDPin, LOW); digitalWrite(ledEPin, LOW); digitalWrite(ledFPin, LOW); }
If these two if statements fire in the same loop iteration, there will be rapid on/offs. Plug in the value of 2.3 seconds (for current minus previous) and tell me if they both will fire.
|
|
|
|
|
Logged
|
|
|
|
|
Poole, Dorset, UK
Offline
God Member
Karma: 8
Posts: 673
|
 |
« Reply #11 on: November 13, 2012, 05:56:55 pm » |
Ok you did the blink without delay thing and its done a lot of good now you need to look at using an FSM (Finite State Machine) you'll find more in the playground buy here some pseudo code as a pointer. int state =0;
void buttonACode(){ // that ever you want for button a but don't use delays } void buttonBCode(){ // that ever you want for button b but don't use delays } void buttonCCode(){ // that ever you want for button c but don't use delays } in loop() // Pseudo code warning. This is not c/c++ if (buttonA is pressed) state =1; if (buttonB is pressed) state =2; if (buttonC is pressed) state =3; switch (state){ case 0: do nothing, maybe turn off all LEDs break; case 1: break; case 0: buttonACode(); break; case 2: buttonBCode(); break; case 3: buttonCCode(); break; }
Good luck Mark
|
|
|
|
|
Logged
|
|
|
|
|
Central Illinois
Offline
Newbie
Karma: 0
Posts: 9
trying to understand programming the arduino
|
 |
« Reply #12 on: November 13, 2012, 07:44:38 pm » |
I'm not sure it is the case of them being turned on/off quickly when they turn on like they are supposed to It is, I can see it in the logic. if(currentMillis - previousMillis > ledFOn){ digitalWrite(ledFPin, HIGH); } ... if(currentMillis - previousMillis > ledAOn){ digitalWrite (ledBPin, LOW); digitalWrite (ledAPin, LOW); digitalWrite(ledCPin, LOW); digitalWrite(ledDPin, LOW); digitalWrite(ledEPin, LOW); digitalWrite(ledFPin, LOW); }
If these two if statements fire in the same loop iteration, there will be rapid on/offs. Plug in the value of 2.3 seconds (for current minus previous) and tell me if they both will fire. Woo Hoo!!! I got it to work the way I want it to. If someone could check the code to see if you see any issues? I will then be working on the button code probably tomorrow. Here is the code that works: // test code utilizing blink without delay. // written 11/13/12.
const int ledAPin = 7; // the number of the LED pin const int ledBPin = 8; // the number of the 2nd LED pin const int ledCPin = 9; // the number of the 3rd LED pin const int ledDPin = 10; // the number of the 4th LED pin const int ledEPin = 11; // the number of the 5th LED pin const int ledFPin = 12; // the number of the 6th LED pin
// Variables will change:
long previousMillis = 0; // will store last time LED was updated
long ledAOn = 2500; // total time ledA is on long ledBOn = 300; // the time ledB will come on long ledBOff = 1700; // the time ledB goes off long ledCOn = 600; long ledCOff = 1400; long ledDOn = 900; long ledDOff = 1100; long ledEOn = 1200; long ledEOff = 800; long ledFOn = 1500; long ledFOff = 500; long totalTime = 3000; // the total time for the sequence
void setup() { // set the digital pin as output: pinMode(ledAPin, OUTPUT); pinMode(ledBPin, OUTPUT); pinMode(ledCPin, OUTPUT); pinMode(ledDPin, OUTPUT); pinMode(ledEPin, OUTPUT); pinMode(ledFPin, OUTPUT); }
void loop() {
unsigned long currentMillis = millis(); // if(currentMillis - previousMillis > interval) { // // save the last time you blinked the LED // previousMillis = currentMillis; // // // if the LED is off turn it on and vice-versa: // if (ledState == LOW) // ledState = HIGH; // else // ledState = LOW; // // // set the LED with the ledState of the variable: // digitalWrite(ledPin, ledState);
if(currentMillis - previousMillis > totalTime) { previousMillis = currentMillis; }
if(currentMillis - previousMillis > ledFOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledFPin, HIGH); }
if(currentMillis - previousMillis > ledEOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledEPin, HIGH); }
if(currentMillis - previousMillis > ledDOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledDPin, HIGH); }
if(currentMillis - previousMillis > ledCOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledCPin, HIGH); } if(currentMillis - previousMillis > ledBOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledBPin, HIGH); }
if(currentMillis - previousMillis < ledAOn){ digitalWrite (ledAPin, HIGH); } if(currentMillis - previousMillis > ledAOn){ digitalWrite (ledBPin, LOW); digitalWrite (ledAPin, LOW); digitalWrite(ledCPin, LOW); digitalWrite(ledDPin, LOW); digitalWrite(ledEPin, LOW); digitalWrite(ledFPin, LOW); } if(currentMillis - previousMillis > totalTime) { previousMillis = currentMillis; } }
Awesome....thanks again.
|
|
|
|
|
Logged
|
Sincere thanks for all help given.
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1888
|
 |
« Reply #13 on: November 13, 2012, 08:42:37 pm » |
I'm not sure it is the case of them being turned on/off quickly when they turn on like they are supposed to It is, I can see it in the logic. if(currentMillis - previousMillis > ledFOn){ digitalWrite(ledFPin, HIGH); } ... if(currentMillis - previousMillis > ledAOn){ digitalWrite (ledBPin, LOW); digitalWrite (ledAPin, LOW); digitalWrite(ledCPin, LOW); digitalWrite(ledDPin, LOW); digitalWrite(ledEPin, LOW); digitalWrite(ledFPin, LOW); }
If these two if statements fire in the same loop iteration, there will be rapid on/offs. Plug in the value of 2.3 seconds (for current minus previous) and tell me if they both will fire. Woo Hoo!!! I got it to work the way I want it to. If someone could check the code to see if you see any issues? I will then be working on the button code probably tomorrow. Here is the code that works: // test code utilizing blink without delay. // written 11/13/12.
const int ledAPin = 7; // the number of the LED pin const int ledBPin = 8; // the number of the 2nd LED pin const int ledCPin = 9; // the number of the 3rd LED pin const int ledDPin = 10; // the number of the 4th LED pin const int ledEPin = 11; // the number of the 5th LED pin const int ledFPin = 12; // the number of the 6th LED pin
// Variables will change:
long previousMillis = 0; // will store last time LED was updated
long ledAOn = 2500; // total time ledA is on long ledBOn = 300; // the time ledB will come on long ledBOff = 1700; // the time ledB goes off long ledCOn = 600; long ledCOff = 1400; long ledDOn = 900; long ledDOff = 1100; long ledEOn = 1200; long ledEOff = 800; long ledFOn = 1500; long ledFOff = 500; long totalTime = 3000; // the total time for the sequence
void setup() { // set the digital pin as output: pinMode(ledAPin, OUTPUT); pinMode(ledBPin, OUTPUT); pinMode(ledCPin, OUTPUT); pinMode(ledDPin, OUTPUT); pinMode(ledEPin, OUTPUT); pinMode(ledFPin, OUTPUT); }
void loop() {
unsigned long currentMillis = millis(); // if(currentMillis - previousMillis > interval) { // // save the last time you blinked the LED // previousMillis = currentMillis; // // // if the LED is off turn it on and vice-versa: // if (ledState == LOW) // ledState = HIGH; // else // ledState = LOW; // // // set the LED with the ledState of the variable: // digitalWrite(ledPin, ledState);
if(currentMillis - previousMillis > totalTime) { previousMillis = currentMillis; }
if(currentMillis - previousMillis > ledFOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledFPin, HIGH); }
if(currentMillis - previousMillis > ledEOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledEPin, HIGH); }
if(currentMillis - previousMillis > ledDOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledDPin, HIGH); }
if(currentMillis - previousMillis > ledCOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledCPin, HIGH); } if(currentMillis - previousMillis > ledBOn){ if(currentMillis - previousMillis < ledAOn) digitalWrite(ledBPin, HIGH); }
if(currentMillis - previousMillis < ledAOn){ digitalWrite (ledAPin, HIGH); } if(currentMillis - previousMillis > ledAOn){ digitalWrite (ledBPin, LOW); digitalWrite (ledAPin, LOW); digitalWrite(ledCPin, LOW); digitalWrite(ledDPin, LOW); digitalWrite(ledEPin, LOW); digitalWrite(ledFPin, LOW); } if(currentMillis - previousMillis > totalTime) { previousMillis = currentMillis; } }
Awesome....thanks again. A couple of things... Remove the commented out code, no need for it to waste space. Create a variable that stores the result of currentMillis - previousMillis, and use that in your comparisons; no need to do the arithmetic that many times. Use arrays for the pins and intervals; probably an array of a simple struct simple struct: struct ledData { int pin; unsigned long on; unsigned long off; }; I would also use a variable as a flag that gets set when you turn all the LEDs off, and cleared otherwise. Have a single if statement that checks the flag before even bothering to check if it's been long enough. Psuedocode: if the flag cleared get the time difference check each led to see if it should be on
if it's been long enough turn all leds off set the flag else clear the flag
|
|
|
|
|
Logged
|
|
|
|
|
Central Illinois
Offline
Newbie
Karma: 0
Posts: 9
trying to understand programming the arduino
|
 |
« Reply #14 on: November 14, 2012, 03:42:16 pm » |
A couple of things... Remove the commented out code, no need for it to waste space. Create a variable that stores the result of currentMillis - previousMillis, and use that in your comparisons; no need to do the arithmetic that many times. Use arrays for the pins and intervals; probably an array of a simple struct simple struct: struct ledData { int pin; unsigned long on; unsigned long off; }; I would also use a variable as a flag that gets set when you turn all the LEDs off, and cleared otherwise. Have a single if statement that checks the flag before even bothering to check if it's been long enough. Psuedocode: if the flag cleared get the time difference check each led to see if it should be on
if it's been long enough turn all leds off set the flag else clear the flag I need to try to figure out what you are talking about as far as setting flags, etc. As far as setting a variable to replace doing the arrythmatic, that would be easy enough, but either way doesn't affect the code and/or the functioning of the code. Not a bad idea to make the code look cleaner.
|
|
|
|
|
Logged
|
Sincere thanks for all help given.
|
|
|
|
|