0

enter image description here

I need to simulate a pulse train. Everything is in order with this code, it solves its problem. The question is: I need to change the keys, but it is too difficult to manually type in so many zeros and ones each time. On the other hand, I know that my hexadecimal key looks much shorter: "B8C4F9". Is it possible to create a string in such a way that the system itself translates to the binary system?

And the second question: How can you automatically measure the length of an array so as not to manually calculate KEY_LEN?

const int KEY_LEN = 24; 
const char key[KEY_LEN] = {"101110001100010011111001"}; 

int T = 200; // Pulse repetition period in microseconds
int Zero = 80; //0.4*T; // Duration of zero in microseconds
int One = 120; //0.6*T;// Duration of a unit in microseconds

const int LED_PIN = 13; // Used digital pin 13 of the arduino to connect the transmitter

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

void loop()
{
  for (int n = 0; n < KEY_LEN; n++)
  {
    if (key1[n] == '1') //If the bit read == 1
    {
      sendSignal(One);
    }

    else if (key1[n] == '0')//If the bit being read == 0
    {
      sendSignal(Zero);
    }
  }
}


void sendSignal(int duration)
{ 
  digitalWrite(LED_PIN, LOW);
  delayMicroseconds(duration);
  digitalWrite(LED_PIN, HIGH);
  delayMicroseconds(T - duration);
}
2
  • is this for a TV remote control?
    – jsotola
    Mar 23, 2021 at 3:51
  • This is to control the front door
    – Антон
    Mar 23, 2021 at 4:23

1 Answer 1

1

There is really no reason to save the key as a string, as you are doing. You can just save it as a normal number. Then you can also use the hexadecimal representation on your code:

unsigned long key = 0xB8C4F9;

Then you need to send out each bit of that number:

for (int n = 0; n < KEY_LEN; n++)
{
    if (key & (1 << n)) { //If the bit read == 1
        sendSignal(One);
    } else {//If the bit being read == 0
        sendSignal(Zero);
    }
}

The following is happening in the if statement: (1 << n) will take the value 1 (binary 0b00000001) and will shift the bits to the left by n digits. For example: If n is 2, we get (0b00000001 << 2) = 0b00000100. With this we can select a bit in our number. Then we take that and do a bitwise AND & with the key. That will set all bits to zero, which are not currently selected. The resulting number is zero, if the corresponding bit in key is zero. If the corresponding bit is 1, this will return a number different from zero (which is interpreted as true. Thus we check, if the n-th bit of key is one or zero.

How can you automatically measure the length of an array so as not to manually calculate KEY_LEN?

Generally you can calculate the size of an array with the sizeof() function:

unsigned int size_of_array = sizeof(array)/sizeof(array[0]);

sizeof() returns the number of bytes. So to get the number of elements, we need to divide by the number of bytes in each element;

For the way shown above this is not that easy. Normally it is not really needed, since such keys normally don't change length very often. If you find a variable type, which exactly fits your key size, you could use sizeof(key)*8 to get the number of bits in it. But your current value 0xB8C4F9 only needs 3 bytes. I don't know a type with 3 bytes, but maybe somewhere you can get something like uint24_t. Honestly here I would just use a hardcoded size, just like you are using a hardcoded key value.

8
  • But in this case, the bits are output in reverse (from end to beginning). That is, if the key is in the form, for example "D", then 1011 will be displayed, but this is not true, you need 1101.
    – Антон
    Mar 25, 2021 at 11:23
  • For the values to be read correctly, you need to do this, but it is very long and you can get confused: if (key & (b100000000000000000000000>> n))
    – Антон
    Mar 25, 2021 at 11:48
  • Or like this:: if (key & (0x800000>> n))
    – Антон
    Mar 25, 2021 at 11:54
  • 1
    @Антон Actually no. If you want to change the endianess (from LSB first in my code to MSB first), you just need to change the count direction in the for loop: for(int n = KEY_LEN-1; n>=0; n--) This would start at the KEY_LEN-1'th bit (MSB of your key) and count down to bit zero (LSB). No need to write that MSB directly as binary or hex.
    – chrisl
    Mar 25, 2021 at 12:14
  • 1
    In general there is no "last bit". Instead the terms MSB and LSB (Monst/Least Significant Bit) are used. The numbers in C/C++ are right justified, having the LSB at the right, MSB at the left. key & 1 gives you the LSB. key & (1 << KEY_LEN-1) gives you the MSB. If you want to send the bits MSB first (then going down till LSB), you would start at key & (1 << KEY_LEN-1) as the first bit in your transmission and go down to key & 1 (aka key & (1 << 0)), which would then be the last in your transmission.
    – chrisl
    Mar 25, 2021 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.