Wednesday, November 2, 2016

LCD - Class Work | 11/2/16

Project 23
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // create an lcd object and assign the pins
void setup() {
  lcd.begin(16, 2);
}
void loop() {
  // run the 7 demo routines
  basicPrintDemo();
  displayOnOffDemo();
  setCursorDemo();
  scrollLeftDemo();
  scrollRightDemo();
  cursorDemo();
  createGlyphDemo();
}
void basicPrintDemo() {
  lcd.clear();
  lcd.print("Basic Print");
  delay(2000);
}
void displayOnOffDemo() {
  lcd.clear();
  lcd.print("Display On/Off");
  for (int x = 0; x < 3; x++) {
    lcd.noDisplay();
    delay(1000);
    lcd.display();
    delay(1000);
  }
}
void setCursorDemo() {
  lcd.clear();
  lcd.print("SetCursor Demo");
  delay(1000);
  lcd.clear();
  lcd.setCursor(5, 0);
  lcd.print("5,0");
  delay(2000);
  lcd.setCursor(10, 1);
  lcd.print("10,1");
  delay(2000);
  lcd.setCursor(3, 1);
  lcd.print("3,1");
  delay(2000);
}
void scrollLeftDemo() {
  lcd.clear();
  lcd.print("Scroll Left Demo");
  delay(1000);
  lcd.clear();
  lcd.setCursor(7, 0);
  lcd.print("Beginning");
  lcd.setCursor(9, 1);
  lcd.print("Arduino");
  delay(1000);
  for (int x = 0; x < 16; x++) {
    lcd.scrollDisplayLeft();  // scroll display left 16 times
    delay(250);
  }
}
void scrollRightDemo() {
  lcd.clear();
  lcd.print("Scroll Right");
  lcd.setCursor(0, 1);
  lcd.print("Demo");
  delay(1000);
  lcd.clear();
  lcd.print("Beginning");
  lcd.setCursor(0, 1);
  lcd.print("Arduino");
  delay(1000);
  // Clear the display
  // Clear the display
  for (int x = 0; x < 16; x++) {
    lcd.scrollDisplayRight(); // scroll display right 16 times
    delay(250);
  }
}
void cursorDemo() {
  lcd.clear();
  lcd.cursor();
  lcd.print("Cursor On");
  delay(3000);
  lcd.clear();
  lcd.noCursor();
  lcd.print("Cursor Off");
  delay(3000);
  lcd.clear();
  lcd.cursor();
  lcd.blink();
  lcd.print("Cursor Blink On");
  delay(3000);
  lcd.noCursor();
  lcd.noBlink();
}
void createGlyphDemo() {
  lcd.clear();
  byte happy[8] = {
    B00000,
    B00000,
    B10001,
    B00000,
    B10001,
    B01110,
    B00000,
    B00000
  };
  byte sad[8] = {
    B00000,
    B00000,
    B10001,
    B00000,
    B01110,
    B10001,
    B00000,
    B00000
  };
  lcd.createChar(0, happy);
  lcd.createChar(1, sad);
  
  for (int x = 0; x < 5; x++) {
    lcd.setCursor(8, 0);
    lcd.write((byte)0);
    delay(1000);
    lcd.setCursor(8, 0);
    lcd.write(1);
    delay(1000);
  }
}

Project 24


// PROJECT 24
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int maxC = 0, minC = 100, maxF = 0, minF = 212;

int scale = 1;
int buttonPin = 8;

void setup() {
  lcd.begin(16, 2);
  // create an lcd object and assign the pins
  // Set the display to 16 columns and 2 rows
  analogReference(INTERNAL);
  // analogReference(INTERNAL1V1); If you have an Arduino Mega
  pinMode(buttonPin, INPUT);
  lcd.clear();
}
void loop() {
  lcd.setCursor(0, 0);
  // set cursor to home position
  int sensor = analogRead(0);
  int buttonState = digitalRead(buttonPin); // check for button press
  switch (buttonState) {
    case HIGH:
      scale = -scale;
      lcd.clear();
  }
  switch (scale) {
    case 1:
      celsius(sensor);
      break;
    case -1:
      fahrenheit(sensor);
  }
  delay(250);
}
void celsius(int sensor) {
  lcd.setCursor(0, 0);
  int temp = sensor * 0.1074188;
  lcd.print(temp);
  lcd.write(B11011111);
  lcd.print("C ");
  if (temp > maxC) {
    maxC = temp;
  }
  if (temp < minC) {
    minC = temp;
  }
  lcd.setCursor(0, 1);
  lcd.print("H=");
  lcd.print(maxC);
  lcd.write(B11011111);
  lcd.print("C L=");
  lcd.print(minC);
  lcd.write(B11011111);
  lcd.print("C ");
}
void fahrenheit(int sensor) {
  lcd.setCursor(0, 0);
  float temp = ((sensor * 0.1074188) * 1.8) + 32; // convert to F
  lcd.print(int(temp));
  lcd.write(B11011111);
  lcd.print("F ");
  if (temp > maxF) {
    maxF = temp;
  }
  if (temp < minF) {
    minF = temp;
  }
  lcd.setCursor(0, 1);
  lcd.print("H=");
  lcd.print(maxF);
  lcd.write(B11011111);
  lcd.print("F L=");
  lcd.print(minF);
  lcd.write(B11011111);
  lcd.print("F ");
}

No comments:

Post a Comment