Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A very primitive example of how to read from analogue ports and display values on a LCD display
- //
- // Note: There are known errors of not cleaning old numbers before writing new numbers
- // /*****\ Libraries (and/or files) to be included: /*****\
- // needed for the I2C_LCD to work
- #include <Wire.h> // Comes with Arduino IDE (provides the basic I2C communications)
- #include <LiquidCrystal_I2C.h> // MUST be the latest library from: https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
- // /*****\ Variables to be used in this sketch (program) /*****\
- //***** I2C_LCD details *****
- // Set the pins on the I2C chip used for LCD connections:
- // LiquidCrystal_I2C lcd(addr, en, rw, rs, d4, d5, d6, d7, bl, blpol)
- // I2C board marked: "YwRobot Arduino LCM1602 IIC V1" and
- // I2C board marked: no markings, but a0, a1 & A2 are dual sodlerpads under the blue potentiometer,
- // and a jumper is marked LED. Bought on ebay from seller: mxqtech
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
- //***** Analog variables *****
- const int analogInPin1 = A1; // Analog input pin
- const int analogInPin2 = A2; // Analog input pin
- const int analogInPin3 = A3; // Analog input pin
- int sensorValue1_raw = 0;
- int sensorValue2_raw = 0;
- int sensorValue3_raw = 0;
- float sensorValue1 = 0;
- float sensorValue2 = 0;
- float sensorValue3 = 0;
- /**************************************************************************/
- // Arduino SETUP - is run Once, after the Arduino has been reset.
- /**************************************************************************/
- void setup() {
- // set up the LCD's number of columns and rows:
- lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines,
- lcd.backlight(); // turn on backlight
- // Print a message to the LCD.
- // lcd.print("hello, world!");
- }
- /**************************************************************************/
- // Arduino LOOP procedure - continues to be repeated, until power off or
- /**************************************************************************/
- void loop() {
- read_sensors();
- convert_readings();
- display_readings();
- }
- /**************************************************************************/
- // Functions that does all the work
- /**************************************************************************/
- void read_sensors(){
- sensorValue1_raw = analogRead(analogInPin1);
- sensorValue2_raw = analogRead(analogInPin2);
- sensorValue3_raw = analogRead(analogInPin3);
- }
- void convert_readings(){
- // Here we need to make a conversion of the raw value (0-1023) to a decimal value
- // that relates the raw sensor value to whatever measurement is used in the tube being sensed/measured.
- // Here it is also possible to trimm with calibration values
- sensorValue1 = (sensorValue1_raw - 205) * 2.69; // 205 and 2.69 are here random numbers, but in real life will be usefull values
- sensorValue2 = (sensorValue2_raw - 205) * 2.69;
- sensorValue3 = (sensorValue3_raw - 205) * 2.69;
- }
- void display_readings(){
- int a = 0;
- // NOTE: Cursor Position: (CHAR, LINE) start at 0
- lcd.setCursor(0,1); //Start at character 0 on line 1
- lcd.print( sensorValue1_raw );
- //discovering number of digits before the decimal point
- if (sensorValue1 > 999) a = 0;
- else if (sensorValue1 > 99) a = 1;
- else if(sensorValue1 > 9) a = 2;
- lcd.setCursor(0, 7 + a);
- lcd.print( sensorValue1 );
- lcd.setCursor(0,2); //Start at character 0 on line 2
- lcd.print( sensorValue2_raw );
- //discovering number of digits before the decimal point
- if (sensorValue2 > 999) a = 0;
- else if (sensorValue2 > 99) a = 1;
- else if(sensorValue2 > 9) a = 2;
- lcd.setCursor(0, 7 + a);
- lcd.print( sensorValue2 );
- lcd.setCursor(0,3); //Start at character 0 on line 3
- lcd.print( sensorValue3_raw );
- //discovering number of digits before the decimal point
- if (sensorValue3 > 999) a = 0;
- else if (sensorValue3 > 99) a = 1;
- else if(sensorValue3 > 9) a = 2;
- lcd.setCursor(0, 7 + a);
- lcd.print( sensorValue3 );
- }
Advertisement
Add Comment
Please, Sign In to add comment