# Flipping random bits on the Arduino

Hello guys.

In my project i need to be able the mess up the datastream (data recieved from another Arduino) somehow, which in my case is going to be by flipping bits.

What i want to do is make different functions, eg. 25% flip, 10% flip, 1% flip, etc.

When using eg. the 25% flip function, every bit the Arduino recieves should have a 25% chance of being flipped. Flipped in this case means going from a 0 to a 1 or a 1 to a 0. The same goes for the 10% and 1% functions.

Now, i don't really have an idea on where to start, and how to make this whole flip function, so i was hoping for your guidance here.

Best regards

If (random(100) <= flipPercentage)
B = !B;
output.writebit(B);

Hi westfw, thanks for your answer. Looks like a clever way to do it.

Now, does anyone know how to read the bits one by one? Say, the number im recieving is 254 = 1111 1110 in binary. How do i read the single bits instead of the full byte?

dronewillow:
Hi westfw, thanks for your answer. Looks like a clever way to do it.

Now, does anyone know how to read the bits one by one? Say, the number im recieving is 254 = 1111 1110 in binary. How do i read the single bits instead of the full byte?

Read from what? The serial line? You can’t. You’ll have to read the whole byte, flip the bits you want, and then send the whole byte.

To clarify, i'm probably going to send the sensor data (1 data packet = 2 bytes) by WiFi, and will be recieving them by WiFi too. I am using and ESP8266 (using the espnow.h library) for the WiFi part.

dronewillow:
To clarify, i’m probably going to send the sensor data (1 data packet = 2 bytes) by WiFi, and will be recieving them by WiFi too. I am using and ESP8266 for the WiFi part.

Then you’re definitely not going to read them one bit at a time. Do you know how WiFi works? You’ll be sending UDP or TCP packets. You’ll be sending a lot of stuff with each byte.

I see. Do you have any idea on how to implement it another way then?

dronewillow:
I see. Do you have any idea on how to implement it another way then?

Delta_G:
You'll have to read the whole byte, flip the bits you want, and then send the whole byte.

Oh, so you want me to flip the bits before i send them, and not after i receive them?

dronewillow:
Oh, so you want me to flip the bits before i send them, and not after i receive them?

What? That's not what it said at all.

Seriously, I have no idea what the hell you are building. You've given us some vague description about "flipping bits" that honestly makes no sense at all why you would be doing that. So I have no idea what is the best way to accopmplish your actual goal since you haven't shared that.

If you want an Arduino to read from Serial (or something else) and then retransmit that with some bits flipped then you will have to do that one byte at a time. You can't read single bits like that.

Sorry if i haven't been clear enough.

I am building a setup including a drone, a distance sensor (connected to arduino 1) and a vibration motor (connected to arduino 2).

The idea is, to put a distance sensor under the drone -> measure distance to the ground -> send distance from the arduino on the drone (arduino 1) to the arduino on the ground (arduino 2) -> make the vibration motor vibrate more, the further away from the ground the drone is.

The whole idea is to make it possible to land the drone with your eyes closed.

After this works, then i want to emulate a bad connection. This means adding artificial delay and flipping some of the bits, to simulate errors in the transmission between the two arduinos.

By the way, thanks for helping me. I really appreciate it!

dronewillow:
After this works, then i want to emulate a bad connection. This means adding artificial delay and flipping some of the bits, to simulate errors in the transmission between the two arduinos.

Why? You just have a few extra drones around to crash? Why not add error checking to your comms so dropped bits aren't ever a problem?

It's actually on purpose.

I am trying to make a visual demonstration of why reliable low latency networks are important, so that's why I want to add errors instead of removing them.

I am height limiting the drone (a DJI Matrice 100), so it can't really break if it crashes.

dronewillow:
I am trying to make a visual demonstration of why reliable low latency networks are important, so that's why I want to add errors instead of removing them.

By intentionally introducing errors? Doesn't that negate the test? Because now you are relying on transmitting the bad data with perfect fidelity. So it's really good data when you get it.

Why not do something that actually messes up your comms like taking off the antenna or something. I still don't understand the purpose of this exercise. If you really want to demonstrate to a group the importance of error checking in communications then you don't need any drones or computers. Just let them play the telephone game.

I know, that the bad data, would actually be good data. For this exact experiment, that is not really the important part though. The important part is just to be able to have different levels of errors, like 25% of the bits flipped, 10% of the bits flipped, 1%, etc.

It's not really a test, just a demonstration, where the drone needs to be a part of the demonstration.

I can understand, why you don't understand the way i'm doing this, i just need help figuring out the bit flip part. Thanks for your input though - i'll be sure to keep that in mind in the future, when doing demonstrations like this!

dronewillow:
just a demonstration

Of what? What is supposed to happen? Why not just forget the whole bit flipping in the second arduino and just send bad data to begin with then.

A demonstration of what happens if you have a bad connection, that introduces bit flips or delay.

If i just send the bad data from the first arduino, then that would still mean bit flipping, just on the first arduino instead, wouldn't it? In that case, do you have any proposals as to how to go about that?

dronewillow:
A demonstration of what happens if you have a bad connection, that introduces bit flips or delay.

No, I think we’ve established this much. I mean are you going to say, flip a switch and then the drone crashes or what. How will this demonstrate anything? If you just want thte drone to crash there are much easier ways to do that.

dronewillow:
In that case, do you have any proposals as to how to go about that?

Sure, check out the bitmath section of the reference area. Everything you need is there. Never be afraid to RTFM.

Look under “Bits and Bytes”.

You should probably also find the bitshift operators << and >>. they might be useful if you want to do this fast. But with the stuff from Bits and Bytes you can do what you want.

No, i want to flip different switches, each with a different amount of bit flip - and make it harder for the user to land the drone, depending on which amount of bit flip he chooses. Remember, that the user is blindfolded, and purely rely on the vibration strength of the vibration motor, to determine how far above the ground the drone is. This means, that the more bit flip, the harder it is for the user to determine the distance to the ground, thus making the landing more difficult

Thanks for that link, i'll be sure to check it out. I just didn't know where to look. The bitRead() looks like just what i need. Thanks!

``````boolean Serial.readbit() {
static uint8_t bitnum = 0;   // next bit to read
static uint8_t currentbyte;  // a saved byte's worth.
if (bitnum > 7) {  // all out of bits from the last byte
// so get another byte;
while (Serial.available() <= 0)
; // spin, waiting for a byte