Using binary switch to select program

First post!!

Hi Guys!

I am new to Arduino. I am working on a project to use an UNO and an EL wire shield (opto isolator) to run 8 channels of EL wire.

I took some time to learn the Arduino environment and can write sketches to run the EL wire in different sequences.

I would like the option to select different programs to run to change the sequence that the wire light up in.

I have a rotary 3 bit binary switch that I can wire to the Aduino and use digital read to select a program. What would the code look like to do that? This would give me the chance to run 8 individual programs.

I understand IF and ELSE statements. But what if you want to say if bit one is on, else if bit two is on, else if bit 1 and two are on? <- I know what I just typed looks stupid.

Thank you for your help.

Read all three bits, and use bitWrite() to write them to a byte. Then, simply use that byte in a switch with cases 0 to 7.

If you want to change sequences "on-the-fly", without resetting the program, this code is not as trivial as you would think it is. It's not the most difficult project in the world, but you cannot use delay if you want a responsive program. You must use the technique shown in the BlinkWithoutDelay example. Post at least one example of the sequencing code you currently use.

Your loop will look something like this:

void loop()
{
  byte switch_selection = 0;
  if( digitalRead(sPin1)==LOW ) switch_selection += 1;
  if( digitalRead(sPin2)==LOW) switch_selection += (1<<1);
  if( digitalRead(sPin3)==LOW) switch_selection += (1<<2);

  switch( switch_selection )
  {
    case 0:
      sequence0();
    case 1:
      sequence1();
    case 2:
      sequence2();
    case 3:
      sequence3();

  // etc etc
  }
}

The trick, which may be the hard part for you to come to grips with, is to write each of the sequencing functions so that they do not block. This means that BlinkWithoutDelay will be the most important example in the IDE for you to understand. It even says so in the comments:

Turns on and off a light emitting diode(LED) connected to a digital
pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.

So I can't use a delay function? But that's how I will make the sections turn on and off.

Here is the most basic sequence I would use.

void setup() {                
   // The EL channels are on pins 2 through 9
   // Initialize the pins as outputs
   pinMode(2, OUTPUT);  // channel A  
   pinMode(3, OUTPUT);  // channel B   
   pinMode(4, OUTPUT);  // channel C
   pinMode(5, OUTPUT);  // channel D    
   pinMode(6, OUTPUT);  // channel E
   pinMode(7, OUTPUT);  // channel F
   pinMode(8, OUTPUT);  // channel G
   pinMode(9, OUTPUT);  // channel H
   // We also have two status LEDs, pin 10 on the EL Shield, 
   // and pin 13 on the Arduino itself
   pinMode(10, OUTPUT);     
   pinMode(13, OUTPUT);    
 }

void loop() 
 {
   int x,status;
   
   // Step through all eight EL channels (pins 2 through 9)
   for (x=2; x<=9; x++)
   {
     digitalWrite(x, HIGH);   // turn the EL channel on
     delay(100);              // wait for 1/10 second
     digitalWrite(x, LOW);    // turn the EL channel off

    digitalWrite(10, status);   // blink both status LEDs
     digitalWrite(13, status);
     status = !status; 
   }
 }

Keep in mind I am a rookie here. I am still trying to wrap my head around this code. I am a hardware guy. I can solder a tree branch to a big mac. I can't code for crap.

Thanks

you could start with something like this, simplifying your example sketch.

Notice how I put the EL wires into an array... this simplifies the programming and you can treat the pins as if they were numbered zero through seven (0-7) which makes it programmer friendly. The first element in an array is zero not one.

I added two examples of the BlinkWithoutDelay to your sketch. One in the loop() function where you will notice that the sketch now alternates between two flashing functions, marquee() and knightRider().

then, you will see in each of those functions (marquee() is your code modified for the BlinkWithoutDelay method) that the wires light up in order, and are controlled by the amount of time (100milliseconds) each led is lit.

Now, you can add to your sketch the bit about reading the binary switch and choosing which function to run depending on the values you retrieve. You get to do this all without any blocking code or delays.

int wire [8] = {2,3,4,5,6,7,8,9};  // I created an array for your wires... the EL channels are on pins 2 through 9
unsigned long cycleStart, startTime;
//
void setup() 
{     
  Serial.begin(115200); 
  for (int i = 0; i < 8; i++)
  {
    pinMode(wire[i], OUTPUT);// notice how I initialized the pins with fewer lines of code
  } 
  pinMode(10, OUTPUT);     
  pinMode(13, OUTPUT);    
}

void loop() 
{
  // see note below....................
  if (millis() - startTime >10000UL)
  {
    knightRider();
  }
  else if (millis() - startTime >20000UL)
  {
    marquee();
  }
  else startTime = millis();
  // replace this stuff above here with your code to sense the switch and run your functons.
  // like th Jiggy-Ninja example...
}
//
void marquee()
{
  static int index;
  if (millis() - cycleStart >= 100UL)
  {
    digitalWrite(wire[index], LOW);
    Serial.println(index);
    index++;
    if (index > 7) index = 0;
    digitalWrite(wire[index], HIGH);
    digitalWrite(13, !digitalRead(13));
    digitalWrite(10, !digitalRead(13));//alternating flashing, remove the "!" to flash together
    cycleStart += 100UL;
  }
}
//
void knightRider()
{
  static int index;// not a good practice to use the same variable name here as the other function so shame on me!
  static int myDir = 1;// controls the direction of the 'motion'
  if (millis() - cycleStart >= 100UL)
  {
    digitalWrite(wire[index], LOW);
    index += myDir;
    digitalWrite(wire[index], HIGH);
    digitalWrite(13, !digitalRead(13));
    digitalWrite(10, !digitalRead(13));//alternating flashing, remove the "!" to flash together
    cycleStart += 100UL;
    if (index > 6 || index < 1) myDir = -myDir;
  }
}

teflon1234:
So I can't use a delay function? But that's how I will make the sections turn on and off.

This demo several things at a time may help you to understand how to manage time without using delay().

...R

Thank you. I will do some more research and mess around with the code and wiring.

I will probably be back for more help later. I have one more week until the festival.

You guys are awesome. Thank you.