Hardware Reference
In-Depth Information
Sketch
Time to write the sketch, as shown in Listing 14-1.
Listing 14-1: Sketch (fi lename: Chapter14.ino )
1 #include <Servo.h>
2
3 float tempC;
4 int angleC;
5 int reading;
6 int tempPin = A0;
7 int servoPin = 9;
8
9 Servo thServo;
10
11 void setup()
12 {
13 analogReference(INTERNAL);
14 Serial.begin(9600);
15 thServo.attach(servoPin);
16 thServo.write(90);
17 delay(1000);
18 }
19
20 void loop()
21 {
22 reading = analogRead(tempPin);
23 tempC = reading / 9.31;
24 angleC = map(tempC, 0, 50, 135, 45);
25 Serial.print(tempC);
26 Serial.print(" Celsius, ");
27 Serial.print(angleC);
28 Serial.println(" degrees");
29 thServo.write(angleC);
30 delay(500);
31 }
The work starts right from line 1. On the i rst line of the sketch, the Servo
library is imported. On lines 3 to 7, variables are dei ned. The temperature is
dei ned as a l oating-point number, and all other variables are dei ned as integers.
On line 9, a Servo object is created, called thServo , short for thermometer
Servo. This is the instance on which instructions will be called.
On line 11, the setup function is created. In this function, three things will
be done. First, the reference voltage is set to INTERNAL , meaning the analog-to-
digital converter will compare against a 1.1 V reference, not 5 volts as it would
normally. This works for all analog inputs, and therefore, no pin is specii ed.
 
Search WWH ::




Custom Search