#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
void setup(void) {
u8g2.begin();
u8g2.enableUTF8Print(); // enable UTF8 support for the Arduino print() function
pinMode(6, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
u8g2.setFont(u8g2_font_shnufont);
u8g2.setFontDirection(0);
}
void loop(void) {
u8g2.clearBuffer();
if (analogRead(A3)<300)
{
u8g2.setCursor(0, 15);
u8g2.print("绿灯");
u8g2.sendBuffer();
noTone(tonepin);
}
else {
u8g2.setCursor(0, 40);
u8g2.print("红灯禁止通行");
u8g2.sendBuffer();
playSong(melody_motherland, duration_motherland, sizeof(melody_motherland) / sizeof(melody_motherland[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);
}
}