Hello everyone , i am new in this forum and have two litte question , first of all i am reading "ARDUINO APLICACIONES EN ROBOTICA, MECATRONICA E INGENIERIAS." is a good book for begginer like me , so the book give one part of the code that is the union and said that i have to show the variables in the display serial and analyst the results , this is the code that i do:
void setup() {
Serial.begin(9600);
}
union {
int i;
char c;
long u;
float x;
word w;
}comando;
void loop() {
comando.x=M_PI;// asignacion de una costante de tipo flotante (numero pi)
//bitSet(comando.x,16); // cambiar el bit 16 de x
Serial.print(F("Variable tipo char: "));
Serial.println(comando.c);
Serial.print(F("Variable tipo int: "));
Serial.println(comando.i);
Serial.print(F("Variable tipo long: "));
Serial.println(comando.u);
Serial.print(F("Variable tipo float: "));
Serial.println(comando.x);
Serial.print(F("Variable tipo word : "));
Serial.println(comando.w);
Serial.print(F("Se repitira en 10 segundos"));
delay(10000);
}
In screen show :
Variable tipo char: ⸮
Variable tipo int: 4059
Variable tipo long: 1078530011
Variable tipo float: 3.14
Variable tipo word : 4059
Se repitira en 10 segundos
So i dont understand the results because i give x the value of the number pi so what i understand is that union make that all the variables modify with this value so why i is not 3? that is the whole part of 3.1416, the same for w, u i dont understand why is so big and in w and i is so small, c i believe that because it is a fraction and only number int can pass to letters or other numbres , if someone can explain me this i will be very gratefull.
The second question is why can not configure the bits of x? i try to change the bit 16 of x but this said that in arduino one cant do this, but i read that arduino one can use bitSet so i dont undestand why this dont let me use bitSet, i put this part like comment to make tests , thank you for everything that you will say me and if i found how to solve this questions i will post here the solution for people that in the future need this.
Sorry for my english, i am good to read but bad to write, in the future i will try to make my english more smooth
CambioDeTipoPro.ino (655 Bytes)