Go Down

Topic: How to add switch code for this program I have created? (Read 659 times) previous topic - next topic

justinwebb

I have a dip switch that I want to change between different sets of code but not sure where or how to implement the code.  So when the dip switch is on for 1 I want it to prun program one, when 2 is on and 1 is off run program 2. 

here is my code for program 1
Code: [Select]
/*
  Traffic Light Intersection
  Code for a 4 lane multiple traffic light intersection

 
*/
   int NdoghouseRed =13; //assign North CH dog house
   int NdoghouseYel = 12;
   int NdoghouseGrn = 11;
   int NdoghouseGrnArw = 10;
   int NdoghouseYelArw = 9;
   int N3lightRed = 8;  //assign North CH 3 light
   int N3lightYel = 7;
   int N3lightGrn = 6;
   int S3mRed = 5;  //assign South 3M 3 light
   int S3mYel = 4;
   int S3mGrn = 3;
   int S3mDoghouseRed = 14;  //assign South 3M Doghouse
   int S3mDoghouseYel = 15;
   int S3mDoghouseGrn = 16;
   int S3mDoghouseYelArw = 17;
   int S3mDoghouseGrnArw = 18;
   int W3lightRed = 19;  //assign West CH 3 light
   int W3lightYel = 20;
   int W3lightGrn = 21;
   int Weagle4Red = 22;  //assign West Eagle 4 Light
   int Weagle4Yel = 24;
   int Weagle4Grn = 26;
   int Weagle4GrnArw = 28;
   int EeagleLEDRed = 34;  //assign East Eagle 3 light LED
   int EeagleLEDYel = 32;
   int EeagleLEDGrn = 30;
   int Eeagle4Red = 42;  //assign East Eagle 4 light
   int Eeagle4Yel = 40;
   int Eeagle4Grn = 38;
   int Eeagle4GrnArw = 36;
   long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
  long interval = 1000; //you can define other intervals such as long interval 2,3,4,5  if you need other time choices

void setup()  {
     pinMode (NdoghouseRed, OUTPUT);
     pinMode (NdoghouseYel, OUTPUT);
     pinMode (NdoghouseGrn, OUTPUT);
     pinMode (NdoghouseGrnArw, OUTPUT);
     pinMode (NdoghouseYelArw, OUTPUT);
     pinMode (N3lightRed, OUTPUT);
     pinMode (N3lightYel, OUTPUT);
     pinMode (N3lightGrn, OUTPUT);
     pinMode (S3mRed, OUTPUT);
     pinMode (S3mYel, OUTPUT);
     pinMode (S3mGrn, OUTPUT);
     pinMode (S3mDoghouseRed, OUTPUT);
     pinMode (S3mDoghouseYel, OUTPUT);
     pinMode (S3mDoghouseGrn, OUTPUT);
     pinMode (S3mDoghouseYelArw, OUTPUT);
     pinMode (S3mDoghouseGrnArw, OUTPUT);
     pinMode (W3lightRed, OUTPUT);
     pinMode (W3lightYel, OUTPUT);
     pinMode (W3lightGrn, OUTPUT);
     pinMode (Weagle4Red, OUTPUT);
     pinMode (Weagle4Yel, OUTPUT);
     pinMode (Weagle4Grn, OUTPUT);
     pinMode (Weagle4GrnArw, OUTPUT);
     pinMode (EeagleLEDRed, OUTPUT);
     pinMode (EeagleLEDYel, OUTPUT);
     pinMode (EeagleLEDGrn, OUTPUT);
     pinMode (Eeagle4Red, OUTPUT);
     pinMode (Eeagle4Yel, OUTPUT);
     pinMode (Eeagle4Grn, OUTPUT);
     pinMode (Eeagle4GrnArw, OUTPUT);
     
      //initalize the lights
     digitalWrite (NdoghouseRed, HIGH);
     digitalWrite (N3lightRed, HIGH);
     digitalWrite (S3mRed, HIGH);
     digitalWrite (S3mDoghouseRed, HIGH);
     digitalWrite (W3lightGrn, HIGH);
     digitalWrite (Weagle4Grn, HIGH);
     digitalWrite (Weagle4GrnArw, HIGH);
     digitalWrite (EeagleLEDGrn, HIGH);
     digitalWrite (Eeagle4Grn, HIGH);
     digitalWrite (Eeagle4GrnArw, HIGH);
}

    void loop()  {
    delay (20000);
    digitalWrite (Eeagle4GrnArw, LOW);
    digitalWrite (Weagle4GrnArw, LOW);
    delay (35500);
    digitalWrite (Eeagle4Grn, LOW);
    digitalWrite (Weagle4Grn, LOW);
    digitalWrite (EeagleLEDGrn, LOW);
    digitalWrite (W3lightGrn, LOW);
    digitalWrite (EeagleLEDYel, HIGH);
    digitalWrite (Eeagle4Yel, HIGH);
    digitalWrite (Weagle4Yel, HIGH);
    digitalWrite (W3lightYel, HIGH);
    delay (5000);
    digitalWrite (EeagleLEDYel, LOW);
    digitalWrite (Eeagle4Yel, LOW);
    digitalWrite (Weagle4Yel, LOW);
    digitalWrite (W3lightYel, LOW);
    digitalWrite (EeagleLEDRed, HIGH);
    digitalWrite (Eeagle4Red, HIGH);
    digitalWrite (W3lightRed, HIGH);
    digitalWrite (Weagle4Red, HIGH);
    delay (2000);
    digitalWrite (NdoghouseRed, LOW);
    digitalWrite (N3lightRed, LOW);
    digitalWrite (S3mRed, LOW);
    digitalWrite (S3mDoghouseRed, LOW);
    digitalWrite (NdoghouseGrnArw, HIGH);
    digitalWrite (NdoghouseGrn, HIGH);
    digitalWrite (N3lightGrn, HIGH);
    digitalWrite (S3mDoghouseGrn, HIGH);
    digitalWrite (S3mDoghouseGrnArw, HIGH);
    digitalWrite (S3mGrn, HIGH);
    delay (15000);
    digitalWrite (NdoghouseGrnArw, LOW);
    digitalWrite (S3mDoghouseGrnArw, LOW);
    digitalWrite (S3mDoghouseYelArw, HIGH);
    digitalWrite (NdoghouseYelArw, HIGH);
    delay (5000);
    digitalWrite (S3mDoghouseYelArw, LOW);
    digitalWrite (NdoghouseYelArw, LOW);
    delay (35500);
    digitalWrite (NdoghouseGrn, LOW);
    digitalWrite (N3lightGrn, LOW);
    digitalWrite (S3mGrn, LOW);
    digitalWrite (S3mDoghouseGrn, LOW);
    digitalWrite (NdoghouseYel, HIGH);
    digitalWrite (N3lightYel, HIGH);
    digitalWrite (S3mYel, HIGH);
    digitalWrite (S3mDoghouseYel, HIGH);
    delay (5000);
    digitalWrite (NdoghouseYel, LOW);
    digitalWrite (N3lightYel, LOW);
    digitalWrite (S3mYel, LOW);
    digitalWrite (S3mDoghouseYel, LOW);
    digitalWrite (S3mDoghouseRed, HIGH);
    digitalWrite (S3mRed, HIGH);
    digitalWrite (NdoghouseRed, HIGH);
    digitalWrite (N3lightRed, HIGH);
    delay (2000);
    digitalWrite (W3lightRed, LOW);
    digitalWrite (Weagle4Red, LOW);
    digitalWrite (EeagleLEDRed, LOW);
    digitalWrite (Eeagle4Red, LOW);
    digitalWrite (Eeagle4GrnArw, HIGH);
    digitalWrite (Eeagle4Grn, HIGH);
    digitalWrite (EeagleLEDGrn, HIGH);
    digitalWrite (Weagle4GrnArw, HIGH);
    digitalWrite (Weagle4Grn, HIGH);
    digitalWrite (W3lightGrn, HIGH);
}


