Thursday, April 30, 2015

Read Digital Pin and Serial Message, Display it on LCD using Arduino

 In addition of my previous tinkering with LCD on arduino nano, I add a chunk of program to display the state of digital pin 7 on second row LCD.

I also remove the scroll command, package it as separate function but not called. I plan to using another scrolling method, some code without delay. (had it in mind, but still lazy to coding it, maybe next)




#include <softwareSerial.h>
#include<liquidCrystal.h>
SoftwareSerial mySerial(8, 9); // RX, TX

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);//rs e d4 d5 d6 d7
String tulisan = "Tadaa...";
String info =":)";
void setup() {
  pinMode(7,INPUT);
  pinMode(6,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(13,OUTPUT);
  lcd.begin(16, 2);
  lcd.print(tulisan);
  mySerial.begin(9600);
  delay(1000);
}


void loop() {
  if (digitalRead(7)==1){
    digitalWrite(10,HIGH);
  }else{
    digitalWrite(10,LOW);
  }
  if (mySerial.available()){
    lcd.clear();
    digitalWrite(13,HIGH);
    while(mySerial.available()>0){
      lcd.setCursor(0,0);
      tulisan=mySerial.readString();
      mySerial.println(tulisan);
      lcd.print(tulisan);
    }
  }
  
  //scLeft();
  //scRight();
  //scDef();
  info ="Pin7 = ";
  info+=digitalRead(7);
  
  lcd.setCursor(0,1);
  lcd.print(info);
  digitalWrite(13,LOW);
  delay(1000);
}

void scLeft(){
  for (int i = 0; i < tulisan.length(); i++) {
    lcd.scrollDisplayLeft();
    delay(150);
  }
}

void scRight(){
  for (int i = 0; i < (tulisan.length()+16); i++) {
    lcd.scrollDisplayRight();
    delay(150);
  }
}

void scDef(){
  for (int i = 0; i < 16; i++) {
    lcd.scrollDisplayLeft();
    delay(150);
  }
}
//PWM: 3, 5, 6, 9, 10, and 11


.


Menulis perintah ke baris pertama LCD via bluetooth, baris kedua digunakan untuk mengecek pin 7 (digital). Fitur scrill (di post sebelumnya dihilangkan, ada di bawah sebagai function tetapi tak dipanggil di program utama)