Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 39 additions & 49 deletions NFC_attach.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ void setup() {
mfrc522.PCD_DumpVersionToSerial();
Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));


display.clearDisplay();//一度初期化
display.setTextSize(2);// 出力する文字の大きさ
display.setTextColor(WHITE);//出力する文字の色
display.setCursor(0, 0);//カーソル位置の指定
display.println("Nocard");// ディスプレイへ表示する文字列
resetDisplay()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

何回も初期化すると思うので,画面の初期化処理を関数にする.関数の内容は

void resetDisplay () {
  display.clearDisplay();
  display.setCursor(0, 0);
  display.setTextColor(WHITE);
}

printlnText("Nocard", 2);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目的は

  • 何回か処理するので関数にまとめる
  • 画面に映す文字の内容と大きさを紐付けする
    の2点.関数の内容は
void printlnText (String text, int textSize) {
  display.setTextSize(textSize)
  display.println(text)
}

今回は display.println()display.print() のそれぞれに対応する関数を作っています

display.display();//ディスプレイ情報の更新


Expand All @@ -47,17 +43,35 @@ void setup() {


Message = "No mode";
NUMBER = "000";
NUMBER = 000;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8行目int NUMBER と宣言しているので,辻褄が合わないなというお気持ち

}

void resetDisplay () {
display.clearDisplay(); // 一度初期化
display.setCursor(0, 0); // 一度初期化
display.setTextColor(WHITE);
}

void printText (String text, int textSize) {
display.setTextSize(textSize)
display.print(text)
}

void printlnText (String text, int textSize) {
display.setTextSize(textSize)
display.println(text)
}

void loop() {
digitalWrite(3, HIGH); //初期設定
digitalWrite(4, LOW); //初期設定
noTone(6);//初期設定
if ( ! mfrc522.PICC_IsNewCardPresent()) return;// Mifareカードの確認(新しいカードが無ければ終了し、loop関数を繰り返す)
if ( ! mfrc522.PICC_ReadCardSerial()) return;// Mifareカードのデータ読み込み(読み取れなければ終了し、loop関数を繰り返す)

if (
! mfrc522.PICC_IsNewCardPresent() || // Mifareカードのデータ読み込み
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

論理演算子を用いて簡潔に.見た目もよくなるし,loop関数で分岐する回数が半分になるので処理的にも優しくなりまする

! mfrc522.PICC_ReadCardSerial()) { // Mifareカードの確認
return; // どちらかを満たさない場合はloop関数を繰り返す
}



Expand All @@ -72,15 +86,11 @@ void loop() {
Serial.println(strUID);


resetDisplay()

if ( strUID == UID ) {
display.clearDisplay();//一度初期化
display.setCursor(0, 0);//一度初期化
display.println("collect");
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("cardUID = ");
display.println(strUID);
printlnText("collect", 2);
printlnText("cardUID = " + strUID, 1);
display.display();

tone(6, 1700);
Expand All @@ -91,16 +101,9 @@ void loop() {
delay(500);

blank(strUID);
}

if ( !(strUID == UID) ) {
display.clearDisplay();//一度初期化
display.setCursor(0, 0);//一度初期化
display.println("error!");
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("cardUID = ");
display.println(strUID);
} else {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strUID == UID

  • 一致する
  • 一致しない
    の2種類の状況以外はありえないので, else を使った方が明示的.加えてメモリ消費も少なくなる.

printlnText("error!", 2);
printlnText("cardUID = " + strUID, 2);
display.display();

Serial.println("else");
Expand All @@ -109,11 +112,8 @@ void loop() {
tone(6, 500);
delay(1000);

display.clearDisplay();//一度初期化
display.setTextSize(2);// 出力する文字の大きさ
display.setTextColor(WHITE);//出力する文字の色
display.setCursor(0, 0);//カーソル位置の指定
display.println("Nocard");// ディスプレイへ表示する文字列
resetDisplay()
printlnText("Nocard", 2);
display.display();//ディスプレイ情報の更新
}
}
Expand All @@ -128,39 +128,29 @@ void blank(String strUID) {
pointAdd = digitalRead(A2);
submit = digitalRead(A3);

display.clearDisplay();//一度初期化
display.setTextSize(2);// 出力する文字の大きさ
display.setCursor(0, 0);//カーソル位置の指定
display.println(Message);// ディスプレイへ表示する文字
display.setTextSize(1);// 出力する文字の大きさ
display.print("NUMBER = ");
display.setTextSize(2);// 出力する文字の大きさ
display.println(NUMBER);
resetDisplay()
printlnText(Message, 2);
printText("NUMBER = ", 1);
printlnText(NUMBER, 2);
display.display();//ディスプレイ情報の更新
delay(300);


if ( mode <= 85 ) {
Message = "Reset";
}


if ( 85 < mode && mode < 170 ) {
} else if ( 85 < mode && mode < 170 ) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if を使おうの会

Message = "Point";
}


if ( 170 <= mode ) {
} else if ( 170 <= mode ) {
Message = "GameT";
}


if (pointDeduct == 0) {
NUMBER = NUMBER - 10;
NUMBER -= 10;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a = a - ba -= b と書き換えられるのだ!変数名を2回書かなくてもよくなる!!べんり!!!

}

if (pointAdd == 0) {
NUMBER = NUMBER + 10;
NUMBER += 10;
}

if (submit == 0) {
Expand Down