Components and supplies
Breadboard (generic)
Rotary potentiometer (generic)
Arduino UNO
Hook Up Wire Kit, 22 AWG
Through Hole Resistor, 330 ohm
10 LEDs Bar Array, Green
Project description
Code
Demo code for map() function
arduino
This is the code used to debug the flickering 9th LED.
1/* 2 MapDemo -- A simple program to demonstrate converting the 0..1023 values from the analog to digital converter (ADC) inputs to a more useful range for human consuption of 0..10. 3 The reason for this demo is not to read any actual values, but to show a technique for getting the most even distribution of values from the map function. The technique used is 4 not entirely intuitive after reading the Arduino reference page at: https://www.arduino.cc/reference/en/language/functions/math/map/ 5 6 This program is public domain and comes with no warranty of any kind. 7*/ 8 9void setup() { 10 11 // Turn the built-in LED on to indicate program is starting. 12 pinMode(LED_BUILTIN, OUTPUT); 13 digitalWrite(LED_BUILTIN, HIGH); 14 15 // Initialize serial communication and delay to give time to start the serial console. 16 Serial.begin(9600); 17 delay(10000); 18 19 // Turn off the built-in LED to indicate startup is done. 20 digitalWrite(LED_BUILTIN, LOW); 21} 22 23void loop() { 24 25 // All these loop map 0 through 1023 onto 0 through 10, but in different ways. 26 27 // This seems logigal. The ADC from value ranges 0..1023 and the to value ranges 0..10, with 0 meaning no LEDs lit. But, there's a problem. The distribution is not even. Only one analog value maps to 10. 28 for (int x=0; x<1024; x++) { 29 int y = map(x, 0, 1023, 0, 10); 30 Serial.print("y = map(x, 0, 1023, 0, 10): x = "); 31 Serial.print(x); 32 Serial.print(", y = "); 33 Serial.println(y); 34 } 35 36 // This way is not as intuitive, as it seems to be mapping the impossible to obtain ADC value 1024 to the impossible to display value of 11. But, it works and it gives a more even distribution of values. 37 for (int x=0; x<1024; x++) { 38 int y = map(x, 0, 1024, 0, 11); 39 Serial.print("y = map(x, 0, 1024, 0, 11): x = "); 40 Serial.print(x); 41 Serial.print(", y = "); 42 Serial.println(y); 43 } 44 45 // There's also the mathematical way of doing it using slope-intercept form: y = mx + b where the slope (m) is found by the ratio of the two ranges and the y intersection is 0. 46 // See: https://en.wikipedia.org/wiki/Linear_equation#Slope-intercept_form 47 // Since the intersection with the y-axis is at 0, we can ignore it and the equation simplifies to y = mx. 48 // The value of m (slope) can be found using the concept of rise over run. The rise is the change in y value, so 0..11 in our case. The run is the change in x, or 0..1024. So m = 11 /1024. 49 for (int x=0; x<1024; x++) { 50 int y = (int) 11 * x / 1024; 51 Serial.print("y = (int) 11 * x / 1024: x = "); 52 Serial.print(x); 53 Serial.print(", y = "); 54 Serial.println(y); 55 } 56 57 // Pause and wait for any kind of input before running again. 58 Serial.println("Send any serial input to run again."); 59 while (!Serial.available()); 60 Serial.read(); 61}
Bargraph Voltmeter
arduino
Read analog input voltage and light one of ten LEDs on a bargraph, or none if the voltage is below the lowest threshold.
1/* 2 Bargraph Meter -- Light a single LED in a 10-segment bargraph to indicate input voltage at analog pin. 3 No LEDs are lit below the lowest threshold. 4 5 Code by Alex Horton 6 Public domain with no warranty or guaranty as to fitness of purpose. 7*/ 8 9int analogValue = 0; 10int level = 0; 11 12void setup() { 13 Serial.begin(9600); 14 pinMode(13,OUTPUT); 15 pinMode(12,OUTPUT); 16 pinMode(11,OUTPUT); 17 pinMode(10,OUTPUT); 18 pinMode(9,OUTPUT); 19 pinMode(8,OUTPUT); 20 pinMode(7,OUTPUT); 21 pinMode(6,OUTPUT); 22 pinMode(5,OUTPUT); 23 pinMode(4,OUTPUT); 24 pinMode(A5,INPUT); 25} 26 27void loop() { 28 analogValue = analogRead(A5); 29 level = map(analogValue, 0, 1024, 0, 11); 30 Serial.println(level); 31 Serial.println(analogValue); 32 33 switch (level) { 34 case 0: 35 digitalWrite(13,LOW); 36 digitalWrite(12,LOW); 37 digitalWrite(11,LOW); 38 digitalWrite(10,LOW); 39 digitalWrite(9,LOW); 40 digitalWrite(8,LOW); 41 digitalWrite(7,LOW); 42 digitalWrite(6,LOW); 43 digitalWrite(5,LOW); 44 digitalWrite(4,LOW); 45 break; 46 case 1: 47 digitalWrite(13,LOW); 48 digitalWrite(12,LOW); 49 digitalWrite(11,LOW); 50 digitalWrite(10,LOW); 51 digitalWrite(9,LOW); 52 digitalWrite(8,LOW); 53 digitalWrite(7,LOW); 54 digitalWrite(6,LOW); 55 digitalWrite(5,LOW); 56 digitalWrite(4,HIGH); 57 break; 58 case 2: 59 digitalWrite(13,LOW); 60 digitalWrite(12,LOW); 61 digitalWrite(11,LOW); 62 digitalWrite(10,LOW); 63 digitalWrite(9,LOW); 64 digitalWrite(8,LOW); 65 digitalWrite(7,LOW); 66 digitalWrite(6,LOW); 67 digitalWrite(5,HIGH); 68 digitalWrite(4,LOW); 69 break; 70 case 3: 71 digitalWrite(13,LOW); 72 digitalWrite(12,LOW); 73 digitalWrite(11,LOW); 74 digitalWrite(10,LOW); 75 digitalWrite(9,LOW); 76 digitalWrite(8,LOW); 77 digitalWrite(7,LOW); 78 digitalWrite(6,HIGH); 79 digitalWrite(5,LOW); 80 digitalWrite(4,LOW); 81 break; 82 case 4: 83 digitalWrite(13,LOW); 84 digitalWrite(12,LOW); 85 digitalWrite(11,LOW); 86 digitalWrite(10,LOW); 87 digitalWrite(9,LOW); 88 digitalWrite(8,LOW); 89 digitalWrite(7,HIGH); 90 digitalWrite(6,LOW); 91 digitalWrite(5,LOW); 92 digitalWrite(4,LOW); 93 break; 94 case 5: 95 digitalWrite(13,LOW); 96 digitalWrite(12,LOW); 97 digitalWrite(11,LOW); 98 digitalWrite(10,LOW); 99 digitalWrite(9,LOW); 100 digitalWrite(8,HIGH); 101 digitalWrite(7,LOW); 102 digitalWrite(6,LOW); 103 digitalWrite(5,LOW); 104 digitalWrite(4,LOW); 105 break; 106 case 6: 107 digitalWrite(13,LOW); 108 digitalWrite(12,LOW); 109 digitalWrite(11,LOW); 110 digitalWrite(10,LOW); 111 digitalWrite(9,HIGH); 112 digitalWrite(8,LOW); 113 digitalWrite(7,LOW); 114 digitalWrite(6,LOW); 115 digitalWrite(5,LOW); 116 digitalWrite(4,LOW); 117 break; 118 case 7: 119 digitalWrite(13,LOW); 120 digitalWrite(12,LOW); 121 digitalWrite(11,LOW); 122 digitalWrite(10,HIGH); 123 digitalWrite(9,LOW); 124 digitalWrite(8,LOW); 125 digitalWrite(7,LOW); 126 digitalWrite(6,LOW); 127 digitalWrite(5,LOW); 128 digitalWrite(4,LOW); 129 break; 130 case 8: 131 digitalWrite(13,LOW); 132 digitalWrite(12,LOW); 133 digitalWrite(11,HIGH); 134 digitalWrite(10,LOW); 135 digitalWrite(9,LOW); 136 digitalWrite(8,LOW); 137 digitalWrite(7,LOW); 138 digitalWrite(6,LOW); 139 digitalWrite(5,LOW); 140 digitalWrite(4,LOW); 141 break; 142 case 9: 143 digitalWrite(13,LOW); 144 digitalWrite(12,HIGH); 145 digitalWrite(11,LOW); 146 digitalWrite(10,LOW); 147 digitalWrite(9,LOW); 148 digitalWrite(8,LOW); 149 digitalWrite(7,LOW); 150 digitalWrite(6,LOW); 151 digitalWrite(5,LOW); 152 digitalWrite(4,LOW); 153 break; 154 case 10: 155 digitalWrite(13,HIGH); 156 digitalWrite(12,LOW); 157 digitalWrite(11,LOW); 158 digitalWrite(10,LOW); 159 digitalWrite(9,LOW); 160 digitalWrite(8,LOW); 161 digitalWrite(7,LOW); 162 digitalWrite(6,LOW); 163 digitalWrite(5,LOW); 164 digitalWrite(4,LOW); 165 break; 166 } 167}
Bargraph Voltmeter
arduino
Read analog input voltage and light one of ten LEDs on a bargraph, or none if the voltage is below the lowest threshold.
1/* 2 Bargraph Meter -- Light a single LED in a 10-segment bargraph to indicate input voltage at analog pin. 3 No LEDs are lit below the lowest threshold. 4 5 Code by Alex Horton 6 Public domain with no warranty or guaranty as to fitness of purpose. 7*/ 8 9int analogValue = 0; 10int level = 0; 11 12void setup() { 13 Serial.begin(9600); 14 pinMode(13,OUTPUT); 15 pinMode(12,OUTPUT); 16 pinMode(11,OUTPUT); 17 pinMode(10,OUTPUT); 18 pinMode(9,OUTPUT); 19 pinMode(8,OUTPUT); 20 pinMode(7,OUTPUT); 21 pinMode(6,OUTPUT); 22 pinMode(5,OUTPUT); 23 pinMode(4,OUTPUT); 24 pinMode(A5,INPUT); 25} 26 27void loop() { 28 analogValue = analogRead(A5); 29 level = map(analogValue, 0, 1024, 0, 11); 30 Serial.println(level); 31 Serial.println(analogValue); 32 33 switch (level) { 34 case 0: 35 digitalWrite(13,LOW); 36 digitalWrite(12,LOW); 37 digitalWrite(11,LOW); 38 digitalWrite(10,LOW); 39 digitalWrite(9,LOW); 40 digitalWrite(8,LOW); 41 digitalWrite(7,LOW); 42 digitalWrite(6,LOW); 43 digitalWrite(5,LOW); 44 digitalWrite(4,LOW); 45 break; 46 case 1: 47 digitalWrite(13,LOW); 48 digitalWrite(12,LOW); 49 digitalWrite(11,LOW); 50 digitalWrite(10,LOW); 51 digitalWrite(9,LOW); 52 digitalWrite(8,LOW); 53 digitalWrite(7,LOW); 54 digitalWrite(6,LOW); 55 digitalWrite(5,LOW); 56 digitalWrite(4,HIGH); 57 break; 58 case 2: 59 digitalWrite(13,LOW); 60 digitalWrite(12,LOW); 61 digitalWrite(11,LOW); 62 digitalWrite(10,LOW); 63 digitalWrite(9,LOW); 64 digitalWrite(8,LOW); 65 digitalWrite(7,LOW); 66 digitalWrite(6,LOW); 67 digitalWrite(5,HIGH); 68 digitalWrite(4,LOW); 69 break; 70 case 3: 71 digitalWrite(13,LOW); 72 digitalWrite(12,LOW); 73 digitalWrite(11,LOW); 74 digitalWrite(10,LOW); 75 digitalWrite(9,LOW); 76 digitalWrite(8,LOW); 77 digitalWrite(7,LOW); 78 digitalWrite(6,HIGH); 79 digitalWrite(5,LOW); 80 digitalWrite(4,LOW); 81 break; 82 case 4: 83 digitalWrite(13,LOW); 84 digitalWrite(12,LOW); 85 digitalWrite(11,LOW); 86 digitalWrite(10,LOW); 87 digitalWrite(9,LOW); 88 digitalWrite(8,LOW); 89 digitalWrite(7,HIGH); 90 digitalWrite(6,LOW); 91 digitalWrite(5,LOW); 92 digitalWrite(4,LOW); 93 break; 94 case 5: 95 digitalWrite(13,LOW); 96 digitalWrite(12,LOW); 97 digitalWrite(11,LOW); 98 digitalWrite(10,LOW); 99 digitalWrite(9,LOW); 100 digitalWrite(8,HIGH); 101 digitalWrite(7,LOW); 102 digitalWrite(6,LOW); 103 digitalWrite(5,LOW); 104 digitalWrite(4,LOW); 105 break; 106 case 6: 107 digitalWrite(13,LOW); 108 digitalWrite(12,LOW); 109 digitalWrite(11,LOW); 110 digitalWrite(10,LOW); 111 digitalWrite(9,HIGH); 112 digitalWrite(8,LOW); 113 digitalWrite(7,LOW); 114 digitalWrite(6,LOW); 115 digitalWrite(5,LOW); 116 digitalWrite(4,LOW); 117 break; 118 case 7: 119 digitalWrite(13,LOW); 120 digitalWrite(12,LOW); 121 digitalWrite(11,LOW); 122 digitalWrite(10,HIGH); 123 digitalWrite(9,LOW); 124 digitalWrite(8,LOW); 125 digitalWrite(7,LOW); 126 digitalWrite(6,LOW); 127 digitalWrite(5,LOW); 128 digitalWrite(4,LOW); 129 break; 130 case 8: 131 digitalWrite(13,LOW); 132 digitalWrite(12,LOW); 133 digitalWrite(11,HIGH); 134 digitalWrite(10,LOW); 135 digitalWrite(9,LOW); 136 digitalWrite(8,LOW); 137 digitalWrite(7,LOW); 138 digitalWrite(6,LOW); 139 digitalWrite(5,LOW); 140 digitalWrite(4,LOW); 141 break; 142 case 9: 143 digitalWrite(13,LOW); 144 digitalWrite(12,HIGH); 145 digitalWrite(11,LOW); 146 digitalWrite(10,LOW); 147 digitalWrite(9,LOW); 148 digitalWrite(8,LOW); 149 digitalWrite(7,LOW); 150 digitalWrite(6,LOW); 151 digitalWrite(5,LOW); 152 digitalWrite(4,LOW); 153 break; 154 case 10: 155 digitalWrite(13,HIGH); 156 digitalWrite(12,LOW); 157 digitalWrite(11,LOW); 158 digitalWrite(10,LOW); 159 digitalWrite(9,LOW); 160 digitalWrite(8,LOW); 161 digitalWrite(7,LOW); 162 digitalWrite(6,LOW); 163 digitalWrite(5,LOW); 164 digitalWrite(4,LOW); 165 break; 166 } 167}
Demo code for map() function
arduino
This is the code used to debug the flickering 9th LED.
1/* 2 MapDemo -- A simple program to demonstrate converting the 0..1023 3 values from the analog to digital converter (ADC) inputs to a more useful range 4 for human consuption of 0..10. 5 The reason for this demo is not to read any 6 actual values, but to show a technique for getting the most even distribution of 7 values from the map function. The technique used is 8 not entirely intuitive 9 after reading the Arduino reference page at: https://www.arduino.cc/reference/en/language/functions/math/map/ 10 11 12 This program is public domain and comes with no warranty of any kind. 13*/ 14 15void 16 setup() { 17 18 // Turn the built-in LED on to indicate program is starting. 19 20 pinMode(LED_BUILTIN, OUTPUT); 21 digitalWrite(LED_BUILTIN, HIGH); 22 23 // 24 Initialize serial communication and delay to give time to start the serial console. 25 26 Serial.begin(9600); 27 delay(10000); 28 29 // Turn off the built-in LED to 30 indicate startup is done. 31 digitalWrite(LED_BUILTIN, LOW); 32} 33 34void 35 loop() { 36 37 // All these loop map 0 through 1023 onto 0 through 10, but in 38 different ways. 39 40 // This seems logigal. The ADC from value ranges 0..1023 41 and the to value ranges 0..10, with 0 meaning no LEDs lit. But, there's a problem. 42 The distribution is not even. Only one analog value maps to 10. 43 for (int x=0; 44 x<1024; x++) { 45 int y = map(x, 0, 1023, 0, 10); 46 Serial.print("y = 47 map(x, 0, 1023, 0, 10): x = "); 48 Serial.print(x); 49 Serial.print(", 50 y = "); 51 Serial.println(y); 52 } 53 54 // This way is not as intuitive, 55 as it seems to be mapping the impossible to obtain ADC value 1024 to the impossible 56 to display value of 11. But, it works and it gives a more even distribution of values. 57 58 for (int x=0; x<1024; x++) { 59 int y = map(x, 0, 1024, 0, 11); 60 Serial.print("y 61 = map(x, 0, 1024, 0, 11): x = "); 62 Serial.print(x); 63 Serial.print(", 64 y = "); 65 Serial.println(y); 66 } 67 68 // There's also the mathematical 69 way of doing it using slope-intercept form: y = mx + b where the slope (m) is found 70 by the ratio of the two ranges and the y intersection is 0. 71 // See: https://en.wikipedia.org/wiki/Linear_equation#Slope-intercept_form 72 73 // Since the intersection with the y-axis is at 0, we can ignore it and the equation 74 simplifies to y = mx. 75 // The value of m (slope) can be found using the concept 76 of rise over run. The rise is the change in y value, so 0..11 in our case. The run 77 is the change in x, or 0..1024. So m = 11 /1024. 78 for (int x=0; x<1024; x++) 79 { 80 int y = (int) 11 * x / 1024; 81 Serial.print("y = (int) 11 * x / 1024: 82 x = "); 83 Serial.print(x); 84 Serial.print(", y = "); 85 Serial.println(y); 86 87 } 88 89 // Pause and wait for any kind of input before running again. 90 Serial.println("Send 91 any serial input to run again."); 92 while (!Serial.available()); 93 Serial.read(); 94}
Downloadable files
Bargraph Voltmeter (schematic)
Screenshot image of KiCAD schematic.
Bargraph Voltmeter (schematic)
Bargraph Voltmeter (KiCAD schematic)
Indicate potentiometer voltage on LED bargraph display.
Bargraph Voltmeter (KiCAD schematic)
Bargraph Voltmeter (KiCAD schematic)
Indicate potentiometer voltage on LED bargraph display.
Bargraph Voltmeter (KiCAD schematic)
Comments
Only logged in users can leave comments
dhorton668
0 Followers
•0 Projects
1
0