// include the library code:
#include <LiquidCrystal.h>
int Min = 0; // This is the Min reading of the sensor
int Max = 1023; // This is the Max reading of the sensor
float Desired_Min = 4.0; // this is the desired number that you want converted with min reading of the sensor
float Desired_Max = 20.0; // this is the desired number that you want converted with max reading of the sensor
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
lcd.begin(16, 2); // start the library
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
float Readings = mapf(sensorValue, Min , Max, Desired_Min, Desired_Max);
// print out the value you read:
Serial.print(voltage);
Serial.print("v / Raw:");
Serial.print(sensorValue);
lcd.setCursor(0,0);
lcd.print("4-20mA"); // LCD 標題
lcd.setCursor(9,0);
lcd.print(char(61)); // 印出 = 的字元
char mAvalue[28];
dtostrf(Readings, 4, 1, mAvalue);
Serial.print(" MA:");
Serial.println(mAvalue);
lcd.print (mAvalue);
lcd.print ("mA");
lcd.setCursor(0,1); // move cursor to second line "1" and 9 spaces over
lcd.print(voltage); lcd.print(F("v / "));
char buffer [8]; // a few bytes larger than your LCD line
uint16_t raw = sensorValue; // data read from instrument
sprintf (buffer, "RAW:%04u", raw); // send data to the buffer
lcd.print (buffer); // display line on buffer
delay(1000);
}
double mapf(double val, double in_min, double in_max, double out_min, double out_max) {
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}