why bitWrite is adding bits to my variable instead of changing it?

I'm programming a learn mode to store RC Switch signal, and I'm having some issues with the bitWrite function.

Code:

---



```
if (LearnMode == 1){
remotecontrol1 = remotecontrolsignal;
bit1R1 = bitRead(remotecontrol1, 4);
bit2R1 = bitRead(remotecontrol1, 5);
if (bit1R1 == 1 && bit2R1 == 0){                    //Received signal using button 1, so store code from button 1, change bits and store button 2 and button 3.
remotecontrol1B1 = remotecontrol1;
Serial.println("Button 1");
EEPROM.put(EEPROMaddrR1B1, remotecontrol1B1); //bits 4 and 5 are 10, it means button 1.

bitWrite(remotecontrol1B1, 4, 0);
        bitWrite(remotecontrol1B1, 5, 1);
        remotecontrol1B2 = remotecontrol1B1;
        EEPROM.put(EEPROMaddrR1B2, remotecontrol1B2);  //bits 4 and 5 now are 01, it means button 2.

bitWrite(remotecontrol1B1, 4, 1);
        bitWrite(remotecontrol1B1, 5, 1);
        remotecontrol1B3 = remotecontrol1B1;
        EEPROM.put(EEPROMaddrR1B3, remotecontrol1B3);  //bits 4 and 5 now are 11, it means button 3.

Serial.println("LearnMode DISABLED");
        LearnMode = 0;
      }
```

|

and it gives me this values:
remotecontrolsignal: 1011011010100010101011010101 (this is the input from the remote).
remotecontrol1B1: 101010111101011110010111010101
remotecontrol1B2: 1101010001010101111010111100101
remotecontrol1B3: 1011011010100010101011110101

But if I use a code where I input this binary and change it using bitWrite, everything works perfectly. So I don't see why it adds more bits instead of changing it in this code.

I'm saving separated values for each button because I tried to save just the "id" (first 22 bits) and use bitRead to see which button was pressed but I couldn't find a way to do that (save just the ID).

I'm new to programming and the forum, so any suggestions or advice are welcomed.

Forget about your program for a minute and write a small program that illustrates the problem then post it here

Are you perhaps confusing the bit order of the target variable and/or that bit counting starts from zero ?

I cannot understand your problem. You write that bitWrite works perfectly, and then you write that it adds bits instead of changing them. Your snippet does not tell anything about data types, EEPROM addresses and so on, so that is a tough one..

Sure. Here I used the same value from my remote control signal (1011011010100010101011010101) and
and changed it using this code:

Code:


---




```
void setupcolor=#000000[/color] {
Serial.begincolor=#000000[/color];
long remotecontrolsignal = 1011011010100010101011010101;
Serial.println(remotecontrolsignal, BIN);
bitWrite(remotecontrolsignal, 5, 0);
bitWrite(remotecontrolsignal, 4, 1);
Serial.println(remotecontrolsignal, BIN); //it prints 1011011010100010101011010101
bitWrite (remotecontrolsignal, 5, 1);
bitWrite (remotecontrolsignal, 4, 0);
Serial.println(remotecontrolsignal, BIN); //it prints 1011011010100010101011100101
bitWrite (remotecontrolsignal, 5, 1);
bitWrite (remotecontrolsignal, 4, 1);
Serial.println(remotecontrolsignal, BIN); //it prints 1011011010100010101011110101
}

void loopcolor=#000000[/color] {}
```

|

As you can see, it works the way it should works. But if I use the previous code, it gives me other values (each Serial.print of this code should be each remote control button from previous code)

Danois90:
I cannot understand your problem. You write that bitWrite works perfectly, and then you write that it adds bits instead of changing them. Your snippet does not tell anything about data types, EEPROM addresses and so on, so that is a tough one..

That's the problem haha, I don't know why it works in one code and doesn't work in other. You mentioned EEPROM, you think this would be the problem?

Your snippet does not reveal the declarations of "EEPROMaddrR1B*", nether does it reveal from where the value "remotecontrolsignal" comes, neither does it reveal which data types are used for the variables. These informations are essential if you want help and that is why you should never post snippets.

long remotecontrolsignal = 1011011010100010101011010101;

What do you think that this line does ?

Danois90:
Your snippet does not reveal the declarations of "EEPROMaddrR1B*", nether does it reveal from where the value "remotecontrolsignal" comes, neither does it reveal which data types are used for the variables. These informations are essential if you want help and that is why you should never post snippets.

Oh, I see. As I mentioned before, I'm new to all of this, and I didn't pay attention to this detail. Sorry about that.

But your answer helped me see where the problem is. The problem is with the 'EEPROM.put' and not with 'bitWrite'. Thanks.

UKHeliBob:

long remotecontrolsignal = 1011011010100010101011010101;

What do you think that this line does ?

it stores 1011011010100010101011010101 to remotecontrolsignal.

Now I see that my problem is how I'm storing this value to EEPROM. I have no clue how to solve this, but now I know where the problem is and what I should search for.

it stores 1011011010100010101011010101 to remotecontrolsignal.

It tries to store 1011011010100010101011010101 in the variable and fails because the number is too big

You need to tell the compiler that the number is in binary rather than decimal (the default)

long remotecontrolsignal = 0b1011011010100010101011010101; would be a starting point

UKHeliBob:
It tries to store 1011011010100010101011010101 in the variable and fails because the number is too big

You need to tell the compiler that the number is in binary rather than decimal (the default)

long remotecontrolsignal = 0b1011011010100010101011010101; would be a starting point

@OP
1011011010100010101011010101 is a number -- in which base? Base 10 or base 2 or base 16 or base 8?

If you begin the number with this preamble 0x (zero x), then it is a number in base 16. That means:
1011011010100010101011010101
==> 0x1011011010100010101011010101
=1x1627 + 0x1626 + ... + 1x160

If you begin the number with this preamble 0b (zero b), then it is a number in base 2. That means:
1011011010100010101011010101
==> 0b1011011010100010101011010101
=1x227 + 0x226 + ... + 1x20

If you begin the number without any preamble, then the it is a number in base 10. That means:
1011011010100010101011010101
==> 1011011010100010101011010101
=1x1027 + 0x1026 + ... + 1x100
= 1000000000000000000000000000 + 0 + 10000000000000000000000000 +.....+ 1

Now, you see how big is your number which you have tried to store in a 32-bit variable.

GolamMostafa:
@OP
1011011010100010101011010101 is a number -- in which base? Base 10 or base 2 or base 16 or base 8?

If you begin the number with this preamble 0x (zero x), then it is a number in base 16. That means:
1011011010100010101011010101
==> 0x1011011010100010101011010101
=1x1627 + 0x1626 + ... + 1x160

If you begin the number with this preamble 0b (zero b), then it is a number in base 2. That means:
1011011010100010101011010101
==> 0b1011011010100010101011010101
=1x227 + 0x226 + ... + 1x20

If you begin the number without any preamble, then the it is a number in base 10. That means:
1011011010100010101011010101
==> 1011011010100010101011010101
=1x1027 + 0x1026 + ... + 1x100
= 1000000000000000000000000000 + 0 + 10000000000000000000000000 +.....+ 1

Now, you see how big is your number which you have tried to store in a 32-bit variable.

Yes, I see. But how can I store a number this big into EEPROM? I need to save at least 22bits (first 22 from the left to right), I tried to use highByte and lowByte to split it, but it's not working.

Code:


---




```
#include <EEPROM.h>

void setupcolor=#000000[/color] {
  Serial.begincolor=#000000[/color];
  long remotecontrolsignal = 1011011010100010101011010101; 
  Serial.println(remotecontrolsignal, BIN);
  bitWrite(remotecontrolsignal, 5, 0); 
    bitWrite(remotecontrolsignal, 4, 1);
  Serial.println(remotecontrolsignal, BIN); //it prints 1011011010100010101011010101
    bitWrite (remotecontrolsignal, 5, 1);
  bitWrite (remotecontrolsignal, 4, 0);
  Serial.println(remotecontrolsignal, BIN); //it prints 1011011010100010101011100101
  bitWrite (remotecontrolsignal, 5, 1);
  bitWrite (remotecontrolsignal, 4, 1);
  Serial.println(remotecontrolsignal, BIN); //it prints 1011011010100010101011110101

EEPROM.write(0, highBytecolor=#000000[/color]);
  EEPROM.write(1, lowBytecolor=#000000[/color]);
  byte high = EEPROM.readcolor=#000000[/color];
  byte low = EEPROM.readcolor=#000000[/color];
  int value = word(high, low);
  Serial.print(value, BIN);

}

void loopcolor=#000000[/color] {}
```

