๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[์ž๋™์ฐจ ๋’ทํŽธ์— LCDํŒ์œผ๋กœ ์ƒํ™ฉ ์ „๋‹ฌํ•˜๊ธฐ 4] Joystick button ๋งˆ๋‹ค lcd ์ถœ๋ ฅ ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/Arduino

[์ž๋™์ฐจ ๋’ทํŽธ์— LCDํŒ์œผ๋กœ ์ƒํ™ฉ ์ „๋‹ฌํ•˜๊ธฐ 4] Joystick button ๋งˆ๋‹ค lcd ์ถœ๋ ฅ

์ง•์ง•์•ŒํŒŒ์นด 2023. 11. 10. 12:09
728x90
๋ฐ˜์‘ํ˜•

โญ Joystick button ๋งˆ๋‹ค lcd ์ถœ๋ ฅ

 โ“ Wire.h

I2C ํ†ต์‹ ์„ ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ. ๋งˆ์Šคํ„ฐ ํ•œ๋Œ€์™€ ์Šฌ๋ ˆ์ด๋ธŒ ์—ฌ๋Ÿฌ๋Œ€๋ฅผ ํ†ต์‹ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.

SDA๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก๊ณผ ์†ก์‹ ์„ ํ•˜๋Š” ๋ฐ˜์ด์ค‘(์–‘๋ฐฉํ–ฅ ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์†ก์‹ ๊ณผ ์ˆ˜์‹ ์ค‘์— ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•œ๋‹ค.)

SCL์€ ๊ธฐ์ค€ ํด๋Ÿญ์„ ์Šฌ๋ ˆ์ด๋ธŒ์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.

 

์Šฌ๋ ˆ์ด๋ธŒ์˜ ๊ตฌ๋ณ„์„ ์ฃผ์†Œ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธด ๋ฐ์ดํ„ฐ์˜ ๊ตํ™˜์—๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค.

ํด๋Ÿญ์„ ๊ฐ€์ง€๊ณ  ํ†ต์‹ ํ•˜๋ฏ€๋กœ ๋™๊ธฐ์‹ ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

arduino(uno๋Š” ์ œ์™ธ)์—๋Š” I2C ํ†ต์‹ ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์นฉ์ด ๋‚ด์žฅ๋˜์–ด์žˆ๋‹ค.

// mytest.ino
//#include "test2.h"

// LiquidCrystal ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ 
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "test2.h" 

// ์กฐ์ด์Šคํ‹ฑ ์‰ด๋“œ์˜ ๋ฒ„ํŠผ์ด ๋ˆ„๋ฅด๋Š” ๊ฑธ ์ž…๋ ฅ๋ฐ›๊ธฐ ์œ„ํ•ด ์„ ์–ธ
const int buttonPin2 = 2;                           
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;

// lcd ๊ฐ์ฒด ์„ ์–ธ
LiquidCrystal_I2C lcd(0x27, 16, 2);      // ์ฃผ์†Œ, ์—ด, ํ–‰

void setup() {
  Serial.begin(9600);                               // ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์„ ์‹œ์ž‘ํ•˜๋ฉฐ, ํ†ต์‹ ์†๋„๋Š” 9600
 
  pinMode(buttonPin2, INPUT_PULLUP );
  pinMode(buttonPin3, INPUT_PULLUP );
  pinMode(buttonPin4, INPUT_PULLUP );
  pinMode(buttonPin5, INPUT_PULLUP );

  lcd.init();     // LCD ์ดˆ๊ธฐํ™”
  // Print a message to the LCD
  lcd.backlight();        // LCD ๋ฐฑ๋ผ์ดํŠธ ์ผœ๊ธฐ
}

void loop() {

  int X = analogRead(0);                           // ๋ณ€์ˆ˜ X์— ์•„๋‚ ๋กœ๊ทธ 0๋ฒˆํ•€์— ์ž…๋ ฅ๋˜๋Š” ์‹ ํ˜ธ๋ฅผ ๋Œ€์ž…
  int Y = analogRead(1);                           // ๋ณ€์ˆ˜ Y์— ์•„๋‚ ๋กœ๊ทธ 1๋ฒˆํ•€์— ์ž…๋ ฅ๋˜๋Š” ์‹ ํ˜ธ๋ฅผ ๋Œ€์ž…
 
  int buttonValue2 = digitalRead(2);               // buttonValue๊ฐ’ ์„ ์–ธ
  int buttonValue3 = digitalRead(3);
  int buttonValue4 = digitalRead(4);
  int buttonValue5 = digitalRead(5);

  char data[16];
  

  if (buttonValue2 == LOW) {                       // if๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๊ฐ ๋ฒ„ํŠผ์ด ๋ˆŒ๋ฆฌ๋ฉด ์•ŒํŒŒ๋ฒณ์ด ์‹œ๋ฆฌ์–ผ๋ชจ๋‹ˆํ„ฐ์— ์ถœ๋ ฅ๋˜๋„๋ก ์„ค์ •
    String data = "front car!";
    printCountTest1(data);
  }
  if (buttonValue3 == LOW) {
    String data = "front animal!";
    printCountTest1(data);
  }
  if (buttonValue4 == LOW) {
    String data = "baby in car";
    printCountTest1(data);
  }
  if (buttonValue5 == LOW) {
    String data = "broken car";
    printCountTest1(data);
  }
  delay(500);                                        // 0.5์ดˆ๋™์•ˆ ์ง€์†
}
728x90
๋ฐ˜์‘ํ˜•
Comments