0

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;

Joy Biswas
  • 11
  • 1
  • The equation `(a+1)/4 - 1` makes no sense, to scale from `10` bits to `8` bits you simply have to divide by `4` or, even better, shift right by `2` bits. There is no need to check for `a < 0` if you use the correct mapping. By the way if you have doubts on simple arithmetic then you should use the [map](https://www.arduino.cc/en/Reference/Map) function from the *Arduino* library. If you want to use the `10` bit value afterwards, simply don't overwrite the variable `a` and store the `8` bit version elsewhere. – Patrick Trentin Mar 06 '17 at 09:06

1 Answers1

0

NO need calculation. You Can simply use analogWrite function.

Chayan Mistry
  • 396
  • 3
  • 14