-
Notifications
You must be signed in to change notification settings - Fork 0
/
average-voltage-calc.ino
76 lines (54 loc) · 1.86 KB
/
average-voltage-calc.ino
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Average analog pin voltage calculator for Arduino Uno.
This sketch calculates the average voltage on analog pins of an Arduino Uno.
As soon as the serial monitor starts, the arduino keeps taking readings from the analog pins.
It calculates the average voltage and reports the data to the serial monitor every second.
The accuracy of the average voltage data increases as the arduino receives more readings in each loop.
Within about 20 loops, the data becomes as much as 95% accurate.
Component: Potentiometer/voltage source as input in analog pins.
Created: 12 June, 2018
By Muhammad Arifur Rahman
*/
int loopNum; //declaring average voltage calculation variables
double avgVol,volSum;
void setup() {
for(int apin=A0;apin<=A5;apin++) //there are six(6) analog pins in arduino uno
pinMode(apin,INPUT); //initializing to read voltage from analog pins
loopNum=1;
avgVol=volSum=0;
Serial.begin(9600); //define the serial data transmission rate
if(Serial) //check if serial monitor is ready
Serial.println("Arduino is live!");
}
void loop() {
Serial.print("Reading #");
Serial.println(loopNum);
Serial.print("Current average voltage: ");
Serial.print(avgVol);
Serial.println("V");
for(int apin=A0;apin<=A5;apin++)
{
double apinVol=analogRead(apin);
apinVol*=5; apinVol/=1023; //conversion of the voltage to the standard
Serial.print("Pin #A");
Serial.print(apin-A0);
Serial.print(": Voltage= ");
Serial.print(apinVol);
Serial.print("V | ");
if(fabs(apinVol-avgVol)>0.01) //check further if the voltage difference is more than 10mV
{
Serial.print(fabs((apinVol-avgVol))*1000);
if(apinVol>avgVol)
Serial.println("mV more");
else if(apinVol<avgVol)
Serial.println("mV less");
}
else
Serial.println("Perfect voltage!");
volSum+=apinVol;
}
avgVol=volSum/(6.0*(double)loopNum);
loopNum++;
Serial.println("");
delay(1000); //reading interval, change it for faster/slower data reading
}