2022-06-22 팀프로젝트_리듬스타

2022. 6. 22. 15:01학부 강의/Arduino

팀별로 Tinkercad를 이용해서 게임을 만드는 프로젝트다.

 

https://www.tinkercad.com/things/6yDHTe0QRnq

 

Circuit design RhythmStar | Tinkercad

Circuit design RhythmStar created by tired_i with Tinkercad

www.tinkercad.com


발표자료

 

 


디지털 게임기 프로젝트 조사

디지털 게임기 프로젝트 조사.hwp
1.81MB


개발과정

 

회로

코드

#include<stdio.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_LEDBackpack.h>
#define STARTPOS 16

LiquidCrystal_I2C leftLcd(0x20, 16, 2);
LiquidCrystal_I2C rightLcd(0x21, 16, 2);

Adafruit_7segment seg = Adafruit_7segment();

byte char_all[8][8]{
{0x00, 0x18, 0x18, 0x18, 0x1F, 0x03, 0x16, 0x0C}//♪
, {0x00, 0x00, 0x018, 0x18, 0x18, 0x1F, 0x00, 0x00}//♩
, {0x00, 0x18, 0x1F, 0x01, 0x01, 0x19, 0x1F, 0x00}//♫
, {0x00, 0x00, 0x1C, 0x14, 0x14, 0x1C, 0x0F, 0x00}//𝅗𝅥
, {0x00, 0x04, 0x0C, 0x18, 0x18, 0x0C, 0x07, 0x01}//sucess 
, {0x00, 0x11, 0x1B, 0x0E, 0x04, 0x0E, 0x1B, 0x11}//fail
};

double SongDelay[3] = {1.2, 1.8, 1.2}; 
double tempo = 1; //falling speed
int song = 0;
int clock = 0; 
int score = 0;
int count = 0; 
int end[3] ={204, 139, 216};

///////////////////////////////////////////////////////////////////

class LcdLine {

  private :
  int lineNum;
  int pinNum;
  int row;


  public :

  int pos=STARTPOS;
  int able=0;
  int note=0;

  LcdLine(int lineNum) {
    this->lineNum = lineNum;
    if (lineNum < 2){
      if(lineNum == 0){
        this->row = 0;
        this->pinNum = 10;

      }
      else if(lineNum == 1){
        this->row = 1;
        this->pinNum = 9;
      }
    }
    else{
      if(lineNum == 2){
        this->row = 0;
        this->pinNum = 8;
      }
      else if(lineNum == 3){
        this->row = 1;
        this->pinNum = 7;
      }
    }
  }


  void print(char str[16], int col){
    if(lineNum < 2){
      leftLcd.setCursor(col, row); 
      leftLcd.print(str); 
    }else{
      rightLcd.setCursor(col, row);
      rightLcd.print(str);
    }
  }

  void write(int icon, int col){
    if(lineNum < 2){
      leftLcd.setCursor(col, row); 
      leftLcd.write(icon); 
    }else{
      rightLcd.setCursor(col, row);
      rightLcd.write(icon);
    }
  }

  void write(int icon){
    if(lineNum < 2){
      leftLcd.setCursor(pos, row); 
      leftLcd.write(icon); 
    }else{
      rightLcd.setCursor(pos, row);
      rightLcd.write(icon);
    }
  }

};

//////////////////////////////////////////////////////////////////

LcdLine Line0(0);
LcdLine Line1(1);
LcdLine Line2(2);
LcdLine Line3(3);
LcdLine Lines[4] = {Line0, Line1, Line2, Line3};

//stay

