This question is following this post: arduino - how to feed a struct from a serial.read()
First I would like to thank Sol Arnu for his previous code I am showing below. Please note the code is efficient. So far the best try. Even if I don't think any bit is sent to the table. Now I can print a "1". However if I change the page sent (2 instead of 1), there is no change. This part x.b = val1 | (val2<<8); Serial.print(x.a.page);
is confusing for me. Does it compare 2 var bits and places the result in an instance of a struct by a union?
I didn't say it's an Arduino Uno (8 bits, memory and register). Can you see what's happening?
#include <SoftwareSerial.h>
#define PACKED __attribute__((__packed__))
SoftwareSerial PORTone(8, 9); // port 1
SoftwareSerial PORTtwo(10, 11); // port 2
PACKED union {
PACKED struct {
unsigned int val1:
1; // (0,1)
unsigned int val2:
4; // 10 choices (4 bits)
unsigned int val3:
3; // 5 choices (3 bits)
unsigned int val4:
2; // 3 choices (2 bits)
}
*PtrStr;
uint8_t val_table[2];
uint16_t b;
}
*MegaUnion;
//*********************************SETUP***************
void setup() {
Serial.begin(9600);
PORTone.begin(9600);
}
//**********************************LOOP***************
void loop() {
while(PORTone.available()>0) {
delay(100);
for(int i = 0; i<=4; i++) {
for(int i = 0; i<=3; i++) {
for( int j = 0; j<=3; j++) {
int inByte = PORTone.read();
*MegaUnion.struct.val1[j] = inByte; // error: 'union<anonymous>' has no member named 'val1'
}
// I should that too
// u_structTable x;
// x.b = val1 | (val2<<8);
// Serial.print(x.a.page);
}
}
// what is coming from the table
Serial.print( (*MegaUnion).page);
}
}
I should answer to Michaël Roy, (hopefully I can pass by this second thread). Thank you for your valuable answer. This seems to be another way I will test.
"EDIT" I also corrected a big mistake, nicely found by Nick Gammon, in order to give a better view of the subject. My bad.