질문게시판 > Atmega128과 도트 매트릭스질문

TODAY6,715 TOTAL7,736,834
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | 질문게시판 | Atmega128과 도트 매트릭스질문

페이지 정보

작성자 탈모공학자 작성일2023-12-01 01:36 조회1,645회 댓글1건

본문

	도트매트릭스를 이용해서 게임을 만드는 중인데, 지금 막힌 부분이 한 플레이어당 9개씩 돌을 놓고 나서 18개를 다 놓으면 그다음부터는 자기가 놓은 도트를 선택해서 한칸씩 옮길 수 있어야하는데 그게 너무 어렵습니다. 스위치는 새로 추가해도 되긴 하는데 사실 지금 여기서 아무리 해봐도 감이 안잡혀요. 아니면 내가 착수한 도트를 다시 선택해서 움직이게만 된다면 좀 진전이 있을텐데 말이죠..

#define F_CPU 16E6
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "TWI.h"
#include "Dot_I2C.h"

#define MAX_STONES 18  // 최대 돌의 개수
#define NUM_VALID_POSITIONS 24  // 착수 가능한 위치의 개수

// 착수 가능한 위치 좌표
uint8_t validPositions[NUM_VALID_POSITIONS][2] = {
{1, 1}, {4, 1}, {7, 1},
{2, 2}, {4, 2}, {6, 2},
{3, 3}, {4, 3}, {5, 3},
{1, 4}, {2, 4}, {3, 4}, {5, 4}, {6, 4}, {7, 4},
{3, 5}, {4, 5}, {5, 5},
{2, 6}, {4, 6}, {6, 6},
{1, 7}, {4, 7}, {7, 7}
};

typedef uint8_t DOT_I2C_Color;  // 색상 유형 정의

#define PLAYER1_COLOR DOT_I2C_RED
#define PLAYER2_COLOR DOT_I2C_GREEN

volatile unsigned char currentRow = 1;
volatile unsigned char currentCol = 1;
volatile uint8_t putPressed = 0;  // 착수 버튼
uint8_t currentPlayer = 1;  // 1 또는 2

uint8_t stoneRows[MAX_STONES];
uint8_t stoneCols[MAX_STONES];
uint8_t stoneCount = 0;


void updateDisplay() {
DOT_I2C_Clear();

// 착수한 돌들 표시
for (uint8_t i = 0; i < stoneCount; ++i) {
DOT_I2C_Color stoneColor = (i % 2 == 0) ? PLAYER1_COLOR : PLAYER2_COLOR;
DOT_I2C_DrawPixel(stoneCols[i], stoneRows[i], stoneColor);
}

// 현재 차례의 돌 표시
DOT_I2C_DrawPixel(currentCol, currentRow, (currentPlayer == 1) ? PLAYER1_COLOR : PLAYER2_COLOR);

DOT_I2C_WriteDisplay();
}



void putStone() {
if (stoneCount >= MAX_STONES) {
return; // 착수 가능 돌 개수 초과 시 함수 종료
}

// 착수 가능한 위치인지 확인
for (uint8_t i = 0; i < NUM_VALID_POSITIONS; ++i) {
if (validPositions[i][0] == currentCol && validPositions[i][1] == currentRow) {
// 확인할 변수
uint8_t positionOccupied = 0;

// 이미 돌이 있는지 확인
for (uint8_t j = 0; j < stoneCount; ++j) {
if (stoneCols[j] == currentCol && stoneRows[j] == currentRow) {
positionOccupied = 1;
break;
}
}

// 이미 돌이 있는 위치라면 아무 동작도 수행하지 않음
if (positionOccupied) {
break;
}

// 착수한 돌의 위치 저장
stoneRows[stoneCount] = currentRow;
stoneCols[stoneCount] = currentCol;

// 착수한 플레이어에 따라 돌의 색상 결정
DOT_I2C_Color stoneColor = (currentPlayer == 1) ? PLAYER1_COLOR : PLAYER2_COLOR;

// 저장된 돌의 개수 증가
++stoneCount;

// 돌을 그릴 때 색상 적용
DOT_I2C_DrawPixel(currentCol, currentRow, stoneColor);

// 디스플레이 업데이트
DOT_I2C_WriteDisplay();

// 차례 교체
currentPlayer = (currentPlayer == 1) ? 2 : 1;

// 표시 업데이트
updateDisplay();
break;
}
}
}


