user77904: import muvium.compatibility.arduino.*; 

public class Class0 extends Arduino{ 

/*
Author: First Name MI. Last Name
Date: June 25, 2021
Title: Arduino Experiment 2
Description: Writing to the Output Pins using the FOR statement

Create a program that will display four different LED sequences having
different transition states repeated at different number of times and 
that the whole program will repeat itself continuously

Apply the concept of using funcions for each of the four LED sequences,
 each of which are to be called upon from the main program.

This time simplify the LED sequence functions using FOR control statements
to achieve the required iterations

Pin modes of each LED pins are to be iterated together using a FOR statement

The circuit:
 * LEDs connected from digital pin 4-11, all with respect to ground.
*/
// ---------------------------------------------------------
// Declaration of Global Variables:
  int led1 = 4; // LED1 connected to digital pin 4
  int led2 = 4; // LED2 connected to digital pin 4
  int led3 = 4; // LED3 connected to digital pin 4
  int led4 = 4; // LED4 connected to digital pin 4
  int switchPin1 = 2; // PushButton connected to digital pin 2
  int switchPin2 = 3; // PushButton connected to digital pin 3
  int buttonState1 = 0; // variable for reading pushbutton status
  int buttonState2 = 0; // variable for reading pushbutton status
// ---------------------------------------------------------
  public void setup() {
    // the setup() method runs once, when the sketch starts
    // initialize the digital pins as outputs;
    
    for (led1 = 4; led1 < 12; led1++) {
      pinMode(led1, OUTPUT); // Configure pin 4 to 11 as outputs
    }
    pinMode(switchPin1, INPUT);// Configure pin 2 as input
    pinMode(switchPin2, INPUT);// Configure pin 3 as input
  }
// ---------------------------------------------------------
  public void loop() {
    // the loop() method runs over and over again,
    // as long as the Arduino has power

    // read the state of pushbutton value
    buttonState1 = digitalRead(switchPin1);
    buttonState2 = digitalRead(switchPin2);

    switch (buttonState1) {
      case (0):
        for (led1 = 7, led2 = 8; (led1 > 3) && (led2 < 12); led1--, led2++) {
        digitalWrite(led1, HIGH); // set the LED on
        digitalWrite(led2, HIGH); // set the LED on
        delay(500); // wait for 500 milliseconds
        digitalWrite(led1, LOW); // set the LED off
        digitalWrite(led2, LOW); // set the LED off
        }
        break;

      case 1:
        for (led1 = 11, led2 = 9, led3 = 7, led4 = 5; (led1 > 9) && (led2 > 7) && (led3 > 5) && (led4 > 3); led1--, led2--, led3--, led4--) {
        digitalWrite(led1, HIGH); // set the LED on
        digitalWrite(led2, HIGH); // set the LED on
        digitalWrite(led3, HIGH); // set the LED on
        digitalWrite(led4, HIGH); // set the LED on
        delay(500); // wait for 500 milliseconds
        digitalWrite(led1, LOW); // set the LED off
        digitalWrite(led2, LOW); // set the LED off
        digitalWrite(led3, LOW); // set the LED off
        digitalWrite(led4, LOW); // set the LED off
        }
        break;
        
      case 2:
        for (led1 = 11, led2 = 10, led3 = 9, led4 = 8; (led1 > 6) && (led2 > 5) && (led3 > 4) && (led4 > 3); led1-=4, led2-=4, led3-=4, led4-=4) {
          digitalWrite(led1, HIGH); // set the LED on
          digitalWrite(led2, HIGH); // set the LED on
          digitalWrite(led3, HIGH); // set the LED on
          digitalWrite(led4, HIGH); // set the LED on
          delay(500); // wait for 500 milliseconds
          digitalWrite(led1, LOW); // set the LED off
          digitalWrite(led2, LOW); // set the LED off
          digitalWrite(led3, LOW); // set the LED off
          digitalWrite(led4, LOW); // set the LED off
          }
          break;

      default:
        for (led1 = 11, led2 = 4; (led1 > 7) && (led2 < 8); led1--, led2++) {
          digitalWrite(led1, HIGH); // set the LED on
          digitalWrite(led2, HIGH); // set the LED on
          delay(500); // wait for 500 milliseconds
          digitalWrite(led1, LOW); // set the LED off
          digitalWrite(led2, LOW); // set the LED off
          }
    }
  }
}


