I have a project with arduino UNO . first i write this code:
int a;
void setup(){
for (byte i=0;i<8;i++){
pinMode(i,OUTPUT);
}
}
void loop(){
a = analogRead(A0);//read voltage at A0
a = (a+1)/4 - 1;//scale from 10 bit (0-1023) to 8 bit (0-255)
if (a<0)
{
a = 0;
}
PORTD = a;
}
it works well but there is a problem . i convert the a value 10 bit to 8 bit . but now i want to use the 10 bit value for a. now how can i repalce the pin number Instead PORTD = a;
(a+1)/4 - 1makes no sense, to scale from10bits to8bits you simply have to divide by4or, even better, shift right by2bits. There is no need to check fora < 0if you use the correct mapping. By the way if you have doubts on simple arithmetic then you should use the map function from the Arduino library. If you want to use the10bit value afterwards, simply don't overwrite the variableaand store the8bit version elsewhere.