Hello,
I have a problem with bitRead.
If I run bitRead passing in the serial monitor the letter 'A', so that the function becomes bitRead ('A', 0), rightly I get a value of 1, because the ASCII value of 'A' is 1000001 and the first position is 1.
Everything is ok.
The problems arise when I try to run bitRead after all other functions.
In my code, bitRead always returns zero and I do not understand why.
It seems that the variable 'Valbit' is in some way affected by the above functions, and the end result is always 0.
The problem does not occur when I run bitRead alone without the other functions above.
Why is it so?
Thank you all for the help.
char Valbit;
void setup()
{
Serial.begin(9600);}
void loop()
{
if (Serial.available()>0){Valbit=Serial.read();
Serial.print("VALUE= ");
Serial.print(Valbit);Serial.print(" DECIMAL VAL= ");
Serial.print(Valbit,DEC);Serial.print(" BINARY VAL= ");
Serial.println(Valbit,BIN);//bitSet(Valbit, 1); Sets (writes a 1 to) the given bitPositionof variable x
byte bitset=bitSet(Valbit, 0);Serial.print("bitSet= ");
Serial.write(bitset);Serial.print(" DECIMAL bitSet= ");
Serial.print(bitset,DEC);Serial.print(" BINARY bitSet= ");
Serial.println(bitset,BIN);//bitClear(x, bitPosition); Clears (writes a 0 to) the given bitPosition of variable x
byte bitclear=bitClear(Valbit, 0);Serial.print("bitClear= ");
Serial.write(bitclear);Serial.print(" DECIMAL bitClear= ");
Serial.print(bitclear,DEC);Serial.print(" BINARY bitClear= ");
Serial.println(bitclear,BIN);//bitWrite(x, bitPosition, value) Sets the given value (as 0 or 1) of the bit at the given bitPositionof variable x
byte bitwrite=bitWrite(Valbit, 4,0);
Serial.print("bitWrite= ");
Serial.write(bitwrite);Serial.print(" DECIMAL bitwrite= ");
Serial.print(bitwrite,DEC);Serial.print(" BINARY bitwrite= ");
Serial.println(bitwrite,BIN);//bitRead(x, bitPosition); Returns the value (as 0 or 1) of the bit at the given bitPositionof variable x
int bitread=bitRead(Valbit, 0);
Serial.print("bitRead= ");
Serial.println(bitread);
}}