PROGMEM const uint16_t music0[] = {0, 184, 195, 293, 195, 
               0, 184, 195, 146, 
               0, 184, 195, 293, 195, 
               0, 184, 195, 0, 146, 164, 
               0, 184, 195, 293, 195, 
               0, 184, 195, 146, 
               0, 184, 195, 293, 195, 
               0, 184, 195, 0, 
               369, 391, 369, 587, 369, 369, 
               369, 369, 369, 369, 369, 391, 391, 
               369, 391, 369, 587, 369, 369, 
               369, 369, 369, 369, 369, 391, 391, 
               369, 391, 369, 587, 369, 369, 
               369, 369, 369, 369, 369, 391, 391, 
               369, 391, 369, 587, 391, 440, 
               440, 739, 587, 
               0, 329, 493, 440, 440, 
               0, 440, 391, 440, 440, 
               0, 329, 493, 440, 440, 
               0, 440, 391, 440, 440, 
               0, 329, 493, 440, 440, 
               0, 440, 391, 440, 440, 
               587, 493, 587, 493, 
               440, 440, 440, 493, 440, 391, 
               391, 329, 493, 
               523, 493, 440, 493, 
               391, 329, 493, 
               523, 493, 440, 493, 
               391, 329, 493, 
               523, 493, 440, 493, 
               587, 493, 587, 493, 
               440, 440, 440, 493, 440, 391, 
               369, 391, 369, 587, 369, 369, 
               369, 369, 369, 369, 369, 391, 391, 
               369, 391, 369, 587, 369, 369, 
               369, 369, 369, 369, 369, 391, 391, 
               369, 391, 369, 587, 369, 369, 
               369, 369, 369, 369, 369, 391, 391, 
               369, 391, 369, 587, 391, 440, 
                                   440, 739, 587}; /*, 
               369, 391, 369, 587, 369, 369, 
               369, 369, 369, 369, 369, 391, 391, 
               369, 391, 369, 587, 369, 369, 
               369, 369, 369, 369, 369, 391, 391, 
               369, 391, 369, 587, 369, 369, 
               369, 369, 369, 369, 369, 391, 391, 
               369, 391, 369, 587, 369, 369, 
               440, 739, 587};*/

PROGMEM const uint16_t mDelay0[] = {125, 125, 125, 375, 250,
                250, 125, 375, 250, 
                125, 125, 125, 375, 250, 
                250, 125, 125, 250, 125, 125, 
                125, 125, 125, 375, 250, 
                250, 125, 375, 250, 
                125, 125, 125, 375, 250, 
                250, 125, 375, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 125, 125, 125, 125, 125, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 125, 125, 125, 125, 125, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 125, 125, 125, 125, 125, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 375, 500, 
                125, 125, 250, 375, 125, 
                125, 250, 125, 250, 250, 
                125, 125, 250, 375, 125, 
                125, 250, 125, 250, 250, 
                125, 125, 250, 375, 125, 
                125, 250, 125, 250, 250, 
                250, 250, 250, 250, 
                125, 125, 125, 125, 250, 250, 
                625, 125, 500, 
                125, 375, 125, 125, 
                625, 125, 500, 
                125, 375, 125, 125, 
                625, 125, 500, 
                125, 375, 125, 125, 
                250, 250, 250, 250, 
                125, 125, 125, 125, 250, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 125, 125, 125, 125, 125, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 125, 125, 125, 125, 125, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 125, 125, 125, 125, 125, 250, 
                125, 125, 125, 375, 125, 125, 
                                    125, 375, 500};/*, 
                125, 125, 125, 375, 125, 125, 
                125, 125, 125, 125, 125, 125, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 125, 125, 125, 125, 125, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 125, 125, 125, 125, 125, 250, 
                125, 125, 125, 375, 125, 125, 
                125, 375, 500};*/

