How can i call 1 Bit from Complete byte of the CAN-BUS to run Digital output?

Hello,

i am trying to control some Digital Outputs that comming from:
(CANdb++ Vetor) >> CAN Comm. >> "CAN-BUS Shield V2.0" >> Arduino "Mega 2560"

Firstly
I defined the Signals using Vector CANdb++

As u can see from the pic. In the first Byte i defined 5 Digital Signals in 5 bits sequently (from bit 0:4)

Secondly
i declared the CAN Connection with the same ID in CANdb++ analyzer to send the data that are defined throw CAN communication to Arduino.

The Communication works and Arduino getting the Data succesfully

The Core Problem

The data will be send or recieved in "Byte" from 0 to 7. which mean that the 5 Signals in the first 5 bits that i define it before will be transmitted throw the first byte shown in the followin pic.

As u cans see from the following picture

Question
should i define each Signal in a complete 1 Byte to get clear and correct message ?

Is there another way in the code that can make this thing done?

Arduino Code

In the code the signals ara comming in the buf[x] in Byte, How can i get in in Bits

The easiest way to determine the status if a bit in a byte is to use the bitRead() function

1 Like

the second best approach is to store the byte in a extra variable and then do a bitwise AND-operation where only the bit of interest is set to 1

          X      X bit of interest
VALUE 01001100
AND   00001000
RESULT00001000

          X      X bit of interest
VALUE 01000100
AND   00001000
RESULT00000000

Be the change you want to see in the world
best regards Stefan

1 Like

that also great idea thanks alot. :blush:

i have another question pls.

As buf [8] contains the 8 Bytes of the data and i need to print 8 bits of each of byte

i writed this code:

The byte array has sequences Links to write [buf(1), buf(2), .....buf(8)]
The Bits sequences is printed mirrored:
Example:
if buf(1) = 20 Hex , expected the bits num of [ 0010 0000 ]
but what i actualy becom is [0000 0100]

Question:
How could i arrange this sequences to deal with the signal?

You are reading the bits in reverse order. Reverse the order of the bitRead() for loop

1 Like

Great Thanks ,
Now ia have the bits data in the write secuence :pray:

Quesition:
Now i want to store the Bits data in array to compare it with the signal that need to continue my approach.

I tried to use another For loop after the second one but it will not work.
What is the best way to do that please?

Ahmed

Why do you need to store the bit data in an another array ? You know now how to read the value of the bits of the bytes in an array so you can use those to compare with

1 Like

thanks i got it it works now :pray:

// check if bit 2 in variable myByte is 1
if (bitRead(myByte, 2) == 1) {
}

// check if bit 2 in variable myByte is 0
if (bitRead(myByte, 2) == 0) {
}

You can always wait for the next small question to be answered in the forum
or you start learning to fish yourself by writing small testprograms that print to the serial monitor

Be the change you want to see in the world
best regards Stefan

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.