|

and this is what I get from serial:

13:17:58.801 -> 11110011111100101001000110101
13:17:58.835 -> 11110011111100101001000010101
13:17:58.868 -> 11110011111100101001000100101
13:17:58.901 -> 11110011111100101001000110101
13:17:58.935 -> 101001000110101

I forgot to ask, how can I declare the base if I get this value from a library output (rc switch)?
In the original code, this value (1011011010100010101011010101) is given from 'mySwitch.getReceivedValue();', so every time it receives a signal, it gives a number, then I check if this number is saved on EEPROM, and if it is, it does something.

But how can I store a number this big into EEPROM?

Why would you want to when you could save a binary number that is not so large then read/set the bits of the number

need to save at least 22bits (first 22 from the left to right),The mention of left to right worries me. Why not right to left, which would be more conventional.

Please post the full program that receives and stores the 22 bits

UKHeliBob:
Why would you want to when you could save a binary number that is not so large then read/set the bits of the number

need to save at least 22bits (first 22 from the left to right),The mention of left to right worries me. Why not right to left, which would be more conventional.

Please post the full program that receives and stores the 22 bits

I'm a little confused. I'm not saving it in binary as long I understood from this topic, so how can I do that? Knowing that the number is given from the library.

And I need to save the first 22 from the left because the last 6 bits are always the same.
bit 0=1, bit 1=0, bit 2=1, bit 3=0 (anti code), and bit 4 and 5 are 1 or 0 based on the button I press in the remote control. That's how my remote control sends data. Up to now, I don't have a code that stores these 22 bits, I was trying to store all 28 bits (that was the code that I posted at the beginning of the topic)

I'm a little confused.

Join the club

I'm not saving it in binary as long I understood from this topic,

You seem to be trying to save each bit into a long variable, but you are not doing that. The first thing to do is to use an unsigned long rather than a signed one otherwise I predict more complications

Next you need to realise that 101010, for instance, is not the same as 0b101010, as has been pointed out in the thread, 101010 is a number in base 10. 0b101010 is a number in base 2

Up to now, I don't have a code that stores these 22 bits

OK, but have you got a complete program that receives the 22 bits ?

UKHeliBob:
Join the club
You seem to be trying to save each bit into a long variable, but you are not doing that. The first thing to do is to use an unsigned long rather than a signed one otherwise I predict more complications

Next you need to realise that 101010, for instance, is not the same as 0b101010, as has been pointed out in the thread, 101010 is a number in base 10. 0b101010 is a number in base 2
OK, but have you got a complete program that receives the 22 bits ?

I got it. Yes, I have, but now it is trying to receive all 28bits and it is failing because my variable is not being stored correctly. So, I need to find a way to convert this value into binary, then store it to my variable. Am I right??

I'm studying a little more about binary and decimal to get it clear in my head :stuck_out_tongue_closed_eyes:

How are you receiving the 28 bits ?

So, I need to find a way to convert this value into binary, then store it to my variable. Am I right??

Who knows ? We don't know how you are receiving the data, what data type it is, whether you receive it bit by bit or as a single unsigned long, for instance

Once again, please post your code

So, I'm using rc switch and I need to store "mySwitch.getReceivedValue();", but I'm having some issues doing that. As I could understand, when I get this value and try to store it, my program is converting it to decimal. I don't understand much about a binary and decimal convert, but what I could get is that to be binary it needs to have "0b" before the number, but how can I put that 0b to my value? or what should I do to store it?

P.S.: The value is a 28bit binary number.

You already have a thread on this subject

Why did you start a second one ?