here is program 2

Code: [Select]
  int NdoghouseRed =13;
  int N3lightRed = 8;
  int S3mRed = 5;
  int S3mDoghouseRed = 14;
  int W3lightYel = 20;
  int Weagle4Yel = 24;
  int EeagleLEDYel = 32;
  int Eeagle4Yel = 40;
 
  void setup()  {
    pinMode (NdoghouseRed, OUTPUT);
    pinMode (N3lightRed, OUTPUT);
    pinMode (S3mRed, OUTPUT);
    pinMode (S3mDoghouseRed, OUTPUT);
    pinMode (W3lightYel, OUTPUT);
    pinMode (Weagle4Yel, OUTPUT);
    pinMode (EeagleLEDYel, OUTPUT);
    pinMode (Eeagle4Yel, OUTPUT);
   
}
  void loop()  {
    digitalWrite (NdoghouseRed, HIGH);
    digitalWrite (N3lightRed, HIGH);
    digitalWrite (S3mRed, HIGH);
    digitalWrite (S3mDoghouseRed, HIGH);
    delay (500);
    digitalWrite (NdoghouseRed, LOW);
    digitalWrite (N3lightRed, LOW);
    digitalWrite (S3mRed, LOW);
    digitalWrite (S3mDoghouseRed, LOW);
    digitalWrite (W3lightYel, HIGH);
    digitalWrite (Weagle4Yel, HIGH);
    digitalWrite (EeagleLEDYel, HIGH);
    digitalWrite (Eeagle4Yel, HIGH);
    delay (500);
    digitalWrite (Weagle4Yel, LOW);
    digitalWrite (W3lightYel, LOW);
    digitalWrite (EeagleLEDYel, LOW);
    digitalWrite (Eeagle4Yel, LOW);
   
  }

robtillaart

think this way
Code: [Select]

void setup()
{
...  // setup is shared
}

void loop()
{
  if (digitalRead(PINX) == HIGH) program1();
  else program2();
}

program1()
{
...
}

program2()
{
...
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

justinwebb

I will have more then 2 programs forgot to mention that, how would it be coded for like 6 different programs with the switch? 

AWOL

For six programs, you need at least three switches (which would allow up to eight functions to be specified).
Simply read the switch states and combine them into a three bit number, and use switch/case.
e.g.
Code: [Select]

byte select = 0;
bitWrite (select, 0, digitalRead (SW0));
bitWrite (select, 1, digitalRead (SW1));
bitWrite (select, 2, digitalRead (SW2));
switch (select) {

}

"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.

robtillaart


Or use a resistor ladder switch on a analog port ... 
Or use a shift register - need 3 pins for 256 (or more) distinct values
Or ...

Or use 6 Arduino  ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up