//dolphin
PROGMEM const uint16_t music1[] = {261, 261, 261, 261, 293, 0, 220, 220, 220, 207, 195, 0, //12
               261, 261, 195, 261, 261, 195, 195, 261, 261, 195, 261, 261, 195, 195, 261, 261, 195, 0,   261, 329, 329, 261,//22
               261, 261, 195, 261, 261, 195, 195, 261, 261, 195, 261, 261, 195, 195, 261, 261, 195, 0,   261, 329, 329, 261,//22
               0,   261, 261, 246, 0,   293, 293, 261, 220, 220, 195, 329, 329, 293, 0  , 293, 349, 329, 261,//19
               261, 391, 329, 391, 329, 391, 391, 329, 440, 0,   0,   195, 261, 195, 261, 195, 261, 195, 261,//19
               261, 261, 261, 293, 0  , 220, 220, 220, 207, 195, 0,   261, 261, 261, 293, 195, 261, 195, 261, 195, 261, 195, 261,//23
               261, 261, 261, 293, 0  , 220, 220, 220, 207, 195, 0,   261, 261, 261, 293, 195, 261, 195, 261, 195, 261, 195, 261,//23
              };

PROGMEM const uint16_t mDelay1[] = {250, 125, 125, 250, 125, 62 , 187, 125, 125, 125, 250, 250,
                125, 125, 250, 125, 62,  187, 125, 125, 125, 250, 125, 62,  187, 125, 125, 125, 250, 500, 250, 250, 250, 250,
                125, 125, 250, 125, 62,  187, 125, 125, 125, 250, 125, 62,  187, 125, 125, 125, 250, 500, 250, 250, 250, 250,
                500,250, 125, 375, 250, 250, 125, 250, 125, 125, 125, 250, 125, 375, 250, 125, 125, 250,  250, 
                125, 250, 250, 250, 250, 250, 250, 125, 375, 250, 375, 125, 125, 125, 125, 125, 125, 125,  250,
                250, 250, 250, 125, 62,  187, 125, 125, 125, 250, 250, 250, 250, 250, 125, 125, 125, 125,  125, 125, 125, 125, 250,
                250, 250, 250, 125, 62,  187, 125, 125, 125, 250, 250, 250, 250, 250, 125, 125, 125, 125,  125, 125, 125, 125, 250,

               };

//rooftop
PROGMEM const uint16_t music2[] = {195, 174, 
               195, 220, 261, 220, 195, 174, 
               195, 220, 195, 174, 146, 0, 174, 146, 
               195, 220, 261, 220, 195, 174, 
               195, 220, 195, 174, 220, 220, 220, 220, 
               220, 220, 146, 0, 220, 195, 220, 195, 220, 195, 
               220, 195, 220, 0, 523, 783, 880, 783, 698, 698, 
               783, 0, 220, 261, 220, 195, 174, 
               195, 220, 195, 174, 220, 391, 349, 
               391, 440, 523, 440, 391, 349, 
               391, 440, 391, 349, 293, 349, 293, 
               391, 440, 523, 440, 391, 349, 
               391, 440, 391, 349, 440, 440, 440, 440, 
               440, 440, 293, 0, 440, 391, 440, 391, 440, 391, 
               440, 391, 440, 523, 0, 349, 391, 349, 
               391, 440, 523, 440, 391, 349, 
               391, 440, 391, 349, 293, 0, 146, 
               220, 220, 220, 195, 220, 195, 195, 195, 195, 
               195, 195, 195, 174, 195, 146, 174, 146, 
               220, 220, 220, 146, 195, 195, 195, 195, 
               195, 195, 195, 261, 220, 195, 0, 195, 
               195, 195, 220, 195, 195, 195, 220, 195, 
               195, 195, 195, 174, 195, 220, 174, 0, 220, 
               220, 220, 220, 220, 195, 195, 220, 195, 174, 
               195, 195, 195, 174, 195, 220, 174, 523, 523, 523, 
               523, 587, 698, 783, 349, 349, 
               349, 261, 220, 233, 220, 523, 523, 523, 
               523, 587, 698, 783, 493, 523, 440, 
               523, 440, 523, 440, 391};

