#include <U8g2lib.h>
#include "u8g2_font_shnufont.h"
#include "pitches.h"
#include "song.h"

int tonepin = 5;

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ A5, /* data=*/ A4, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display
//U8G2_R0(无旋转),U8G2_R1(顺时针90°),U8G2_R2(顺时针180°)

void setup(void) {
  u8g2.begin();
  u8g2.setFlipMode(0); //set flipmode to 0 in wokwi simulator;set flipmode to 1 on grove board
  u8g2.enableUTF8Print();		// enable UTF8 support for the Arduino print() function
  u8g2.setFont(u8g2_font_shnufont);
  u8g2.setFontDirection(0);
}

void loop(void) {

  u8g2.clearBuffer();
  if(analogRead(A0)<=1000){
    u8g2.setCursor(0, 40);
    u8g2.print("我和我的祖国");
    u8g2.sendBuffer();
    playSong(melody_motherland2, duration_motherland2, sizeof(melody_motherland2) / sizeof(melody_motherland2[0]));
  
  }

  delay(1000);
}

void playSong(int melody[], float duration[], int length)
{
  for (int thisNote = 0; thisNote < length; thisNote++) {
    int noteDuration = 250 * duration[thisNote];
    tone(tonepin, melody[thisNote], noteDuration);
    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.00;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    //noTone(tonepin);
  }
 
}