MrAlvin

Read analoge sensor - display on LCD

May 25th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. // A very primitive example of how to read from analogue ports and display values on a LCD display
  2. //
  3. // Note: There are known errors of not cleaning old numbers before writing new numbers
  4.  
  5. // /*****\  Libraries (and/or files) to be included: /*****\
  6.  
  7. // needed for the I2C_LCD to work
  8. #include <Wire.h>               // Comes with Arduino IDE (provides the basic I2C communications)
  9. #include <LiquidCrystal_I2C.h>  // MUST be the latest library from: https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
  10.  
  11. // /*****\ Variables to be used in this sketch (program)  /*****\
  12.  
  13. //***** I2C_LCD details *****
  14.  
  15. // Set the pins on the I2C chip used for LCD connections:
  16. // LiquidCrystal_I2C lcd(addr, en, rw, rs, d4, d5, d6, d7, bl, blpol)
  17. // I2C board marked: "YwRobot Arduino LCM1602 IIC V1" and
  18. // I2C board marked: no markings, but a0, a1 & A2 are dual sodlerpads under the blue potentiometer,
  19. //                   and a jumper is marked LED. Bought on ebay from seller: mxqtech
  20. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
  21.  
  22.  
  23. //***** Analog variables *****
  24. const int analogInPin1 = A1;  // Analog input pin
  25. const int analogInPin2 = A2;  // Analog input pin
  26. const int analogInPin3 = A3;  // Analog input pin
  27. int sensorValue1_raw = 0;
  28. int sensorValue2_raw = 0;
  29. int sensorValue3_raw = 0;
  30. float sensorValue1 = 0;
  31. float sensorValue2 = 0;
  32. float sensorValue3 = 0;
  33.  
  34.  
  35. /**************************************************************************/
  36. // Arduino SETUP - is run Once, after the Arduino has been reset.
  37. /**************************************************************************/
  38. void setup() {
  39.   // set up the LCD's number of columns and rows:
  40.   lcd.begin(20,4);   // initialize the lcd for 20 chars 4 lines,
  41.   lcd.backlight();   // turn on backlight
  42.   // Print a message to the LCD.
  43.   // lcd.print("hello, world!");
  44.  
  45. }
  46.  
  47.  
  48. /**************************************************************************/
  49. // Arduino LOOP procedure - continues to be repeated, until power off or
  50. /**************************************************************************/
  51. void loop() {
  52.   read_sensors();
  53.   convert_readings();
  54.   display_readings();  
  55. }
  56.  
  57.  
  58. /**************************************************************************/
  59. // Functions that does all the work
  60. /**************************************************************************/
  61. void read_sensors(){
  62.   sensorValue1_raw = analogRead(analogInPin1);
  63.   sensorValue2_raw = analogRead(analogInPin2);
  64.   sensorValue3_raw = analogRead(analogInPin3);
  65. }
  66.  
  67. void convert_readings(){
  68.   // Here we need to make a conversion of the raw value (0-1023) to a decimal value
  69.   // that relates the raw sensor value to whatever measurement is used in the tube being sensed/measured.
  70.   // Here it is also possible to trimm with calibration values
  71.   sensorValue1 = (sensorValue1_raw - 205) * 2.69;  // 205 and 2.69 are here random numbers, but in real life will be usefull values
  72.   sensorValue2 = (sensorValue2_raw - 205) * 2.69;
  73.   sensorValue3 = (sensorValue3_raw - 205) * 2.69;
  74. }
  75.  
  76. void display_readings(){
  77.   int a = 0;
  78.   // NOTE: Cursor Position: (CHAR, LINE) start at 0  
  79.   lcd.setCursor(0,1); //Start at character 0 on line 1
  80.   lcd.print( sensorValue1_raw );
  81.   //discovering number of digits before the decimal point
  82.   if (sensorValue1 > 999) a = 0;
  83.   else if (sensorValue1 > 99) a = 1;
  84.   else if(sensorValue1 > 9) a = 2;
  85.   lcd.setCursor(0, 7 + a);
  86.   lcd.print( sensorValue1 );
  87.  
  88.   lcd.setCursor(0,2); //Start at character 0 on line 2
  89.   lcd.print( sensorValue2_raw );
  90.   //discovering number of digits before the decimal point
  91.   if (sensorValue2 > 999) a = 0;
  92.   else if (sensorValue2 > 99) a = 1;
  93.   else if(sensorValue2 > 9) a = 2;
  94.   lcd.setCursor(0, 7 + a);
  95.   lcd.print( sensorValue2 );
  96.  
  97.   lcd.setCursor(0,3); //Start at character 0 on line 3
  98.   lcd.print( sensorValue3_raw );
  99.   //discovering number of digits before the decimal point
  100.   if (sensorValue3 > 999) a = 0;
  101.   else if (sensorValue3 > 99) a = 1;
  102.   else if(sensorValue3 > 9) a = 2;
  103.   lcd.setCursor(0, 7 + a);
  104.   lcd.print( sensorValue3 );
  105. }
Advertisement
Add Comment
Please, Sign In to add comment