I‘ll use the led for my export device. So I made a board for five LED, One red, two oranges and two green. Then I use the laser cutting machine to cut a part and put it on the board. I'm going to put it on my final project as a small monitor.
Circuit board
LED board
programming
At first, I put the distance sensor in the magazine, then tested the sensor's reading, the value is 490-460. When the object nearest by the sensor, the value is 460. when the object is farthest from the sensor, the value is 490.
Because I want to use 5 LED to represent the remaining bullets. I divide the value into 5 groups: val<= 460, val> 460, val> 470, val> 480, val> 490. Then, set the LED' working condition for each group.
if(val <= 460)
digitalWrite(4,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
f( val > 460 && val <=470)
digitalWrite(4,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
digitalWrite(8,LOW);
if( val > 470 && val <=480)
digitalWrite(4,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
if( val > 480 && val <=490)
digitalWrite(4,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
if( val > 490)
digitalWrite(4,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
So by this program the number of the led will reflect the left iron balls, full magazine with five LEDs and if the magazine is empty, there is only one red LED left.