PROGMEM const uint16_t mDelay2[] = {125, 125, 
                250, 250, 125, 125, 125, 125, 
                125, 125, 125, 62, 187, 125, 125, 125, 
                250, 250, 125, 125, 125, 125, 
                125, 125, 125, 62, 187, 125, 125, 125, 
                125, 250, 125, 125, 62, 62, 62, 62, 62, 62, 
                62, 62, 125, 125, 125, 125, 62, 187, 62, 187, 
                125, 125, 125, 125, 125, 125, 125, 
                125, 125, 125, 62, 312, 125, 125, 
                250, 250, 125, 125, 125, 125, 
                125, 125, 125, 62, 312, 125, 125, 
                250, 250, 125, 125, 125, 125, 
                125, 125, 125, 62, 187, 125, 125, 125, 
                125, 250, 125, 125, 62, 62, 62, 62, 62, 62, 
                62, 62, 250, 125, 125, 125, 125, 125, 
                250, 250, 125, 125, 125, 125, 
                125, 125, 125, 62, 312, 125, 125, 
                125, 125, 125, 62, 62, 125, 125, 125, 125, 
                125, 125, 125, 62, 62, 250, 125, 125, 
                125, 125, 125, 125, 125, 125, 125, 125, 
                125, 125, 125, 125, 125, 125, 125, 125, 
                125, 125, 125, 125, 125, 125, 125, 125, 
                125, 125, 62, 62, 62, 62, 250, 125, 125, 
                125, 125, 125, 125, 125, 125, 125, 62, 62, 
                125, 125, 62, 62, 62, 62, 125, 125, 125, 125, 
                187, 187, 187, 187, 125, 125, 
                125, 125, 125, 62, 187, 125, 125, 125, 
                187, 187, 187, 187, 62, 125, 125, 
                187, 187, 187, 187, 250};

unsigned int music_buffer[217];
unsigned int mDelay_buffer[217];

//////////////////////////////////////////////////////////////////
void setup()
{
  Serial.begin(9600);
  seg.begin(0x70);
  randomSeed(analogRead(0));

  for(int i = 10; i > 6; i--){
    pinMode(i, INPUT);
  }

  leftLcd.init();
  rightLcd.init();
  leftLcd.backlight();
  rightLcd.backlight();

  for(int i=0; i<6; i++){
    leftLcd.createChar(i, char_all[i]);
    rightLcd.createChar(i,char_all[i]);
  }

  //start
  Line0.print("welcome to",3);

  Line1.print("RHYTHM STAR!",2);

  Line3.print("press any key!",1);

  int press = 1;
  clock = 0;
  while(press){
    if(clock==100)
    {
      rightLcd.noDisplay();
      clock++;
    }
    else if(clock==200)
    {
      rightLcd.display();
      clock=0;
    }
    else
    {
    clock++;
    }

    if(digitalRead(10)||digitalRead(9)||digitalRead(8)||digitalRead(7))
    {
      press = 0;
      clock = 0;
    }
  }
  //select song

  rightLcd.display();
  leftLcd.clear();
  rightLcd.clear();
  Line0.print("CHOOSE SONG : ",0);
  Line1.print("STAY", 4);
  Line2.print("DOLPHIN", 4);
  Line3.print("ROOFTOP",4);

  press=1;
  clock=599;
  while(clock>0){
    char timeout[2];
    sprintf(timeout, "%d", (clock/100));  
    Line0.print(timeout, 14);
    if (digitalRead(9))
    {
      Line1.write(0,2);
      Line2.print(" ", 2);
      Line3.print(" ", 2);
      song = 0;
    }
    if (digitalRead(8))
    {
      Line1.print(" ", 2);
      Line2.write(0, 2);
      Line3.print(" ", 2);
      song = 1;
    }
    else if(digitalRead(7))
    {
      Line1.print(" ",2);
      Line2.print(" ", 2);
      Line3.write(0, 2);
      song = 2;
    }
    clock-=2;
  }

  //select difficulty
  rightLcd.display();
  leftLcd.clear();
  rightLcd.clear();
  Line0.print("RHYTHM STAR!",2);
  Line2.print("EASY", 6);
  Line3.print("HARD", 6);
  delay(300);

  press=1;
  clock=599;

  while(clock>0){
    char timeout[2];
    sprintf(timeout, "%d", (clock/100));  
    Line1.print(timeout, 8);
    if(digitalRead(8))
    {
      Line3.print(" ", 4);
      Line2.write(0, 4);
      tempo=1; 
    }
    else if(digitalRead(7))
    {
      Line2.print(" ", 4);
      Line3.write(0, 4);
      tempo = 0.75; 
    }
    clock-=2;
  }

  if(song == 0){
    for(int i = 0; i <= end[song]; i++){
      music_buffer[i] = pgm_read_word_near( music0+ i);
      mDelay_buffer[i] = pgm_read_word_near( mDelay0+ i);
    }
  }
  else if(song == 1){
    for(int i = 0; i <= end[song]; i++){
      music_buffer[i] = pgm_read_word_near( music1+ i);
      mDelay_buffer[i] = pgm_read_word_near( mDelay1+ i);
    }
  }
  else{
    for(int i = 0; i <= end[song]; i++){
      music_buffer[i] = pgm_read_word_near( music2+ i);
      mDelay_buffer[i] = pgm_read_word_near( mDelay2+ i);
    }
  }

}

