วันศุกร์ที่ 28 กันยายน พ.ศ. 2561

Arduino 20 Arduino - LCD 1602A Keypad LCD Shield Relay Control

Arduino - LCD 1602A Keypad LCD Shield Relay Control

Things used in this project

Hardware components

Arduino Mega 2560 & Genuino Mega 2560
×1
ControlEverything.com 4-CHANNEL RELAY CONTROLLER FOR I2C
×1
LCD shield with keypad
×1
Jumper wires (generic)
×1
Power cables + lamp sockets
×1



                      Code

 


/*
Author:Danny van den Brande, Arduinosensors.nl. BlueCore Tech.
Hello world! Today i made a small example on how to use a LCD keypad.
You can add anything to it. In this example we switch on and off relays.
Good luck!
 */
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int relay1 = 30;
int relay2 = 31;
int relay3 = 32;
int relay4 = 33;

int lcdkeys;
void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("BlueCore Tech");
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
}
void loop() {
  Serial.println(lcdkeys);// look in the serial monitor and press buttons to see values.
  lcdkeys=analogRead(0);
  if (lcdkeys<50) {  //if value less then 50 
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("DEVICE 1");
  lcd.setCursor(0, 1);
  lcd.print("IS ON");
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, HIGH);
  }
  else if(lcdkeys<195) { //if value less then 195 and so on. 
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("DEVICE 2");
    lcd.setCursor(0, 1);
    lcd.print("IS ON");
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW);       digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW); }
  else if(lcdkeys<380) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("DEVICE 3");
    lcd.setCursor(0, 1);
    lcd.print("IS ON");
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
  }
  else if(lcdkeys<650) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("DEVICE 4");
    lcd.setCursor(0, 1);
    lcd.print("IS ON");
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, LOW);
  }
  else if(lcdkeys<1023) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("BLUECORE TECH");
    lcd.setCursor(0, 1);
    lcd.print("OFF");
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);  
  }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น