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

[์ž๋™์ฐจ ๋’ทํŽธ์— LCDํŒ์œผ๋กœ ์ƒํ™ฉ ์ „๋‹ฌํ•˜๊ธฐ 6] ์กฐ์ด์Šคํ‹ฑ x, y 4๋ถ„ํ•  ํ•ด์„œ led ๋ถˆ ์ผœ๊ธฐ ๋ณธ๋ฌธ

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

[์ž๋™์ฐจ ๋’ทํŽธ์— LCDํŒ์œผ๋กœ ์ƒํ™ฉ ์ „๋‹ฌํ•˜๊ธฐ 6] ์กฐ์ด์Šคํ‹ฑ x, y 4๋ถ„ํ•  ํ•ด์„œ led ๋ถˆ ์ผœ๊ธฐ

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

โญ ์กฐ์ด์Šคํ‹ฑ x, y

  • X์ถ•์„ ์ฝ๊ณ  X์ถ•์˜ ๊ฐ’์— ๋งŒ์กฑํ•˜๋Š” ์™ผ์ชฝ/์˜ค๋ฅธ์ชฝ์ด ๊ฒฐ์ •๋˜๋ฉด์€ Y์ถ•์€ ๋ฌด์‹œํ•˜๊ณ  ๋„˜์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋ž˜์„œ X์ถ•์˜ ๋ฐฉํ–ฅํ‚ค๋ฅผ ๊ตฌํ•˜๊ณ  Y์ถ•์˜ ๋ฐฉํ–ฅํ‚ค๋ฅผ ๊ตฌํ•˜๋Š” ์‹์„ ๋”ฐ๋กœ if๋ฌธ์„ ๋ถ„๋ฆฌํ•ด์„œ ์ฝ”๋”ฉํ•œ ๊ฒƒ
  • AXIS_X ๊ธฐ์ค€ ์‹ ํ˜ธ๋กœ ๋ณด๋ฉด ๊ธฐ์ค€ ์‹ ํ˜ธ ๊ฐ’์—์„œ +, - ๊ฐ’์ธ ๋‘˜์ค‘ ํ•˜๋‚˜์ƒํƒœ๋งŒ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ.
    • ๋™์‹œ ์˜ค๋ฅธ์ชฝ, ์™ผ์ชฝ์ด ๋‚˜์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
    • ๊ทธ๋ž˜์„œ if~else if๋ฌธ์„ ์‚ฌ์šฉ
const int AXIS_X = A0;
const int AXIS_Y = A1;
const int SW_P = 3; 
 
void setup() {
  Serial.begin(9600);
  pinMode(SW_P,INPUT_PULLUP);
}
 void loop() {
  //X์ถ• ๋ฐฉํ–ฅ๊ฐ’
  if(analogRead(AXIS_X)<=300){
    Serial.println('a');
  }
  else if(analogRead(AXIS_X)>=700){
    Serial.println('d');   
  }
  //Y์ถ• ๋ฐฉํ–ฅ๊ฐ’
  if(analogRead(AXIS_Y)<=300){
    Serial.println('w');      
  }
  else if(analogRead(AXIS_Y)>=700){
    Serial.println('s');
  }
  delay(20);
}

 

โญ ์กฐ์ด์Šคํ‹ฑ x, y 4๋ถ„ํ•  ํ•ด์„œ led ๋ถˆ ์ผœ๊ธฐ

// mytest3.ino

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

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

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

// led ๊ฐ์ฒด ์„ ์–ธ
int GREEN = 13;
int RED = 11;
int BLUE = 9;

void setup() {
  // ์‹œ๋ฆฌ์–ผ ํ†ต์‹ ์„ ์‹œ์ž‘ํ•˜๋ฉฐ, ํ†ต์‹ ์†๋„๋Š” 9600
  Serial.begin(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 ๋ฐฑ๋ผ์ดํŠธ ์ผœ๊ธฐ

  pinMode(RED, OUTPUT); 
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);  
}

// LCD ์ถœ๋ ฅ
void LCDprint(String data) {
  lcd.setCursor(0, 0);    // 1๋ฒˆ์งธ, 1๋ผ์ธ
  lcd.print(data);
  delay(1500);

  lcd.clear();            // ๊ธ€์ž๋ฅผ ๋ชจ๋‘ ์ง€์›Œ๋ผ
}

void loop() {

  if(Serial.available()) {
      char data[16] = {0,};
      char buf;

      for (int i = 0; i < 16; i++) {
          buf = Serial.read();
          //Serial.print("hi");
          data[i] = buf;
      }

      //Serial.print(data);
      LCDprint(data);
      delay(500);
  }

  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);

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

  Serial.print(X);
  Serial.print(", ");
  Serial.println(Y);


  if(X < 495) {
    // ์™ผ์ชฝ
    if(Y < 515) {
      Serial.println('1');      
      digitalWrite(GREEN, HIGH);     
      digitalWrite(BLUE, LOW);     
      digitalWrite(RED, HIGH);         //๋นจ๊ฐ„๋ถˆ ๋„๊ธฐ
    }
    else if(Y >= 515) {
      Serial.println('1');
      digitalWrite(GREEN, HIGH);     
      digitalWrite(BLUE, HIGH);     
      digitalWrite(RED, LOW);      
    }
  }

  else if(X > 495) {
    // ์˜ค๋ฅธ์ชฝ
    if(Y < 515) {
      Serial.println('2');    
      digitalWrite(GREEN, HIGH);     
      digitalWrite(BLUE, LOW);     
      digitalWrite(RED, HIGH);      
    }
    else if(Y >= 515) {
      Serial.println('2');
      digitalWrite(GREEN, HIGH);     
      digitalWrite(BLUE, HIGH);     
      digitalWrite(RED, LOW);      
    }
  }

  else if(X == 495) {
    // ํ›„์ง„
    if(Y < 515) {
      Serial.println('4');     
      digitalWrite(GREEN, LOW);     
      digitalWrite(BLUE, LOW);     
      digitalWrite(RED, HIGH);      
    }
    // ์ „์ง„
    else if(Y > 515) {
      Serial.println('3');
      digitalWrite(GREEN, LOW);     
      digitalWrite(BLUE, HIGH);     
      digitalWrite(RED, LOW);      
    }
  }

  
  if (X == 495) {
    if (Y == 515) {
      digitalWrite(GREEN, LOW);     
      digitalWrite(BLUE, LOW);     
      digitalWrite(RED, LOW);  
    }
  }

  delay(1000); 
}
728x90
๋ฐ˜์‘ํ˜•
Comments