//////////////////////////////////////////////////////////////////////
void loop()
{ 
  leftLcd.clear();
  rightLcd.clear();

  //make new note
  int rnd = random(0,7);
  if(rnd < 4 && Lines[rnd].able == 0){
    Lines[rnd].write(rnd);
    Lines[rnd].note=rnd;
    Lines[rnd].able = 1;
  }

  for(int i = 0; i<4; i++){
    if(Lines[i].able == 0){continue;}
    if(Lines[i].pos == 0 && digitalRead(10-i))
    {
      Lines[i].write(4);
      score+=10;
      Lines[i].pos = STARTPOS;
      Lines[i].able = 0;
    }else if(Lines[i].pos == 0){
      Lines[i].write(5);
      score--;
      Lines[i].pos = STARTPOS;
      Lines[i].able = 0;
    }else{ 
      Lines[i].write(Lines[i].note);
      Lines[i].pos--; 
    }
  }

  tone(3, (int)music_buffer[count] ,(int)mDelay_buffer[count]*tempo);
  delay((int)mDelay_buffer[count]* SongDelay[song] * tempo);
  noTone(3);

  //show score
  seg.print(score, DEC);
  seg.writeDisplay();

  //ending & rebooting
  if(count >= end[song]){
    int icon = 0;
    for(int col = 15; col >-1; col--){
      for(int j = 0; j<4;j++){
        Lines[j].write(icon, col);
        icon++;
        if(icon > 5){icon = 0;}
        delay(50);
      }
    }
    leftLcd.clear();
    rightLcd.clear();
    seg.blinkRate(1);
    delay(5000);
    pinMode(12, OUTPUT);
    digitalWrite(12, HIGH);
  }  

  //make count
  count++;
}

 


Adafruit 7-Segment library

 

  • print(variable,DEC) or print(variable) : this will print a decimal integer, from 0000 up to 9999
Adafruit_7segment seg = Adafruit_7segment();
void setup() {
  seg.begin(0x70);
}
seg.print(1234);
seg.writeDisplay();
seg.blinkRate(1);

객체 사용

 

class LcdLine {

  private :

  int lineNum;
  int pinNum;
  int row;


  public :

  int pos=STARTPOS;
  int able=0;
  int note=0;

  LcdLine(int lineNum) {
    this->lineNum = lineNum;
    if (lineNum < 2){
      if(lineNum == 0){
        this->row = 0;
        this->pinNum = 10;

      }
      else if(lineNum == 1){
        this->row = 1;
        this->pinNum = 9;
      }
    }
    else{
      if(lineNum == 2){
        this->row = 0;
        this->pinNum = 8;
      }
      else if(lineNum == 3){
        this->row = 1;
        this->pinNum = 7;
      }
    }
  }

