user70166: I need to reproduce with a digital pin of an Arduino such a key in the form of a sequence of 1's and 0's, where a one takes 2 ms high and 2 ms low, and a zero takes 1 ms high and 1 ms low.

  int key = 0b101100001111;

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  for (int n = 0; n < 12; n++)
  {
    if (bitRead(key, n) == 1) //execute if bit value == 1
    {
      sendSignal(2000);
    }

    else //execute if bit value == 0
    {
      sendSignal(1000);
    }
  }
}

void sendSignal(int duration)
{
  digitalWrite(13, HIGH);
  delayMicroseconds(duration);
  digitalWrite(13, LOW);
  delayMicroseconds(duration);
}

   

This sketch really works! I figured out what my problem is: I actually need a longer key (64 bits). I am using long type: long key = 0b1000011110001101111100011111001010110001101101111011011100110100; but long only allows 32 bits. What do you recommend to do? Maybe you need to arrange it as an array? I think you can make it easier somehow. thank you

user31105: You should make
byte key = 0b101100001111;

global, i.e. put it BEFORE the setup function and not within it.
Also, a byte only can contain 8 bits, you need 12, so make it an int (thanks to ocrdu, see comment below):
int key = 0b101100001111;

If you need more bits, you can use uint32_t or uint64_t, and count further than the current 12.
Now key is a local variable which is removed after exiting the setup function.
I'm not sure where 'key' comes from in:
switch (bitRead(key, n)) {

Do you get a compiler error, or did you ALSO define a global variable named key ?
Anyway, if you make key global it will fix either way.
A few other remarks:

Try to align your parenthesis, like the 3 }'s at the end, indent them properly, this makes your code much more readable
Instead of the switch/case, you could use an if statement in this case, as the re are only (and always) two possible values (0 and 1):

Thus:
 if(bitRead(key, n) == 0)
 {
     ...
 } 
 else // 1
 {
     ...
 }


Try also comments to write in English instead of Greek, you never know when your comments will be read by non Greek readers (like now).

You can make the following fragment shorter and less duplicated:


Original:
digitalWrite(13, HIGH);
delayMicroseconds(1000);
digitalWrite(13, LOW);
delayMicroseconds(1000);

If you wrap this in a function:
void sendSignal(int duration)
{
    digitalWrite(13, HIGH);
    delayMicroseconds(duration);
    digitalWrite(13, LOW);
    delayMicroseconds(duration);
}

Then you can write a 1 by calling:
sendSignal(2000);

and a 0 by calling:
sendSignal(1000);


user57952: If you modify the code slightly, you can use an array of characters of any length, so need to worry about digital word length.
Here's a modified version of your program that does this.
Note that I also took the liberty of replacing the 'magic number' 13 with LED_PIN, and the 'magic number' 12 with KEY_LEN.  I would suggest you do the same with your pulsewidth values - maybe something like PULSE_WIDTH_ONE_USEC and PULSE_WIDTH_ZERO_USEC?  That way someone else (or you 3 months later) looking at your code can easily see what the values are intended to do
const int KEY_LEN = 62;
const char key[KEY_LEN] = { "10110110110101111001011010110100101011011100110110101101101100" };
const int LED_PIN = 13;

//int key = 0b101100001111;
void setup()
{
  Serial.begin(115200);
  pinMode(13, OUTPUT);
}

void loop()
{
  for (int n = 0; n < KEY_LEN; n++)
  {
    if (key[n]== '1') //execute if bit value == 1
    {
      Serial.print("1");
      sendSignal(200);
    }

    else if (key[n] == '0')//execute if bit value == 0
    {
      Serial.print("0");
      sendSignal(100);
    }
    else
    {
      Serial.print("x");
    }
  }
  Serial.println();
}

void sendSignal(int duration)
{
  digitalWrite(LED_PIN, HIGH);
  //delayMicroseconds(duration);
  delay(duration);
  digitalWrite(LED_PIN, LOW);
  //delayMicroseconds(duration);
  delay(duration);
}


70166: <p>I need to reproduce with a digital pin of an Arduino such a key in the form of a sequence of 1's and 0's, where a one takes 2 ms high and 2 ms low, and a zero takes 1 ms high and 1 ms low.
<a href="https://i.stack.imgur.com/JLtD1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JLtD1.jpg" alt="enter image description here" /></a></p>
<pre><code>  int key = 0b101100001111;

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  for (int n = 0; n &lt; 12; n++)
  {
    if (bitRead(key, n) == 1) //execute if bit value == 1
    {
      sendSignal(2000);
    }

    else //execute if bit value == 0
    {
      sendSignal(1000);
    }
  }
}

