Hot answers tagged

7

You need to use boolean arithmetic. Use the AND operator (&) to combine the incoming value AND the "mask" value (the value of the bits you do care about) and see if it equals the mask bit: byte wanted = 0b11001110; // 207 byte incoming = 223; // for example 11011111 if ((wanted & incoming) == wanted) { // whatever } The AND operator compares ...


5

you can do it in a single line: bool isSet = (var & (1 << bitNumber)) != 0; 1 << bitNumber is a bit shift in this case it means that the 1 will be multiplied with the power of 2 & is the bitwise AND operator. The each bit of the result is 1 if and only if both the corresponding bits in the inputs is 1.


5

You are almost there with that. Personally I'd use OR not +: This is a better way of defining the pins instead of using 1, 2, 4 etc. It results in the same thing, but you can see the bit number in there. The << is left shift. #define PIN0 (1 << 0) #define PIN1 (1 << 1) #define PIN2 (1 << 2) #define PIN3 (1 << 4) Now you can ...


3

The bits within a byte are numbered 0 through 7 from right to left. The rightmost (least significant) is bit 0, next is bit 1... and the leftmost (most significant) is bit 7. The expression _BV(3) builds a byte where bit 3 is set (is 1) and all other bits are clear (are 0). In binary it looks like this: position: 7 6 5 4 3 2 1 0 bit value: 0 0 ...


2

