Skip to main content
2 of 5
added 12 characters in body; edited tags
Michel Keijzers
  • 13k
  • 7
  • 43
  • 59

Form a signal from an array of bits

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. enter image description here

Implement a sequence of 1s and 0s as amplitude modulation.

If you know what the logical one and zero look like, then is it possible to compose them from delays, but only once, somewhere in the beginning. And then, write like this: key = 0b101100001111

Then put a loop, and go through each bit, and if the bit is equal to 1, so that the algorithm itself chooses a sequence of delays inherent to one, that is, 2 ms high, 2 low. I think it can be done with the switch/case selection statement.

I'm posting what I could do, but the sketch doesn't work. Tell me how to do it right! Thanks!

void setup() {
pinMode(13, OUTPUT);
byte key = 0b101100001111;
}
 
void loop() {
for (byte n = 0; n < 12; n++) {
  
switch (bitRead(key, n)) {
  case 0:// выполнить, если значение бита == 0
     digitalWrite(13, HIGH);
     delayMicroseconds(1000);
     digitalWrite(13, LOW);
     delayMicroseconds(1000);
    break;
  case 1:  // выполнить, если значение бита == 1
      digitalWrite(13, HIGH);
     delayMicroseconds(2000);
     digitalWrite(13, LOW);
     delayMicroseconds(2000);
    break;
}
}
}
Антон
  • 265
  • 3
  • 10