I want to read a pot value convert it to range 0-127 then add 128 so it's range is (127-255).
I want to convert this value to binary so for example it could be: 11111111
Then 'explode it to an array' so each bit is a separate value in the array like : {1, 1, 1, 1, 1, 1, 1, 1,}
analogRead returns a number from 0 to 1023 (0 to 0b11 1111 1111).
So I guess first step is shift it right 3 bits to make it 0 to 0b0111 1111.
dataByte = analogRead(A0) >>3;
then put it in the array:
mask for LSB and store it
bitArray[0] = databyte & 0b00000001;
shift over to the the next bit into position
dataByte = dataByte >>1;
mask for LSB and store it
bitArray[1] = databyte & 0b00000001;
dataByte = dataByte >>1;
mask for LSB and store it
bitArray[2] = databyte & 0b00000001;
dataByte = dataByte >>1;
mask for LSB and store it
bitArray[3] = databyte & 0b00000001;
dataByte = dataByte >>1;
mask for LSB and store it
bitArray[4] = databyte & 0b00000001;
dataByte = dataByte >>1;
mask for LSB and store it
bitArray[5] = databyte & 0b00000001;
dataByte = dataByte >>1;
mask for LSB and store it
bitArray[6] = databyte & 0b00000001;
put that in a for:loop if you want.
void loop ()
{
byte out[8];
byte adc = (analogRead(0) >> 3) + 128;
//Byte from pot is different to last time.
if (byte previousCheck != byte adc)
{
//Encode it to an array
for (byte i = 0; i < 8; i++)
{
out[i] = adc & (1 << (7-i)) ? 1 : 0;
}
//Output byte to serial
for (byte i = 0; i < 8; i++)
{
Serial.println(out[i]);
}
//Save just-outputted value to be checked against
byte previousCheck = byte adc;
}
}
Here's what I've knocked up from your code, but I keep getting an error of "expected primary-expression before 'previousCheck'"