GyX=Wire.read()<<8|Wire.read(); This is a bug. It reads two bytes from the I2C bus and builds a 16-bit number from them. One of the bytes (we don't know which one) will be the most significant byte of the result. The other byte will be the least significant byte. This instruction is likely to work just fine on some particular version of some ...


2

Seeing as your data typpe has the volatile keyword maybe the value of inBuf is being changed on another section of code like an interupt, other wise i see no reason for the variable to have a value of 32. If you want to only use one bit you should just use the boolean data type with a true or false value. In that bit of code i assume you are trying to ...


2

A few issues here: The way you “rename” the arrays is wrong, and furthermore there is no point in doing such renaming. There is no reason to divide by 10 (ten, in decimal) within your code: as everything you do is binary, the number ten has nothing to do with what are trying to accomplish. While doing the addition, you forgot to loop over the bits. You ...


2

Personally I'd expand it out a little: if (commandbits & (1 << i)) { PORTB |= (1<<3); } else { PORTB &= ~(1<<3); } It's far more readable, and there are less shifts to mess around with. Plus the AVR has bit set and bit clear operations which that will make use of, especially since it's now using literal values. You could ...


2

There are ways to mask off the don't care bits. Say your example, 0b11xx1111. Then incomingByte = Serial.read(); incomingByte = incomingByte || 0b00110000; // make 4, 5 high if (incomingByte == 0b11111111){ // got all 1s, do something } else { // some 0s received, do something else }


2

There are only two errors I can spot in your program: A block of code should be delimited by curly braces: for (count=0; count<8; count++){ pinMode(pinArray[count],OUTPUT); } // ← ‘}’ instead of ‘]’ If a function expects to be passed arguments, these should be declared as parameters in the function prototype: void ledDisplay(byte x){ // ← mind ...


2

If you modify the code slightly, you can use an array of characters of any length, so need to worry about digital word length. Here's a modified version of your program that does this. Note that I also took the liberty of replacing the 'magic number' 13 with LED_PIN, and the 'magic number' 12 with KEY_LEN. I would suggest you do the same with your ...


2

You should make byte key = 0b101100001111; global, i.e. put it BEFORE the setup function and not within it. Also, a byte only can contain 8 bits, you need 12, so make it an int (thanks to ocrdu, see comment below): int key = 0b101100001111; If you need more bits, you can use uint32_t or uint64_t, and count further than the current 12. Now key is a local ...


2

Your code loops from the high order byte to the low order byte, printing each byte as 2 hex digits. If you want instead to print the first byte as a decimal value, and then the remaining bytes as another decimal value, then do this: Print byte 0 in base 10: Serial.print(data[0]); //first byte Serial.print(":"); Then loop through the remaining ...


2

You can use the bit shift and and operator, like: uint32_t data = 0xFFFF0001; The following line will shift all bits 15 places to the right, meaning the least significant 15 bits will be removed, and the other bits shifted to the right. uint32_t dataLeft = data >> 15; 11111111111111110000000000000001 >>>>>>>>>>>>&...


2

You always write a whole byte when you set the register. You only need to "mask out" those bits if there's a danger that you may inadvertently set them to 1, for instance through some value over flow from other bits when combining values together to form that byte. If you're not doing that then there's nothing to mask out. For instance if you just ...


2

Unfortunately your code is all over the place. You have to use C/C++ syntax, which you don't. Also you don't actually read any input. if statements look like this: if(condition){ my_code(); } if(pin_switch == HIGH) (corrected syntax) will always be false, since pin_switch is of value 10. Its just the pin number. To actually read the pin, you need ...


2

It would be more convenient and less typing if you put all your switch pins in an array. That way you can loop over it to configure the ports and read them rather than copy/pasting the "same" code over and over. The calculation of the resulting code is a classic "shift and add" loop. I put both the decimal version you seem to want and the ...


2

You have two problems with your code: byte is equal to unsigned char and can only hold exactly one byte, aka 8 bits (not 16 bits like your comment says). But you are trying to assign a 16bit number to it. The upper 8 bits are cut out in that process. They are lost. If you want to hold a 16bit number, you need to use a data type, that is at least 16 bits in ...


1

The error occurs, because you try to use the syntax for a 2 dimensional array with a 1 dimensional array (since you have an array of bytes, not bits, and a microcontroller always works with at least one byte). For accessing the individual bits you need to use bitwise operators. For example this: theDisplay[3] |= (1 << bitnumber); // for setting the bit ...


1

The function shiftOut() only works for 8-bit values, as its documentation says. Additionally, a byte can only store 8 bits. It doesn't matter how many bits you try to assign. (The compiler should have given you a warning, but the IDE likes to hide this.) You need to write your own output function to shift out 16 bits, AFAIK.


1

You need to send the 5x24 bits out without a pause. If you then keep the line low for over 80us, that is interpreted by all LED drivers as the reset signal. It is not stated explicitly in the datasheet (at least I haven't found it), but I guess the reset signal means, that at that time all drivers will output the received values on the LEDs. That way you can ...


1

As usual with programming, there are many solutions, for example: You can use a bitmask, put 4 variables in an array with binary values 1000, 0100, 0010 and 0001 and when the switch is pressed, you store the value into each LED pin by comparing the value. However, a more simpler solution is just use a counter from 0 to 4, if the value is 1, the first LED ...


1

The rules of integer arithmetics are tricky in C++. There are rules that give the type of integer literals: 256 and 200 are of type int, which is the default type for integer literals. On the Nano, this type is 16-bits, and covers the range from −32,768 to +32,767. 65536 being too large for the int type, it is implicitly considered a long int. This type is ...


1

You said "I defined TA as a float." That doesn't matter. In C/C++, expressions are only "promoted" to a larger/different data type AFTER they are evaluated. Your calculations are being done using 16-bit ints, which overflow, and then cast to a float. (C/C++ evaluates expressions from the inner-most outward, and if one of the operands to an operator is a ...


1

In C++ you are not allowed to declare non-static member arrays with unspecified size (empty []). Supplying an in-class brace-or-equal (BoE) initializer for such non-static member array does not override this rule struct S { int a[] = { 1, 2, 3 }; // <-- ERROR: `[]` array declaration is not allowed here }; If you really need these arrays as non-static ...


1

If you like both portability, readability and performance there are a few libraries (for instance Arduino-GPIO): #include "GPIO.h" GPIO<BOARD::D11> data; // Setup data.output(); // Manual loop unrolling for speed. Could ask the compiler to do this // Member function write() data.write(commandbits & 0x80); data.write(commandbits & 0x40); ...


1

Bit-wise sending of a chunk of data is really just: go through each byte of the data for each byte (= 8-bits), put each bit on the LED and wait This also has some substantial drawbacks: no error detection or correction. If you get interference on your light sensor / camera due to the environment, you will read wrong data. For this you would need to code ...


1

You're making a mountain out of a molehill. You're trying to manually manipulate binary digits in an array, but C can already manipulate binary digits. Everything is binary already - you just need to represent it as such. All you need to do is convert the contents of your array into an actual number, then do simple mathematics on that number. One of the ...


1

There are two types of right-shift in C - logical and arithmetic. GCC (which is the compiler the Arduino IDE uses) selects which one to use depending on whether the type you are shifting is signed or not: If it's signed then use an arithmetic shift If it's unsigned then use a logical shift In a logical shift, the value is literally shifted to the right ...


Only top voted, non community-wiki answers of a minimum length are eligible