ajlelectronics
Member
- Messages
- 10,842
- Location
- Gloucester, England
I can't get my brain to work!
I have a number of approximate numerical values coming in from a sensor. I want to map those to a definite PWM output value. This is for a fuel level converter to match a sender to an incompatible gauge. The gauge is non linear and sender has a ladder of switches and resistors. The figures I am getting on test are as per the table at the bottom.
The sensor value can flicker +- 3 so a simple array won't work unless the values are smoothed first. If I am going to smooth them I might just as well use IF, THEN, ELSE to map the values. Of course we also have the problem of the sender being short of one switch!
The program I am using is one that used to do LPG / fuel switching in my Scimitar, written many years ago. Ignore the "LPG" section, it is disabled by taking D10 high.
/*
LPG_PetrolGauge 24/08/2015
Fuel gauge conversion
The circuit:
Tank sender connected to analogue pin 0 and ground.
330R 1/2W resistor connected from A0 to +5V regulator.
Value varies from 0R full, to 190R empty.
PWM D9 connected via 3K3 resistor to the base of a TIP120.
Emitter grounded, Collector to fuel gauge. Fuel gauge to 12V vehicle power.
created 18/08/15
By Classic Microcars
.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analogue input pin that the petrol sender is attached to
const int analogInPin1 = A1; // Analogue input pin that the LPG sender is attached to
const int analogOutPin = 9; // Analogue output pin that the fuel gauge is attached to
const int alarmLedPin = 13; // Low fuel warning light output.
const int petrolEnablePin = 10; // Input from fuel pump (petrol)
int sensorValue = 0; // value read from the sender
int outputValue = 0; // value output to the PWM (analog out)
int outputValue1 = 0; // value output
int val = 0; // Tank switch value
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(alarmLedPin, OUTPUT);
pinMode(petrolEnablePin, INPUT);
}
void loop() {
val = digitalRead(petrolEnablePin);
if (val == LOW) {
lpgSender();
}
else {
petrolSender();
}
}
void petrolSender(){
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 370, 0, 108, 1); // Calibration to the actual petrol sender
// Alarm threshold SensorValue of 45 to put the LED on
if (sensorValue < 45) {
digitalWrite(alarmLedPin, HIGH);
}
else
{digitalWrite(alarmLedPin, LOW);
}
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
void lpgSender(){
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 1023, 0, 1, 108); // Calibration to the actual LPG sender
// Alarm threshold SensorValue of 226 to put the LED on
if (sensorValue > 225) {
digitalWrite(alarmLedPin, HIGH);
}
else
{digitalWrite(alarmLedPin, LOW);
}
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
I have a number of approximate numerical values coming in from a sensor. I want to map those to a definite PWM output value. This is for a fuel level converter to match a sender to an incompatible gauge. The gauge is non linear and sender has a ladder of switches and resistors. The figures I am getting on test are as per the table at the bottom.
The sensor value can flicker +- 3 so a simple array won't work unless the values are smoothed first. If I am going to smooth them I might just as well use IF, THEN, ELSE to map the values. Of course we also have the problem of the sender being short of one switch!
The program I am using is one that used to do LPG / fuel switching in my Scimitar, written many years ago. Ignore the "LPG" section, it is disabled by taking D10 high.
/*
LPG_PetrolGauge 24/08/2015
Fuel gauge conversion
The circuit:
Tank sender connected to analogue pin 0 and ground.
330R 1/2W resistor connected from A0 to +5V regulator.
Value varies from 0R full, to 190R empty.
PWM D9 connected via 3K3 resistor to the base of a TIP120.
Emitter grounded, Collector to fuel gauge. Fuel gauge to 12V vehicle power.
created 18/08/15
By Classic Microcars
.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analogue input pin that the petrol sender is attached to
const int analogInPin1 = A1; // Analogue input pin that the LPG sender is attached to
const int analogOutPin = 9; // Analogue output pin that the fuel gauge is attached to
const int alarmLedPin = 13; // Low fuel warning light output.
const int petrolEnablePin = 10; // Input from fuel pump (petrol)
int sensorValue = 0; // value read from the sender
int outputValue = 0; // value output to the PWM (analog out)
int outputValue1 = 0; // value output
int val = 0; // Tank switch value
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(alarmLedPin, OUTPUT);
pinMode(petrolEnablePin, INPUT);
}
void loop() {
val = digitalRead(petrolEnablePin);
if (val == LOW) {
lpgSender();
}
else {
petrolSender();
}
}
void petrolSender(){
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 370, 0, 108, 1); // Calibration to the actual petrol sender
// Alarm threshold SensorValue of 45 to put the LED on
if (sensorValue < 45) {
digitalWrite(alarmLedPin, HIGH);
}
else
{digitalWrite(alarmLedPin, LOW);
}
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
void lpgSender(){
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 1023, 0, 1, 108); // Calibration to the actual LPG sender
// Alarm threshold SensorValue of 226 to put the LED on
if (sensorValue > 225) {
digitalWrite(alarmLedPin, HIGH);
}
else
{digitalWrite(alarmLedPin, LOW);
}
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
Fuel level | PWM output for correct display | Sensor input | |
Empty | 1 | 1 | |
1/8 | 13 | 128 | |
1/4 | 18 | 170 | |
3/8 | 22 | 210 | |
1/2 | 27 | 246 | |
5/8 | 39 | 279 | |
3/4 | 50 | 324 | |
7/8 | 64 | 363 | |
Full | 108 | No more steps |
Last edited: