]> git.piffa.net Git - sketchbook_andrea/blob - basic/pwm/pwm_4_analog_input/pwm_4_analog_input.ino
analog cleanup
[sketchbook_andrea] / basic / pwm / pwm_4_analog_input / pwm_4_analog_input.ino
1 /*
2   Analog PWM
3   
4   Impostare la frequenza del PWM tramite un input analogico.
5
6 - Schema: http://lab.piffa.net/schemi/arduino-pwm-diagram.png
7   
8   */
9   
10 const byte inputPin = A0;// set input pin for the potentiometer
11 int inputValue = 0;      // potentiometer input variable
12 const byte ledPin = 3;   // output pin, deve avere il PWM
13
14 void setup() {
15      // declare the ledPin as an OUTPUT:
16      pinMode(ledPin, OUTPUT);
17 }
18
19 void loop() {
20      // read the value from the potentiometer:
21      inputValue = analogRead(inputPin);
22
23      // send the square wave signal to the LED:
24      analogWrite(ledPin, inputValue/4); 
25      // la lettura analogica e' a 10 bit (0-1024)
26      // Il PWM invece e' a 8 bit (0-255)
27      // Circa 1024 / 4 ~= 255
28      
29      // Domanda: dovrebbe esserci un delay()?
30 }
31