user555: I am using the Arduino IDE to program the attiny85. I want to take an incoming analog reading, then based on that reading, output a specific PWM value. Here's my circuit:

and here's my code:
// to run on attiny85

const byte pwmPin = 0;
const byte analogInPin = A2;

void setup() {
  pinMode(pwmPin, OUTPUT);
}

void loop() {
  int analogIn = analogRead(analogInPin);
  analogWrite(pwmPin, analogIn);
}

should be very simple- I have no problems uploading code to the attiny85, and no problems with simple tests like outputting a specific PWM value (not based on the analog read). but when I try to combine the two- read, then write that value, I can't seem to get things to work. In this circuit for example- I get a reading of 1023 (5v) on the arduino micro- instead of a reading of ~ 790 (3.85v) which is what I should expect. I've used a multimeter to verify the voltages in this circuit- so I think I must either be doing something wrong with my expectations of how to wire up or program the attiny85.

user555: i needed to re-range the 10bit input to 8 bit for the pwm out.
// to run on attiny85

const byte pwmPin = 0;
const byte analogInPin = A2;

void setup() {
}

void loop() {
  pinMode(pwmPin, OUTPUT);
  int analogIn = analogRead(analogInPin);
 analogIn = map(analogIn, 0, 1023, 0, 255);
 analogWrite(pwmPin, analogIn);
}


user136: Analog read is 10-bits (2^10 = 0-1023 range), analog write is 8-bits (2^8 = 0-255 range). Ditch the lower two bits of the result either by doing: 
analogIn = analogIn >> 2;

which can be shortened to:
analogIn >>= 2;

Or you can use the rather complex map function:
analogIn = map( analogIn(0, 1023, 0, 255) )


555: <p>I am using the Arduino IDE to program the attiny85. I want to take an incoming analog reading, then based on that reading, output a specific PWM value. Here's my circuit:</p>

<p><img src="https://i.stack.imgur.com/q83mB.jpg" alt="my circuit"></p>

<p>and here's my code:</p>

<pre><code>// to run on attiny85

const byte pwmPin = 0;
const byte analogInPin = A2;

void setup() {
  pinMode(pwmPin, OUTPUT);
}

void loop() {
  int analogIn = analogRead(analogInPin);
  analogWrite(pwmPin, analogIn);
}
</code></pre>

<p>should be very simple- I have no problems uploading code to the attiny85, and no problems with simple tests like outputting a specific PWM value (not based on the analog read). but when I try to combine the two- read, then write that value, I can't seem to get things to work. In this circuit for example- I get a reading of 1023 (5v) on the arduino micro- instead of a reading of ~ 790 (3.85v) which is what I should expect. I've used a multimeter to verify the voltages in this circuit- so I think I must either be doing something wrong with my expectations of how to wire up or program the attiny85.</p>
 555: <p>i needed to re-range the 10bit input to 8 bit for the pwm out.</p>

<pre><code>// to run on attiny85

const byte pwmPin = 0;
const byte analogInPin = A2;

void setup() {
}

void loop() {
  pinMode(pwmPin, OUTPUT);
  int analogIn = analogRead(analogInPin);
 analogIn = map(analogIn, 0, 1023, 0, 255);
 analogWrite(pwmPin, analogIn);
}
</code></pre>
 136: <p>Analog read is 10-bits (2^10 = 0-1023 range), analog write is 8-bits (2^8 = 0-255 range). Ditch the lower two bits of the result either by doing: </p>

<pre><code>analogIn = analogIn &gt;&gt; 2;
</code></pre>

<p>which can be shortened to:</p>

<pre><code>analogIn &gt;&gt;= 2;
</code></pre>

<p>Or you can use the rather complex map function:</p>

<pre><code>analogIn = map( analogIn(0, 1023, 0, 255) )
</code></pre>
