티스토리 뷰

OS/M5Stack

M5Stack : 가변저항(아날로그) 값 가져오기

파란크리스마스 2019. 2. 20. 01:05
728x90

M5Stack Korea인 WIZnet에서 운영하는 메이커 컨텐츠 커뮤니티 사이트의 후원을 받아서 작성되었습니다.

출처

M5Stack : 가변저항(아날로그) 값 가져오기

M5Stack에서도 아날로그 값을 측정 할 수 있는데, M5Stack Iot Starter Kit의 가변저항(아날로그) 값을 축정하는 함수(analogRead)로 아날로그 결과 값을 받아 와서 LCD에 출력 해보았습니다.

M5Go의 B Port와 가변저항을 Grove 케이블로 연결하고, M5Go의 B Port는 36번핀으로 아날로그 input 초기화( pinMode(36, INPUT); )를 setup 함수에서 하고, loop 함수에서 아날로그 값을 측정하는 함수( analogRead(36); )를 사용 하시면 됩니다.

아두이노 핀 초기화

#define sensorPin 36
void setup() {
  pinMode(sensorPin, INPUT);
}

아날로그 값을 축정

void loop() {
  int cur_sensorValue = analogRead(sensorPin);
}

아두이노 전체 소스

#include <M5Stack.h>

// The scrolling area must be a integral multiple of TEXT_HEIGHT
#define TEXT_HEIGHT 23 // Height of text to be printed and scrolled
#define BOT_FIXED_AREA 0 // Number of lines in bottom fixed area (lines counted from bottom of screen)
#define TOP_FIXED_AREA 0 // Number of lines in top fixed area (lines counted from top of screen)
#define YMAX 230 // Bottom of screen area

// The initial y coordinate of the top of the scrolling area
uint16_t yStart = TOP_FIXED_AREA;
// yArea must be a integral multiple of TEXT_HEIGHT
uint16_t yArea = YMAX-TOP_FIXED_AREA-BOT_FIXED_AREA;
// The initial y coordinate of the top of the bottom text line
uint16_t yDraw = YMAX - BOT_FIXED_AREA - TEXT_HEIGHT;

// Keep track of the drawing x coordinate
uint16_t xPos = 0;

// For the byte we read from the serial port
byte data = 0;

// A few test variables used during debugging
boolean change_colour = 1;
boolean selected = 1;

// We have to blank the top line each time the display is scrolled, but this takes up to 13 milliseconds
// for a full width line, meanwhile the serial buffer may be filling... and overflowing
// We can speed up scrolling of short text lines by just blanking the character we drew
int blank[19]; // We keep all the strings pixel lengths to optimise the speed of the top line blanking

// select the input pin for the potentiometer
#define sensorPin 36
 
// declaration
int cur_sensorValue = 0;

void setup() {
  M5.begin();
  M5.Lcd.fillScreen(TFT_BLACK);

  // Change colour for scrolling zone text
  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);

  // Setup scroll area
  setupScrollArea(TOP_FIXED_AREA, BOT_FIXED_AREA);

  //
  pinMode(sensorPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  cur_sensorValue = analogRead(sensorPin);
  char str[256];
  sprintf(str, "potentiometer value = %d\n", cur_sensorValue);
  Serial.println(str);
  drawCentreString(str);    
  delay(100);
}

void drawCentreString(String message) {
    byte data[message.length()];
    message.getBytes(data, message.length());

    for (int i = 0; i < message.length(); i++ ) {
        byte data = message[i];
        
        // If it is a CR or we are near end of line then scroll one line
        if (data == '\r' || xPos>321) {
          // 새줄
          xPos = 0;
          yDraw = scroll_line(); // It can take 13ms to scroll and blank 16 pixel lines
        }
        if (data > 31 && data < 128) {
          xPos += M5.Lcd.drawChar(data,xPos,yDraw,4);
          blank[(18+(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT)%19]=xPos; // Keep a record of line lengths
        }
        //change_colour = 1; // Line to indicate buffer is being emptied
    }

    // 새줄
    xPos = 0;
    yDraw = scroll_line(); // It can take 13ms to scroll and blank 16 pixel lines
}

// ##############################################################################################
// Call this function to scroll the display one text line
// ##############################################################################################
int scroll_line() {
  int yTemp = yStart; // Store the old yStart, this is where we draw the next line
  // Use the record of line lengths to optimise the rectangle size we need to erase the top line
  // M5.Lcd.fillRect(0,yStart,blank[(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT],TEXT_HEIGHT, TFT_BLACK);

  // LCD 초기화 
  if (yStart == 0)
    M5.Lcd.fillScreen(TFT_BLACK);

  // Change the top of the scroll area
  yStart+=TEXT_HEIGHT;
  // The value must wrap around as the screen memory is a circular buffer
  if (yStart >= YMAX - BOT_FIXED_AREA) 
    yStart = TOP_FIXED_AREA + (yStart - YMAX + BOT_FIXED_AREA);
    
  // Now we can scroll the display
  scrollAddress(yStart);
  
  return  yTemp;
}

// ##############################################################################################
// Setup a portion of the screen for vertical scrolling
// ##############################################################################################
// We are using a hardware feature of the display, so we can only scroll in portrait orientation
void setupScrollArea(uint16_t tfa, uint16_t bfa) {
  M5.Lcd.writecommand(ILI9341_VSCRDEF); // Vertical scroll definition
  M5.Lcd.writedata(tfa >> 8);           // Top Fixed Area line count
  M5.Lcd.writedata(tfa);
  M5.Lcd.writedata((YMAX-tfa-bfa)>>8);  // Vertical Scrolling Area line count
  M5.Lcd.writedata(YMAX-tfa-bfa);
  M5.Lcd.writedata(bfa >> 8);           // Bottom Fixed Area line count
  M5.Lcd.writedata(bfa);
}

// ##############################################################################################
// Setup the vertical scrolling start address pointer
// ##############################################################################################
void scrollAddress(uint16_t vsp) {
  M5.Lcd.writecommand(ILI9341_VSCRSADD); // Vertical scrolling pointer
  M5.Lcd.writedata(vsp>>8);
  M5.Lcd.writedata(vsp);
}

실행결과

M5Stack 물품 구매는 <네이버 검색/쇼핑에서 M5StackKorea>를 검색하시거나, M5Stack 공식 파트너인 <위즈네트 쇼핑몰: Shop.wiznet.io> 으로 접속하세요.

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함