Tuesday, March 11, 2014

Arduino 4x20 LCD and 2 inputs comparator

Staring a new build. So as to that I had an idea, that it can be helpful for everyone starting with code writing to try and manipulate this snippet. It's a simple code to operate a LCD and 2 inputs on A0 and A1. It compares the two with if and else. After the comparator it makes a readable output on the LCD. From here it's very simple to put the comparison into outputs of your own choosing.




//4x20 LCD, 2 sensors in A0 and A1 compare them and output to the LCD
// include the library code:
#include <LiquidCrystal.h>

// initialize the library liquidcrystal.h with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int sensorPin1 = A0; // defines input pin
int sensorValue1 = 0; // make a varianle sensorValue
int sensorPin2 = A1;
int sensorValue2 = 0;

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(20, 4);

}

void loop() {

  sensorValue1 = analogRead(sensorPin1); // get the value of sensor 1
  sensorValue2 = analogRead(sensorPin2); // get valur for sensor 2

    lcd.setCursor(0, 0); // Set cursor to top left
  lcd.print(sensorValue1);

  lcd.setCursor(0, 1);
  lcd.print(sensorValue2);

  if (sensorValue1 < sensorValue2){
    lcd.setCursor(0, 2);
    lcd.print("1 is higher than 2");
  }
  else{
    lcd.setCursor(0, 3);
    lcd.print("2 is higher than 1");
  }

  delay(200);
  lcd.clear();


}