  void print(char str[16], int col){
    if(lineNum < 2){
      leftLcd.setCursor(col, row); 
      leftLcd.print(str); 
    }else{
      rightLcd.setCursor(col, row);
      rightLcd.print(str);
    }
  }

  void write(int icon, int col){
    if(lineNum < 2){
      leftLcd.setCursor(col, row); 
      leftLcd.write(icon); 
    }else{
      rightLcd.setCursor(col, row);
      rightLcd.write(icon);
    }
  }

  void write(int icon){
    if(lineNum < 2){
      leftLcd.setCursor(pos, row); 
      leftLcd.write(icon); 
    }else{
      rightLcd.setCursor(pos, row);
      rightLcd.write(icon);
    }
  }

};

LCD를 효과적으로 제어하기 위해서 객체를 사용한다.

LcdLine Line0(0);
LcdLine Line1(1);
LcdLine Line2(2);
LcdLine Line3(3);

LcdLine Lines[4] = {Line0, Line1, Line2, Line3};
...
LcdLine &LineN = Lines[i];

객체 생성 후 배열에 넣음.

 

참고 : https://yeolco.tistory.com/114


음표 생성 위치(난수 생성)

#include <random>

int main()
{
    // 시드값을 얻기 위한 random_device 생성.
    random_device rd;

    // random_device 를 통해 난수 생성 엔진을 초기화 한다.
    mt19937 gen(rd());

    // 0 부터 99 까지 균등하게 나타나는 난수열을 생성하기 위해 균등 분포 정의.
    uniform_int_distribution<int> dis(0, 3);
}

//이제 dis(gen)을 사용하면 0과 3사이 난수를 획득할 수 있다.

어림도 없지 Tinkercad에는 헤더 파일이 없지롱~

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
  srand(time(NULL));

  for (int i = 0; i < 5; i++) {
    printf("난수 : %d \n", rand() % 4);
  }
  return 0;
}

아 그냥 랜덤 함수를 지원하네;;

 

long randNumber;

void setup() {
  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
}

void loop() {
  // 0 에서 299 사이 임의 값 출력
  randNumber = random(300);
  Serial.println(randNumber);

  // 10 에서 19 사이 임의 값 출력
  randNumber = random(10, 20);
  Serial.println(randNumber);

  delay(50);
}
void setup()
{
    ...
  randomSeed(analogRead(0));
    ...
}

