Assign each bit ,in a serially received bitarray, into a function

LarryD:
See:
https://www.arduino.cc/en/Tutorial/Array

Use similar technique to turn on LEDs on your particular Arduino.

.

thats just sequencing the leds right?

Yes It was the use of an array that would be useful.

The port method Crossroads shared would be the way to proceed.

0d01001010 0b01001010 ;)

The byte read with serial gets sent to PORTD and PORTB which make up the 8 LED outputs. .

LarryD: 0d01001010 0b01001010 ;)

The byte read with serial gets sent to PORTD and PORTB which make up the 8 LED outputs. .

Ok great now half of the problem is done but now how to make each arduino know which bits to work on? Bec maybe like every 500ms i would send a whole new array consisting of 80 bits so how will i make for example arduino number 3 to know to work on bits 24-32?

Give each arduino a 4-bit input that it can read and use as its 'address'. 4 pins that you connect high or low, unique for each one. Then you can have the same code in all slaves. As part of setup(), read in the address and use that to compare against the address that was sent out. If it matches, act on the data.

Or, make the code unique for each slave, and put the address in its code.

CrossRoads: Give each arduino a 4-bit input that it can read and use as its 'address'. 4 pins that you connect high or low, unique for each one. Then you can have the same code in all slaves. As part of setup(), read in the address and use that to compare against the address that was sent out. If it matches, act on the data.

Or, make the code unique for each slave, and put the address in its code.

Mmmmm, i dont get it? Can you type a sample code?

Guys, i found something that is bitset(), any ideas about that command, can someone explain more about it for me.

void setup(){
myAddress  = PINC & 0b00001111; // PORTC, bits 3-2-1-0 (D17 to D14 I think)
Serial.begin(9600);
}
void loop(){
if Serial.available()>1){ // at least 2 bytes received?
incomingAddress = Serial.read();
incomingData = Serial.read();
if (incomingAddress == myAddress){
  PORTB = 0xFO & PORTB ; // clear 11-8
  PORTB = PORTB | (incomingData & 0x0F);
  PORTD = 0x0F & PORTD; //clear 7-4
  PORD = PORTD | (incomingData & 0xF0);
  }

}

Can be a little smarter and perhaps look for incomingAddress to be 0xA0 to 0xA9 so you know you have a good starting byte.

bitset,(), read about it on the Reference page. Read about Direct Port Manipulation while there also.

CrossRoads: bitset,(), read about it on the Reference page. Read about Direct Port Manipulation while there also.

Yeah i have read both but, i cant understand bitset()

bitset()

x is your variable bitPosition can be 0-16 if x is type integer, 0-7 if x is type byte

bitSet(x, bitPosition) Sets (writes a 1 to) the given bitPosition of variable x bitClear(x, bitPosition) Clears (writes a 0 to) the given bitPosition of variable x bitRead(x, bitPosition) Returns the value (as 0 or 1) of the bit at the given bitPosition of variable x bitWrite(x, bitPosition, value) Sets the given value (as 0 or 1) of the bit at the given bitPosition of variable x

bitset(serialRead, 7);
makes bit 7 of variable serialRead a 1
.

LarryD:
bitset()

x is your variable
bitPosition can be 0-16 if x is type integer, 0-7 if x is type byte

bitSet(x, bitPosition) Sets (writes a 1 to) the given bitPosition of variable x
bitClear(x, bitPosition) Clears (writes a 0 to) the given bitPosition of variable x
bitRead(x, bitPosition) Returns the value (as 0 or 1) of the bit at the given bitPosition of variable x
bitWrite(x, bitPosition, value) Sets the given value (as 0 or 1) of the bit at the given bitPosition of variable x

Thanks :smiley: , but i guess this cant be used over serial right? i cannot send commands via serial right? For example i cant send " bitWrite(x,2,0)" over serial and the other arduino recives it and aplies it, or there is a way?

LarryD: bitset(serialRead, 7); makes bit 7 of variable serialRead a 1 .

Yeah,got it

CrossRoads:

void setup(){

myAddress  = PINC & 0b00001111; // PORTC, bits 3-2-1-0 (D17 to D14 I think)
Serial.begin(9600);
}
void loop(){
if Serial.available()>1){ // at least 2 bytes received?
incomingAddress = Serial.read();
incomingData = Serial.read();
if (incomingAddress == myAddress){
  PORTB = 0xFO & PORTB ; // clear 11-8
  PORTB = PORTB | (incomingData & 0x0F);
  PORTD = 0x0F & PORTD; //clear 7-4
  PORD = PORTD | (incomingData & 0xF0);
  }

}



Can be a little smarter and perhaps look for incomingAddress to be 0xA0 to 0xA9 so you know you have a good starting byte.

I dont get the first line of code , and also how will myaddress always be equal to the incoming address since every time the incoming address will change.

If I may:
Crossroads is suggesting you use A0-A3 (also called 14-17) as your address for a give Arduino board.
4 bits gives you 16 possible addresses or Arduinos.
These could be connected to a dip switch that you manipulate to the required address.
These switches should have pull-ups enabled or have external PULLUP resistors, maybe a SIP to +5v
In setup, you read the 4 bits with the PINC instruction.

if (incomingAddress == myAddress){
This makes sure the address just received is the address for this Arduino.
If it is, the second byte is processed and sent to your LEDs.

If the address is not a match, the second byte in the message is not processed.

LarryD:
If I may:
Crossroads is suggesting you use A0-A3 (also called 14-17) as your address for a give Arduino board.
4 bits gives you 16 possible addresses or Arduinos.
These could be connected to a dip switch that you manipulate to the required address.
These switches should have pull-ups enabled or have external PULLUP resistors, maybe a SIP to +5v
In setup, you read the 4 bits with the PINC instruction.

if (incomingAddress == myAddress){
This makes sure the address just received is the address for this Arduino.
If it is, the second byte is processed and sent to your LEDs.

If the address is not a match, the second byte in the message is not processed.

Ok 2 questions

  1. why use & and or “|”
  2. so for the 80 bits i want to send , i would send 20 massage right so it will be, the adress if the arduino in a line and the command which is 8 bits in another line,right?

PORTB = 0xFO & PORTB ; // clear 11-8 & is logical AND This makes D8-D11 all zeros and leaves D12 and D13 alone

PORTB = PORTB | (incomingData & 0x0F); | is logical OR Now you grab bits 0,1,2,3 from the received incomingData and put them into D8-D11

PORTD = 0x0F & PORTD; //clear 7-4 This makes D4-D7 all zeros and leaves D0-D3 alone

PORD = PORTD | (incomingData & 0xF0); Now you grab bits 4,5,6,7 from the received incomingData and put them into D4-D7

Your LEDs are connected to D4-D11

One byte for Arduino address and one byte for the LEDs for that Arduino. 10 Arduinos so 10 address bytes and 10 led bytes = 20 bytes

LarryD:
One byte for Arduino address and one byte for the LEDs for that Arduino.
10 Arduinos so 10 address bytes and 10 led bytes = 20 bytes

ok , but those are lots of bytes i need all the arduinos to apply the commands at the same time and that would not be possible if i sent 20 single bytes, i want to send the command in one packet or message and will be exactly like this:
Serial.print(00001010100011001010110101111100011101010000101010001100101011010111110001110101);