-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressMeter.ino
More file actions
73 lines (62 loc) · 1.33 KB
/
progressMeter.ino
File metadata and controls
73 lines (62 loc) · 1.33 KB
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
/*
progressMeter.ino
William W. Ward
wwward@pobox.com
May 28, 2012
version 0.1
E-mail me with questions for now.
When running, reads a byte from serial and sets the PWM on meterPin.
Input can be 0-99 as a byte (not ASCII) and the PWM out is from
the minVal up to the maxVal as set in the readSerial routine.
These constants should be adjusted depending on the meter's min-max voltage.
This code is licensed under GPL.
*/
int meterPin = 9;
int value = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
value = readSerial();
if (value != 255) {
setMeter(meterPin, value);
}
//Serial.println(value);
value = 255;
}
int readSerial() {
//calibration constants
int maxVal = 255;
int minVal = 0;
byte b = 255;
if (Serial.available() > 0) {
b = Serial.read();
Serial.print("Read: "); //debug
Serial.println(b); //debug
if (b > 99) {
b = 99;
}
if (b < 0 ) {
b = 0;
}
}
int multiplier = (maxVal / 100);
int setVal = 255;
if (b != 255) {
Serial.print("b is: "); //debug
Serial.println(b); //debug
setVal = (multiplier * b);
if (setVal > maxVal) {
setVal = maxVal;
}
if (setVal < minVal) {
setVal = minVal;
}
}
//Serial.println(setVal);
return setVal;
}
void setMeter(int pin, int value) {
delay(30);
analogWrite(pin, value);
}