void loop()
{
    ...
    //make new note
    int rnd = random(0, 7);
    if(rnd < 4 && Lines[rnd].able == 0){
        Lines[rnd].write(rnd);
        Lines[rnd].note = rnd;
        Lines[rnd].able = 1;
    ...
    }

출처 : https://www.arduino.cc/reference/ko/language/functions/random-numbers/random/

출처 : https://velog.io/@t1won/C-11-난수-생성-random-라이브러리


용량 부족

와 아두이노 우노의 SRAM의 용량은 고작 2KB다.

 

노래를 배열로 넣다 보니 공간이 부족하다.

 

반면에 플래시 메모리는 32KB로 여유가 있다.

 

그래서 플래시 메모리를 이용하기로 했다.

 

//dolphin
PROGMEM const uint16_t music1[] = {261, 261, 261, 261, 293, 0, 220, 220, 220, 207, 195, 0, //12
               261, 261, 195, 261, 261, 195, 195, 261, 261, 195, 261, 261, 195, 195, 261, 261, 195, 0,   261, 329, 329, 261,//22
               261, 261, 195, 261, 261, 195, 195, 261, 261, 195, 261, 261, 195, 195, 261, 261, 195, 0,   261, 329, 329, 261,//22
               0,   261, 261, 246, 0,   293, 293, 261, 220, 220, 195, 329, 329, 293, 0  , 293, 349, 329, 261,//19
               261, 391, 329, 391, 329, 391, 391, 329, 440, 0,   0,   195, 261, 195, 261, 195, 261, 195, 261,//19
               261, 261, 261, 293, 0  , 220, 220, 220, 207, 195, 0,   261, 261, 261, 293, 195, 261, 195, 261, 195, 261, 195, 261,//23
               261, 261, 261, 293, 0  , 220, 220, 220, 207, 195, 0,   261, 261, 261, 293, 195, 261, 195, 261, 195, 261, 195, 261//23
              };

PROGMEM const uint16_t mDelay1[] = {250, 125, 125, 250, 125, 62 , 187, 125, 125, 125, 250, 250,
                125, 125, 250, 125, 62,  187, 125, 125, 125, 250, 125, 62,  187, 125, 125, 125, 250, 500, 250, 250, 250, 250,
                125, 125, 250, 125, 62,  187, 125, 125, 125, 250, 125, 62,  187, 125, 125, 125, 250, 500, 250, 250, 250, 250,
                500,250, 125, 375, 250, 250, 125, 250, 125, 125, 125, 250, 125, 375, 250, 125, 125, 250,  250, 
                125, 250, 250, 250, 250, 250, 250, 125, 375, 250, 375, 125, 125, 125, 125, 125, 125, 125,  250,
                250, 250, 250, 125, 62,  187, 125, 125, 125, 250, 250, 250, 250, 250, 125, 125, 125, 125,  125, 125, 125, 125, 250,
                250, 250, 250, 125, 62,  187, 125, 125, 125, 250, 250, 250, 250, 250, 125, 125, 125, 125,  125, 125, 125, 125, 250
               };
unsigned int music_buffer[217];
unsigned int mDelay_buffer[217];
void setup() {
    ...
        if(song == 0){
        for(int i = 0; i <= end[song]; i++){
          music_buffer[i] = pgm_read_word_near( music0+ i);
          mDelay_buffer[i] = pgm_read_word_near( mDelay0+ i);
        }
      }
      else if(song == 1){
        for(int i = 0; i <= end[song]; i++){
          music_buffer[i] = pgm_read_word_near( music1+ i);
          mDelay_buffer[i] = pgm_read_word_near( mDelay1+ i);
        }
      }
      else{
        for(int i = 0; i <= end[song]; i++){
          music_buffer[i] = pgm_read_word_near( music2+ i);
          mDelay_buffer[i] = pgm_read_word_near( mDelay2+ i);
        }
      }
    ...
}
void loop(){
    ...
    tone(3, (int)music_buffer[count] ,(int)mDelay_buffer[count]*tempo);
      delay((int)mDelay_buffer[count]* SongDelay[song] * tempo);
      noTone(3);
    ...
}

PROGMEM const uint16_t

  • 프로그램 메모리에 저장할 경우 몇몇 자료형은 저장하는 것을 지원하지 않는다.

 

  • 프로그램 메모리을 사용할 시에는 오직 읽기만 가능하다. 그래서 상수 선언 const을 한다.

 

출처 : https://blog.naver.com/PostView.naver?blogId=jamespiggy&logNo=221839136191&parentCategoryNo=&categoryNo=49&viewDate=&isShowPopularPosts=true&from=search

출처 : https://juahnpop.tistory.com/120

출처 : https://www.hardcopyworld.com/?p=1189


아... 시험 전날까지 조별과제라니 강호의 도리가 사라졌네...

'학부 강의 > Arduino' 카테고리의 다른 글

2022-05-27 Arduino_13  (0) 2022.05.27
2022-05-23 Arduino_12  (0) 2022.05.23
2022-05-21 Arduino_11  (0) 2022.05.21
2022-05-17 Arduino_10  (0) 2022.05.17
2022-05-12 Arduino_9  (0) 2022.05.12