user4143: I don't know what that code is, but it's not Arduino code. It looks like it's wrapped in some other class.  But in real Arduino code...
To "combine" your two button states it helps to think of them as two individual bits within a byte. A byte is made up of 8 bits, and any of them can be on or off. If you associate your two buttons with two of those bits you can represent them as a single number in a byte.
For example if you use:
uint8_t buttonStates = (digitalRead(switchPin2) << 1) | digitalRead(switchPin1);

you create a single variable containing both states.  In simple terms that means "Read switchPin2, shift the result one bit to the left, then read switchPin1 and superimpose the two results together".
So the result is a byte where bit 1 is switchPin2 and bit 0 is switchPin1.
It gives you, then, a truth table of:
SP1 | SP2 | Variable
----+-----+----------
 0  |  0  | 0b00000000
 1  |  0  | 0b00000001
 0  |  1  | 0b00000010
 1  |  1  | 0b00000011

Which of course is the numbers 0-3 in decimal.
So then you can use a switch:
switch (buttonStates) {
    case 0: // switchPin1 == 0, switchPin2 == 0
        // do something
        break;
    case 1: // switchPin1 == 1, switchPin2 == 0
        // do something
        break;
    case 2: // switchPin1 == 0, switchPin2 == 1
        // do something
        break;
    case 3: // switchPin1 == 1, switchPin2 == 1
        // do something
        break;
}


77904: <pre><code>import muvium.compatibility.arduino.*; 

