Pages: [1] 2   Go Down
Author Topic: 3 switches, code for each switch, trouble figuring out how to read each switch..  (Read 1066 times)
0 Members and 1 Guest are viewing this topic.
Central Illinois
Offline Offline
Newbie
*
Karma: 0
Posts: 9
trying to understand programming the arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
/*
 *  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
Offline Offline
Brattain Member
*****
Karma: 632
Posts: 50182
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 9
trying to understand programming the arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 632
Posts: 50182
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 9
trying to understand programming the arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 92
Posts: 3435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 632
Posts: 50182
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 9
trying to understand programming the arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
// 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 Offline
Brattain Member
*****
Karma: 309
Posts: 26485
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Central Illinois
Offline Offline
Newbie
*
Karma: 0
Posts: 9
trying to understand programming the arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 92
Posts: 3435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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 Offline
Edison Member
*
Karma: 52
Posts: 2367
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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 Offline
Newbie
*
Karma: 0
Posts: 9
trying to understand programming the arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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:

Code:
// 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 Offline
Faraday Member
**
Karma: 92
Posts: 3435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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:

Code:
// 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:
Code:
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:
Code:
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 Offline
Newbie
*
Karma: 0
Posts: 9
trying to understand programming the arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
Code:
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:
Code:
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.

Pages: [1] 2   Go Up
Jump to: