user40293: OK, I'm working on a project where I want to have multiple toggle switches create a "code" to determine further actions. This is essentially 5-bit communication process that will create output with a 32-bit range of outcomes from 00000 to 11111.
I'm testing the method with three switches, so if the first and third switch are HIGH/on it will read 101. Second and third, 011 etc. I'm not versed enough to know if there is a better method here, but is there a better way to do this?
    // Define the pins being used
int pin_switch = 10;
int pin_switch2 = 11;
int pin_switch3 = 12;

// create int for 3-digit code
int code = 000;
 

void setup() 
{
    Serial.begin(9600); 
    pinMode(pin_switch, INPUT); 
    pinMode(pin_switch2, INPUT); 
    pinMode(pin_switch3, INPUT); 
}
 
void loop()
{

if pin_switch == HIGH;
    {code += 100;}

if pin_switch2 == HIGH;
    {code += 10;}

if pin_switch3 == HIGH;
    {code += 1;}

Serial.print(code);

// operate based on code output
if code == 101;
    {do 101 stuff}

if code == 001
    {do 001 stuff}

// reset code for next loop so numbers don't keep rising
code = 000;
}


user69563: It would be more convenient and less typing if you put all your switch pins in an array. That way you can loop over it to configure the ports and read them rather than copy/pasting the "same" code over and over.
The calculation of the resulting code is a classic "shift and add" loop. I put both the decimal version you seem to want and the more "usual" binary version in the snippet below.
The idea behind that sort of loop is:

start with an empty (zero) code
multiply the code by 10 (or left-shift once for binary)
read a switch, add 1 to the code if it is HIGH
go back to step 2 if there are more switches.

The value of the switches already read get "pushed" one slot to the left before reading the next switch, resulting in the code you want.
The order in which you list the ports in the array is important: the first port in the array will change the leftmost (most significant) digit/bit in the code.
const uint8_t switch_pin[] = { 10, 11, 12 };
const uint8_t num_switches = sizeof(switch_pin)/sizeof(switch_pin[0]);

void setup() {
  // Set all switch pins to input mode
  for (uint8_t i = 0; i < num_switches; ++i) {
    pinMode(switch_pin[i], INPUT);
  }
  Serial.begin(9600);
}

void loop() {
  int code = 0;
  // Read each switch and compute the code
  for (uint8_t i = 0; i < num_switches; ++i) {
    // If the code is supposed to be in decimal
    code = code * 10;
    code += digitalRead(switch_pin[i]);
    /* For a binary version, code only needs to be 8 bits long:
    code <<= 1;
    code |= digitalRead(switch_pin[i]);
    */
  }
  
  // Do stuff with the code
  Serial.println(code);
  delay(200);
}


user42015: Unfortunately your code is all over the place. You have to use C/C++ syntax, which you don't. Also you don't actually read any input.

if statements look like this:
  if(condition){
      my_code();
  }


if(pin_switch == HIGH) (corrected syntax) will always be false, since pin_switch is of value 10. Its just the pin number. To actually read the pin, you need to use digitalRead() like this:
  if(digitalRead(pin_switch) == HIGH)

or even simpler
  if(digitalRead(pin_switch))


It seems that you want to manipulate the bits with code += 100;, but the compiler takes numbers as decimal. So you are adding 100 to code. Please look into the bitwise operators. Also: You can actually write numbers in binary or hex by using 0b101 or 0xF3 respectively (the values here are just examples). This also means, that int code = 000; is the same as int code = 0;.



To build the 3 digit code from input you could put them together via bitwise operators:
code = (digitalRead(pin_switch3) << 2) | (digitalRead(pin_switch2) << 1) | digitalRead(pin_switch);

Depending on your used Arduino and the used pins you can get this even easier. The pins are sorted into ports. Each port has a Special Function register, which holds the current state of the pins. You can mask away any unwanted pins and shift the number to be right justified. For example: On the Uno and Nano pins 2 to 7 belong to PORTD. So, if you use pins 2 to 4 for your 3 switches, you could get the code like:
code = (PIND & 0b00011100) >> 2;

This is a bit more difficult to understand, but it will help you later on, when pursuing Arduino more.

For deciding what action to do based on the code, it is normally easier to use a switch case statement:
switch(code){
    case 0b101:
        do_something();
        break;
    case 0b011:
        do_something_else();
        break;
}


40293: <p>OK, I'm working on a project where I want to have multiple toggle switches create a &quot;code&quot; to determine further actions. This is essentially 5-bit communication process that will create output with a 32-bit range of outcomes from 00000 to 11111.</p>
<p>I'm testing the method with three switches, so if the first and third switch are HIGH/on it will read 101. Second and third, 011 etc. I'm not versed enough to know if there is a better method here, but is there a better way to do this?</p>
<pre><code>    // Define the pins being used
int pin_switch = 10;
int pin_switch2 = 11;
int pin_switch3 = 12;

// create int for 3-digit code
int code = 000;
 

void setup() 
{
    Serial.begin(9600); 
    pinMode(pin_switch, INPUT); 
    pinMode(pin_switch2, INPUT); 
    pinMode(pin_switch3, INPUT); 
}
 
void loop()
{

if pin_switch == HIGH;
    {code += 100;}

if pin_switch2 == HIGH;
    {code += 10;}

if pin_switch3 == HIGH;
    {code += 1;}

Serial.print(code);

// operate based on code output
if code == 101;
    {do 101 stuff}

if code == 001
    {do 001 stuff}

// reset code for next loop so numbers don't keep rising
code = 000;
}
</code></pre>
 69563: <p>It would be more convenient and less typing if you put all your switch pins in an array. That way you can loop over it to configure the ports and read them rather than copy/pasting the &quot;same&quot; code over and over.</p>
<p>The calculation of the resulting code is a classic &quot;shift and add&quot; loop. I put both the decimal version you seem to want and the more &quot;usual&quot; binary version in the snippet below.</p>
<p>The idea behind that sort of loop is:</p>
<ol>
<li>start with an empty (zero) code</li>
<li>multiply the code by 10 (or left-shift once for binary)</li>
<li>read a switch, add 1 to the code if it is HIGH</li>
<li>go back to step 2 if there are more switches.</li>
</ol>
<p>The value of the switches already read get &quot;pushed&quot; one slot to the left before reading the next switch, resulting in the code you want.</p>
<p>The order in which you list the ports in the array is important: the first port in the array will change the leftmost (most significant) digit/bit in the code.</p>

<pre class="lang-c++ prettyprint-override"><code>const uint8_t switch_pin[] = { 10, 11, 12 };
const uint8_t num_switches = sizeof(switch_pin)/sizeof(switch_pin[0]);

void setup() {
  // Set all switch pins to input mode
  for (uint8_t i = 0; i &lt; num_switches; ++i) {
    pinMode(switch_pin[i], INPUT);
  }
  Serial.begin(9600);
}

void loop() {
  int code = 0;
  // Read each switch and compute the code
  for (uint8_t i = 0; i &lt; num_switches; ++i) {
    // If the code is supposed to be in decimal
    code = code * 10;
    code += digitalRead(switch_pin[i]);
    /* For a binary version, code only needs to be 8 bits long:
    code &lt;&lt;= 1;
    code |= digitalRead(switch_pin[i]);
    */
  }
  
  // Do stuff with the code
  Serial.println(code);
  delay(200);
}
</code></pre>
 42015: <p>Unfortunately your code is all over the place. You have to use C/C++ syntax, which you don't. Also you don't actually read any input.</p>
<ul>
<li><p>if statements look like this:</p>
<pre><code>  if(condition){
      my_code();
  }
</code></pre>
</li>
<li><p><code>if(pin_switch == HIGH)</code> (corrected syntax) will always be false, since <code>pin_switch</code> is of value <code>10</code>. Its just the pin number. To actually read the pin, you need to use <code>digitalRead()</code> like this:</p>
<pre><code>  if(digitalRead(pin_switch) == HIGH)
</code></pre>
<p>or even simpler</p>
<pre><code>  if(digitalRead(pin_switch))
</code></pre>
</li>
<li><p>It seems that you want to manipulate the bits with <code>code += 100;</code>, but the compiler takes numbers as decimal. So you are adding 100 to <code>code</code>. Please look into the bitwise operators. Also: You can actually write numbers in binary or hex by using <code>0b101</code> or <code>0xF3</code> respectively (the values here are just examples). This also means, that <code>int code = 000;</code> is the same as <code>int code = 0;</code>.</p>
</li>
</ul>
<hr />
<p>To build the 3 digit code from input you could put them together via bitwise operators:</p>
<pre><code>code = (digitalRead(pin_switch3) &lt;&lt; 2) | (digitalRead(pin_switch2) &lt;&lt; 1) | digitalRead(pin_switch);
</code></pre>
<p>Depending on your used Arduino and the used pins you can get this even easier. The pins are sorted into ports. Each port has a Special Function register, which holds the current state of the pins. You can mask away any unwanted pins and shift the number to be right justified. For example: On the Uno and Nano pins 2 to 7 belong to PORTD. So, if you use pins 2 to 4 for your 3 switches, you could get the code like:</p>
<pre><code>code = (PIND &amp; 0b00011100) &gt;&gt; 2;
</code></pre>
<p>This is a bit more difficult to understand, but it will help you later on, when pursuing Arduino more.</p>
<hr />
<p>For deciding what action to do based on the code, it is normally easier to use a switch case statement:</p>
<pre><code>switch(code){
    case 0b101:
        do_something();
        break;
    case 0b011:
        do_something_else();
        break;
}
</code></pre>
