source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/03.Analog/AnalogInOutSerial/AnalogInOutSerial.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 1.6 KB
Line 
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
5 and uses the result to set the pulsewidth 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
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 */
21
22// These constants won't change.  They're used to give names
23// to the pins used:
24const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
25const int analogOutPin = 9; // Analog output pin that the LED is attached to
26
27int sensorValue = 0;        // value read from the pot
28int outputValue = 0;        // value output to the PWM (analog out)
29
30void setup() {
31  // initialize serial communications at 9600 bps:
32  Serial.begin(9600);
33}
34
35void 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
50  // for the analog-to-digital converter to settle
51  // after the last reading:
52  delay(2);
53}
Note: See TracBrowser for help on using the repository browser.