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

B=input.readbit();
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?

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.

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

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

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!

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.

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!

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?

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
	currentbyte = Serial.read();
	bitnum = 0; // reset to first bit in the byte
    }
    if ((currentbyte & (1<<(bitnum++))) == 0)
	return false;  // if bit was 0, return 0
    return true;  // otherwise return 1.
}

writebit is similar - fill bits in a byte until it is full, and then output the byte.

These are just hints, of course. Probably not syntactically correct (I'm pretty sure it's more complicated to add a method to the Serial.class, for instance) and neither the most compact nor the most understandable way to write the code...

dronewillow:
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

This is ridiculous - as I thought when the Original Post was the only one in this Thread.

If you add errors into the system it does not make it harder to land the drone, it makes it impossible.

The whole purpose (and even that is doubtful) of the varying vibrations is that the user can respond to consistent changes. If the data can contain any random value then it becomes useless.

if you really want to do something clever then make a system that provides a consistent user experience in the face of possible errors.

...R

Besides all that, there is a checksum in TCP/IP packets that makes bit errors very unlikely.

Packets with a bad checksum should just be dropped and should never reach the client program.

Robin2:
This is ridiculous - as I thought when the Original Post was the only one in this Thread.

If you add errors into the system it does not make it harder to land the drone, it makes it impossible.

My studygroup and I actually made this work before, but programmed it on two PSoC's and an FPGA instead of the arduino. The project was mostly the same though, and with bit flip percentages up to 10%, it IS actually possible.