public class Class0 extends Arduino{ 

/*
Author: First Name MI. Last Name
Date: June 25, 2021
Title: Arduino Experiment 2
Description: Writing to the Output Pins using the FOR statement

Create a program that will display four different LED sequences having
different transition states repeated at different number of times and 
that the whole program will repeat itself continuously

Apply the concept of using funcions for each of the four LED sequences,
 each of which are to be called upon from the main program.

This time simplify the LED sequence functions using FOR control statements
to achieve the required iterations

Pin modes of each LED pins are to be iterated together using a FOR statement

The circuit:
 * LEDs connected from digital pin 4-11, all with respect to ground.
*/
// ---------------------------------------------------------
// Declaration of Global Variables:
  int led1 = 4; // LED1 connected to digital pin 4
  int led2 = 4; // LED2 connected to digital pin 4
  int led3 = 4; // LED3 connected to digital pin 4
  int led4 = 4; // LED4 connected to digital pin 4
  int switchPin1 = 2; // PushButton connected to digital pin 2
  int switchPin2 = 3; // PushButton connected to digital pin 3
  int buttonState1 = 0; // variable for reading pushbutton status
  int buttonState2 = 0; // variable for reading pushbutton status
// ---------------------------------------------------------
  public void setup() {
    // the setup() method runs once, when the sketch starts
    // initialize the digital pins as outputs;
    
    for (led1 = 4; led1 &lt; 12; led1++) {
      pinMode(led1, OUTPUT); // Configure pin 4 to 11 as outputs
    }
    pinMode(switchPin1, INPUT);// Configure pin 2 as input
    pinMode(switchPin2, INPUT);// Configure pin 3 as input
  }
// ---------------------------------------------------------
  public void loop() {
    // the loop() method runs over and over again,
    // as long as the Arduino has power

    // read the state of pushbutton value
    buttonState1 = digitalRead(switchPin1);
    buttonState2 = digitalRead(switchPin2);

    switch (buttonState1) {
      case (0):
        for (led1 = 7, led2 = 8; (led1 &gt; 3) &amp;&amp; (led2 &lt; 12); led1--, led2++) {
        digitalWrite(led1, HIGH); // set the LED on
        digitalWrite(led2, HIGH); // set the LED on
        delay(500); // wait for 500 milliseconds
        digitalWrite(led1, LOW); // set the LED off
        digitalWrite(led2, LOW); // set the LED off
        }
        break;

      case 1:
        for (led1 = 11, led2 = 9, led3 = 7, led4 = 5; (led1 &gt; 9) &amp;&amp; (led2 &gt; 7) &amp;&amp; (led3 &gt; 5) &amp;&amp; (led4 &gt; 3); led1--, led2--, led3--, led4--) {
        digitalWrite(led1, HIGH); // set the LED on
        digitalWrite(led2, HIGH); // set the LED on
        digitalWrite(led3, HIGH); // set the LED on
        digitalWrite(led4, HIGH); // set the LED on
        delay(500); // wait for 500 milliseconds
        digitalWrite(led1, LOW); // set the LED off
        digitalWrite(led2, LOW); // set the LED off
        digitalWrite(led3, LOW); // set the LED off
        digitalWrite(led4, LOW); // set the LED off
        }
        break;
        
      case 2:
        for (led1 = 11, led2 = 10, led3 = 9, led4 = 8; (led1 &gt; 6) &amp;&amp; (led2 &gt; 5) &amp;&amp; (led3 &gt; 4) &amp;&amp; (led4 &gt; 3); led1-=4, led2-=4, led3-=4, led4-=4) {
          digitalWrite(led1, HIGH); // set the LED on
          digitalWrite(led2, HIGH); // set the LED on
          digitalWrite(led3, HIGH); // set the LED on
          digitalWrite(led4, HIGH); // set the LED on
          delay(500); // wait for 500 milliseconds
          digitalWrite(led1, LOW); // set the LED off
          digitalWrite(led2, LOW); // set the LED off
          digitalWrite(led3, LOW); // set the LED off
          digitalWrite(led4, LOW); // set the LED off
          }
          break;

      default:
        for (led1 = 11, led2 = 4; (led1 &gt; 7) &amp;&amp; (led2 &lt; 8); led1--, led2++) {
          digitalWrite(led1, HIGH); // set the LED on
          digitalWrite(led2, HIGH); // set the LED on
          delay(500); // wait for 500 milliseconds
          digitalWrite(led1, LOW); // set the LED off
          digitalWrite(led2, LOW); // set the LED off
          }
    }
  }
}
</code></pre>
 4143: <p>I don't know what that code is, but it's not Arduino code. It looks like it's wrapped in some other class.  But in <em>real</em> Arduino code...</p>
<p>To &quot;combine&quot; your two button states it helps to think of them as two individual bits within a byte. A byte is made up of 8 bits, and any of them can be on or off. If you associate your two buttons with two of those bits you can represent them as a single number in a byte.</p>
<p>For example if you use:</p>
<pre class="lang-cpp prettyprint-override"><code>uint8_t buttonStates = (digitalRead(switchPin2) &lt;&lt; 1) | digitalRead(switchPin1);
</code></pre>
<p>you create a single variable containing both states.  In simple terms that means &quot;Read switchPin2, shift the result one bit to the left, then read switchPin1 and superimpose the two results together&quot;.</p>
<p>So the result is a byte where bit 1 is switchPin2 and bit 0 is switchPin1.</p>
<p>It gives you, then, a truth table of:</p>
<pre><code>SP1 | SP2 | Variable
----+-----+----------
 0  |  0  | 0b00000000
 1  |  0  | 0b00000001
 0  |  1  | 0b00000010
 1  |  1  | 0b00000011
</code></pre>
<p>Which of course is the numbers 0-3 in decimal.</p>
<p>So then you can use a switch:</p>
<pre class="lang-cpp prettyprint-override"><code>switch (buttonStates) {
    case 0: // switchPin1 == 0, switchPin2 == 0
        // do something
        break;
    case 1: // switchPin1 == 1, switchPin2 == 0
        // do something
        break;
    case 2: // switchPin1 == 0, switchPin2 == 1
        // do something
        break;
    case 3: // switchPin1 == 1, switchPin2 == 1
        // do something
        break;
}
</code></pre>