ISR(INT4_vect) {
// 왼쪽 스위치 눌림
if (currentCol == 4 && currentRow == 1) {
currentCol = 1;
} else if (currentCol == 7 && currentRow == 1) {
currentCol = 4;
} else if (currentCol == 7 && currentRow == 7) {
currentCol = 4;
} else if (currentCol == 4 && currentRow == 7) {
currentCol = 1;
} else if (currentCol == 4 && currentRow == 2) {
currentCol = 2;
} else if (currentCol == 6 && currentRow == 2) {
currentCol = 4;
} else if (currentCol == 6 && currentRow == 6) {
currentCol = 4;
} else if (currentCol == 4 && currentRow == 6) {
currentCol = 2;
} else if (currentCol == 2 && currentRow == 4) {
currentCol = 1;
} else if (currentCol == 3 && currentRow == 4) {
currentCol = 2;
} else if (currentCol == 7 && currentRow == 4) {
currentCol = 6;
} else if (currentCol == 6 && currentRow == 4) {
currentCol = 5;
} else if (currentCol == 4 && currentRow == 3) {
currentCol = 3;
} else if (currentCol == 5 && currentRow == 3) {
currentCol = 4;
} else if (currentCol == 5 && currentRow == 5) {
currentCol = 4;
} else if (currentCol == 4 && currentRow == 5) {
currentCol = 3;
}

updateDisplay();
_delay_ms(270);
}

ISR(INT5_vect) {
// 오른쪽 스위치 눌림
if (currentCol == 1 && currentRow == 1) {
currentCol = 4;
} else if (currentCol == 4 && currentRow == 1) {
currentCol = 7;
} else if (currentCol == 1 && currentRow == 7) {
currentCol = 4;
} else if (currentCol == 4 && currentRow == 7) {
currentCol = 7;
} else if (currentCol == 2 && currentRow == 2) {
currentCol = 4;
} else if (currentCol == 4 && currentRow == 2) {
currentCol = 6;
} else if (currentCol == 2 && currentRow == 6) {
currentCol = 4;
} else if (currentCol == 4 && currentRow == 6) {
currentCol = 6;
} else if (currentCol == 2 && currentRow == 4) {
currentCol = 3;
} else if (currentCol == 1 && currentRow == 4) {
currentCol = 2;
} else if (currentCol == 6 && currentRow == 4) {
currentCol = 7;
} else if (currentCol == 5 && currentRow == 4) {
currentCol = 6;
} else if (currentCol == 3 && currentRow == 3) {
currentCol = 4;
} else if (currentCol == 4 && currentRow == 3) {
currentCol = 5;
} else if (currentCol == 3 && currentRow == 5) {
currentCol = 4;
} else if (currentCol == 4 && currentRow == 5) {
currentCol = 5;
}

updateDisplay();
_delay_ms(270);
}

ISR(INT2_vect) {
// 위쪽 스위치 눌림
if (currentCol == 1 && currentRow == 7) {
currentRow = 4;
} else if (currentCol == 1 && currentRow == 4) {
currentRow = 1;
} else if (currentCol == 4 && currentRow == 3) {
currentRow = 2;
} else if (currentCol == 4 && currentRow == 2) {
currentRow = 1;
} else if (currentCol == 6 && currentRow == 4) {
currentRow = 2;
} else if (currentCol == 6 && currentRow == 6) {
currentRow = 4;
} else if (currentCol == 2 && currentRow == 4) {
currentRow = 2;
} else if (currentCol == 2 && currentRow == 6) {
currentRow = 4;
} else if (currentCol == 7 && currentRow == 7) {
currentRow = 4;
} else if (currentCol == 7 && currentRow == 4) {
currentRow = 1;
} else if (currentCol == 3 && currentRow == 5) {
currentRow = 4;
} else if (currentCol == 3 && currentRow == 4) {
currentRow = 3;
} else if (currentCol == 5 && currentRow == 5) {
currentRow = 4;
} else if (currentCol == 5 && currentRow == 4) {
currentRow = 3;
} else if (currentCol == 4 && currentRow == 7) {
currentRow = 6;
} else if (currentCol == 4 && currentRow == 6) {
currentRow = 5;
}

updateDisplay();
_delay_ms(270);
}

ISR(INT3_vect) {
// 아래쪽 스위치 눌림
if (currentCol == 1 && currentRow == 1) {
currentRow = 4;
} else if (currentCol == 1 && currentRow == 4) {
currentRow = 7;
} else if (currentCol == 7 && currentRow == 1) {
currentRow = 4;
} else if (currentCol == 7 && currentRow == 4) {
currentRow = 7;
} else if (currentCol == 4 && currentRow == 1) {
currentRow = 2;
} else if (currentCol == 4 && currentRow == 2) {
currentRow = 3;
} else if (currentCol == 4 && currentRow == 5) {
currentRow = 6;
} else if (currentCol == 4 && currentRow == 6) {
currentRow = 7;
} else if (currentCol == 6 && currentRow == 2) {
currentRow = 4;
} else if (currentCol == 6 && currentRow == 4) {
currentRow = 6;
} else if (currentCol == 2 && currentRow == 2) {
currentRow = 4;
} else if (currentCol == 2 && currentRow == 4) {
currentRow = 6;
} else if (currentCol == 3 && currentRow == 3) {
currentRow = 4;
} else if (currentCol == 3 && currentRow == 4) {
currentRow = 5;
} else if (currentCol == 5 && currentRow == 3) {
currentRow = 4;
} else if (currentCol == 5 && currentRow == 4) {
currentRow = 5;
}

updateDisplay();
_delay_ms(270);
}

