Go Down

Topic: 3 switches, code for each switch, trouble figuring out how to read each switch.. (Read 1 time) previous topic - next topic

medic29

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: [Select]

/*
*  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);
}
}
Sincere thanks for all help given.

PaulS

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.

medic29

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?
Sincere thanks for all help given.

PaulS

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.

medic29

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???
Sincere thanks for all help given.

Go Up