void sendSignal(int duration)
{
  digitalWrite(13, HIGH);
  delayMicroseconds(duration);
  digitalWrite(13, LOW);
  delayMicroseconds(duration);
}

   
</code></pre>
<p>This sketch really works! I figured out what my problem is: I actually need a longer key (64 bits). I am using long type: long key = 0b1000011110001101111100011111001010110001101101111011011100110100; but long only allows 32 bits. What do you recommend to do? Maybe you need to arrange it as an array? I think you can make it easier somehow. thank you</p>
 31105: <p>You should make</p>
<pre><code>byte key = 0b101100001111;
</code></pre>
<p>global, i.e. put it BEFORE the <code>setup</code> function and not within it.</p>
<p>Also, a byte only can contain 8 bits, you need 12, so make it an int (thanks to ocrdu, see comment below):</p>
<pre><code>int key = 0b101100001111;
</code></pre>
<p>If you need more bits, you can use uint32_t or uint64_t, and count further than the current 12.</p>
<p>Now <code>key</code> is a local variable which is removed after exiting the <code>setup</code> function.</p>
<p>I'm not sure where 'key' comes from in:</p>
<pre><code>switch (bitRead(key, n)) {
</code></pre>
<p>Do you get a compiler error, or did you ALSO define a global variable named <code>key</code> ?
Anyway, if you make <code>key</code> global it will fix either way.</p>
<p>A few other remarks:</p>
<ul>
<li>Try to align your parenthesis, like the 3 }'s at the end, indent them properly, this makes your code much more readable</li>
<li>Instead of the <code>switch</code>/<code>case</code>, you could use an <code>if</code> statement in this case, as the re are only (and always) two possible values (0 and 1):</li>
</ul>
<p>Thus:</p>
<pre><code> if(bitRead(key, n) == 0)
 {
     ...
 } 
 else // 1
 {
     ...
 }
</code></pre>
<ul>
<li><p>Try also comments to write in English instead of Greek, you never know when your comments will be read by non Greek readers (like now).</p>
</li>
<li><p>You can make the following fragment shorter and less duplicated:</p>
</li>
</ul>
<p>Original:</p>
<pre><code>digitalWrite(13, HIGH);
delayMicroseconds(1000);
digitalWrite(13, LOW);
delayMicroseconds(1000);
</code></pre>
<p>If you wrap this in a function:</p>
<pre><code>void sendSignal(int duration)
{
    digitalWrite(13, HIGH);
    delayMicroseconds(duration);
    digitalWrite(13, LOW);
    delayMicroseconds(duration);
}
</code></pre>
<p>Then you can write a 1 by calling:</p>
<pre><code>sendSignal(2000);
</code></pre>
<p>and a 0 by calling:</p>
<pre><code>sendSignal(1000);
</code></pre>
 57952: <p>If you modify the code slightly, you can use an array of characters of any length, so need to worry about digital word length.</p>
<p>Here's a modified version of your program that does this.</p>
<p>Note that I also took the liberty of replacing the 'magic number' 13 with LED_PIN, and the 'magic number' 12 with KEY_LEN.  I would suggest you do the same with your pulsewidth values - maybe something like PULSE_WIDTH_ONE_USEC and PULSE_WIDTH_ZERO_USEC?  That way someone else (or you 3 months later) looking at your code can easily see what the values are intended to do</p>
<pre><code>const int KEY_LEN = 62;
const char key[KEY_LEN] = { &quot;10110110110101111001011010110100101011011100110110101101101100&quot; };
const int LED_PIN = 13;

//int key = 0b101100001111;
void setup()
{
  Serial.begin(115200);
  pinMode(13, OUTPUT);
}

void loop()
{
  for (int n = 0; n &lt; KEY_LEN; n++)
  {
    if (key[n]== '1') //execute if bit value == 1
    {
      Serial.print(&quot;1&quot;);
      sendSignal(200);
    }

    else if (key[n] == '0')//execute if bit value == 0
    {
      Serial.print(&quot;0&quot;);
      sendSignal(100);
    }
    else
    {
      Serial.print(&quot;x&quot;);
    }
  }
  Serial.println();
}

void sendSignal(int duration)
{
  digitalWrite(LED_PIN, HIGH);
  //delayMicroseconds(duration);
  delay(duration);
  digitalWrite(LED_PIN, LOW);
  //delayMicroseconds(duration);
  delay(duration);
}
</code></pre>
