Debian Codegraph @

examples/03.Analog/AnalogInOutSerial/AnalogInOutSerial.ino

raw
1 /*
2 Analog input, analog output, serial output
3
4 Reads an analog input pin, maps the result to a range from 0 to 255 and uses
5 the result to set the pulse width modulation (PWM) of an output pin.
6 Also prints the results to the Serial Monitor.
7
8 The circuit:
9 - potentiometer connected to analog pin 0.
10 Center pin of the potentiometer goes to the analog pin.
11 side pins of the potentiometer go to +5V and ground
12 - LED connected from digital pin 9 to ground through 220 ohm resistor
13
14 created 29 Dec. 2008
15 modified 9 Apr 2012
16 by Tom Igoe
17
18 This example code is in the public domain.
19
20 https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInOutSerial
21 */
22
23 // These constants won't change. They're used to give names to the pins used:
24 const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
25 const int analogOutPin = 9; // Analog output pin that the LED is attached to
26
27 int sensorValue = 0; // value read from the pot
28 int outputValue = 0; // value output to the PWM (analog out)
29
30 void setup() {
31 // initialize serial communications at 9600 bps:
32 Serial.begin(9600);
33 }
34
35 void loop() {
36 // read the analog in value:
37 sensorValue = analogRead(analogInPin);
38 // map it to the range of the analog out:
39 outputValue = map(sensorValue, 0, 1023, 0, 255);
40 // change the analog out value:
41 analogWrite(analogOutPin, outputValue);
42
43 // print the results to the Serial Monitor:
44 Serial.print("sensor = ");
45 Serial.print(sensorValue);
46 Serial.print("\t output = ");
47 Serial.println(outputValue);
48
49 // wait 2 milliseconds before the next loop for the analog-to-digital
50 // converter to settle after the last reading:
51 delay(2);
52 }