-
Notifications
You must be signed in to change notification settings - Fork 0
/
Read_Voltage.c
55 lines (46 loc) · 1.48 KB
/
Read_Voltage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <Servo.h>
Servo myservo;
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
double voltage = 0;
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
void setup() {
Serial.begin(9600);
myservo.attach(9);
myservo.write(0);
pinMode(ledPin, OUTPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
// pinMode(pin7,INPUT);
}
void loop() {
total = total - readings[readIndex];
readings[readIndex] = analogRead(inputPin);
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
delay(1);
sensorValue = analogRead(sensorPin);
delay(sensorValue);
voltage = sensorValue * 5.0/1023;
delay(sensorValue);
//Offset calibration for G_servo
if (voltage>0 && voltage<.75)
voltage = voltage + .12;
else if(voltage>.75 && voltage<1.50)
voltage = voltage + .11;
else if(voltage>1.50 && voltage<2.00)
voltage = voltage + .10;
Serial.println(voltage);
delay(500);
}