ISR(INT6_vect) {
// 착수 스위치 눌림
putPressed = 1;
}



int main() {
TWI_Init();
DOT_I2C_Init();

// 외부 인터럽트 설정
EICRA = (1 << ISC01) | (1 << ISC11) | (1 << ISC21) | (1 << ISC31);
EIMSK = (1 << INT4) | (1 << INT5) | (1 << INT2) | (1 << INT3) | (1 << INT6);

sei(); // 전역적으로 인터럽트 활성화

while (1) {
// 착수 버튼이 눌렸을 때만 착수
if (putPressed) {
putStone();
putPressed = 0;
}

_delay_ms(200); // 딜레이 추가
}
}
  • BASIC4MCU 작성글 SNS에 공유하기
  • 페이스북으로 보내기
  • 트위터로 보내기
  • 구글플러스로 보내기

댓글 1

조회수 1,645

master님의 댓글

master 작성일

복잡한 코드는 남이 도와드리기 어렵습니다.
복잡할 수록 코딩하는 시간보다 고민하는 시간이 많아야 합니다.

질문게시판HOME > 질문게시판 목록

MCU, AVR, 아두이노 등 전자공학에 관련된 질문을 무료회원가입 후 작성해주시면 전문가가 답변해드립니다.
ATMEGA128PWMLED초음파
아두이노AVR블루투스LCD
UART모터적외선ATMEGA
전체 스위치 센서
질문게시판 목록
제목 작성자 작성일 조회
질문 서보모터 드라이버 관련 질문입니다.댓글[1] 뚱보개 23-12-13 1110
질문 Atmega128 USART 질문입니다.댓글[1] dnjsjj 23-12-13 1327
질문 codevision avr atmega128a 사용중입니다 범뚱 23-12-12 1137
답변 답변글 답변 : codevision avr atmega128a 사용중입니다 master 23-12-13 1005
질문 아두이노 주파수 변경하는법댓글[1] 파란하늘 23-12-12 1257
질문 atmega 128 timer interrupt에 관한 질문 atmemem 23-12-12 887
답변 답변글 답변 : atmega 128 timer interrupt에 관한 질문 master 23-12-13 737
질문 Atmega128에서 시계코드에 스톱워치, 타이머를 추가하고 싶습니다. 주넘 23-12-11 1094
답변 답변글 답변 : Atmega128에서 시계코드에 스톱워치, 타이머를 추가하고 싶습니다. master 23-12-13 919
질문 atmega128에서 16x16 LDM에 좌측부분이 안나옵니다.댓글[1] 이미지 주넘 23-12-11 1414
질문 ATmega128 to ATmega128로 USART 통신 질문 alsdn 23-12-08 984
답변 답변글 답변 : ATmega128 to ATmega128로 USART 통신 질문 master 23-12-08 1074
질문 네오픽셀 Rainbow코드 LED주소 지정하는 법 문의 손문일 23-12-08 774
답변 답변글 답변 : 네오픽셀 Rainbow코드 LED주소 지정하는 법 문의댓글[2] master 23-12-08 1884
질문 아두이노 L9110 DC모터 제어댓글[1] wdsdd 23-12-08 1355
질문 Atmega128 CLCD와 블루투스 통신 문제댓글[1] fanfan 23-12-07 1956
답변 답변글 답변 : Atmega128 CLCD와 블루투스 통신 문제댓글[3] master 23-12-07 4924
질문 스마트팜 관련 질문댓글[3] 이미지첨부파일 생물공학도 23-12-07 3175
질문 아두이노 자판기 질문입니다댓글[1] 한세월두세월 23-12-06 1380
답변 답변글 답변 : 아두이노 자판기 질문입니다 master 23-12-07 893
질문 atmega128 질문있습니다댓글[1] 이미지첨부파일 아자자자자자 23-12-06 1829
질문 스텝모터 제어 및 온도센서 값 동시 추출 질문드립니다.댓글[1] metanoia 23-12-06 1638
질문 ATmega128에서 LCD와 블루투스 fanfan 23-12-05 1666
답변 답변글 답변 : ATmega128에서 LCD와 블루투스댓글[1] master 23-12-05 2663
질문 atmega128 압력센서 관해댓글[2] 메시기모찌 23-12-04 5146
질문 안녕하세요 심장박동센서를 이용해 일정 심박수 이상이되면 부저 작동에 대해서 질문드립니다.댓글[8] 충전공 23-12-04 49475
질문 atmega128 CLCD댓글[2] 릴를 23-12-03 5449
질문 atmega128 블루투스 CLCD댓글[3] 릴를 23-12-03 9128
질문 pwm을 이용한 led댓글[1] 이미지첨부파일 아자자자자자 23-12-02 2130
질문 스텝모터, 컬러센서를 이용한 아두이노댓글[3] 초식동물 23-12-02 7071
게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기