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;

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 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. Commented Mar 6, 2017 at 9:06

1 Answer 1

0

NO need calculation. You Can simply use analogWrite function.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.