-
1Step 1
Arduino D2-D11 to lins 1-10 on the LED bar graph. 220 ohm resistors from opposite side of graph to ground.
Fuel level sender Grn/Blk to 5V and Gry/Blk to Analog 0 with 10k to GND
Low fuel LED to Digital 12 with 220 ohm resistor
-
2Step 2
/* *LED Fuel Gauge for Honda SuperHawk 16L Tank *http://www.superhawkforum.com *Code by Will Lyon 10/3/2015. Contact: will.lyon12584@gmail.com *Help from user Doug Jefferies on the Element 14 Forums *5V to fuel sensor Grn/Blk *Fuel sensor Gry/Blk to Analog 0 with 10k resistor to GND *220 ohm resistor to each led and GND */ const int sensorPin = A0; // the pin that the potentiometer is attached to const int ledCount = 10; // the number of LEDs in the bar graph const int numReadings = 35; // use this value to determine the size of the readings array const int fuelPin = 12; // pin for factory low fuel LED int ledPins[] = { 2, 3, 4, 5, 6, 7,8,9,10,11 }; // an array of pin numbers to which LEDs are attached int readings[numReadings]; // the readings from the fuel level gauge int readIndex = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average int averagingCount = 0; // checks the number of values in the Index int timer = 75; // timer for inital LED sweep int pinCount = 10; // number of LED pins void setup() { Serial.begin(9600); pinMode(fuelPin, OUTPUT); digitalWrite(fuelPin, LOW); for (int thisLed = 0; thisLed < ledCount; thisLed++) { // loop over the pin array and set them all to output pinMode(ledPins[thisLed], OUTPUT); } for (int thisReading = 0; thisReading < numReadings; // initialize all readings to zero thisReading++) { readings[thisReading] = 0; } for (int thisPin = 0; thisPin < pinCount; thisPin++) { // loop from the lowest pin to the highest digitalWrite(ledPins[thisPin], HIGH); // turn the pin on delay(timer); // delay for time set bove digitalWrite(ledPins[thisPin], LOW); // turn the pin off } for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { // loop from the highest pin to the lowest digitalWrite(ledPins[thisPin], HIGH); // turn the pin on delay(timer); // delay for time set above digitalWrite(ledPins[thisPin], LOW); // turn the pin off } } void loop() { total = total - readings[readIndex]; // subtract the last reading readings[readIndex] = analogRead(sensorPin); // read from the sensor total = total + readings[readIndex]; // add the reading to the total readIndex = readIndex + 1; // advance to the next position in the array if (readIndex >= numReadings) { // if we're at the end of the array readIndex = 0; // wrap around to the beginning } averagingCount = averagingCount + 1; // increments the count for averaging if (averagingCount >= numReadings) { // caps the averaging count to the array averagingCount = numReadings; } average = total / averagingCount; // calculate the average delay(3000); // delay in between readings so the gauge doesn't fluctuate too fast average = map(average, 585, 940, 0, ledCount); // map the result to a range from 0 to the number of LEDs: for (int thisLed = 0; thisLed < ledCount; thisLed++) { // loop over the LED array if (thisLed == average-1) { // if the array element's index is less than ledLevel digitalWrite(ledPins[thisLed], HIGH); // turn the pin for this element on: } else { digitalWrite(ledPins[thisLed], LOW); // turn off all pins higher than the ledLevel: } } if (average <= 2) { // if either of the last two LED's on the display are lit digitalWrite(fuelPin, HIGH); // turn on the low fuel LED } else { digitalWrite(fuelPin, LOW); // turn the low fuel LED off } Serial.println(analogRead(sensorPin)); delay(100); }
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.