基于arduino的蜂鸣器播放器及其上位机程序

lzusa 发布于 2020-10-18 616 次阅读


#include <avr/pgmspace.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
#define NTC0 -1
#define NTC1 262
#define NTC2 294
#define NTC3 330
#define NTC4 350
#define NTC5 393
#define NTC6 441
#define NTC7 495
#define NTCL5 196
#define NTCL6 221
#define NTCL7 248   
#define NTCH1 525
#define NTCH2 587
#define NTCH3 659
#define NTCH4 698
#define NTCH5 784
#define NTCH6 880
#define NTCH7 988

#define NTD0 -1
#define NTD1 294
#define NTD2 330
#define NTD3 350
#define NTD4 393
#define NTD5 441
#define NTD6 495
#define NTD7 556

#define NTDL1 147
#define NTDL2 165
#define NTDL3 175
#define NTDL4 196
#define NTDL5 221
#define NTDL6 248
#define NTDL7 278

#define NTDH1 589
#define NTDH2 661
#define NTDH3 700
#define NTDH4 786
#define NTDH5 882
#define NTDH6 990
#define NTDH7 112

#define NTE1 330
#define NTE2 350
#define NTE3 393
#define NTE4 441
#define NTE5 495
#define NTE6 556
#define NTE7 624

#define NTEL5 248

const short tune1[] PROGMEM =     //dayu       
{
  NTC3,NTC2,NTC3,NTC6,NTC3,NTC2,NTC3,NTC7,
  NTC3,NTC2,NTC3,NTCH1,NTC7,NTC5,
  NTC3,NTC2,NTC3,NTC6,NTC3,NTC2,NTC3,NTC7,
  NTC5,NTC2,NTCL7,
  NTC3,NTC2,NTC3,NTC6,NTC3,NTC2,NTC3,NTC7,
  NTC3,NTC2,NTC3,NTCH1,NTC7,NTC5,
  NTC2,NTC3,NTCL6,NTC2,NTC3,NTCL6,NTCL5,
  NTCL6,NTC1,NTCL7,

  NTCL6,NTC1,NTC1,NTC2,NTC2,NTC3,NTC3,NTC5,NTC6,
  NTC5,NTC3,NTC2,
  NTCL6,NTC1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MEM = //tiger
{
  NTC1,NTC2,NTC3,NTC1,NTC1,NTC2,NTC3,NTC1,NTC3,NTC4,NTC5,
  NTC3,NTC4,NTC5,NTC5,NTC6,NTC5,NTC4,NTC3,NTC1,NTC5,NTC6,
  NTC5,NTC4,NTC3,NTC1,NTC2,NTCL5,NTC1,NTC2,NTCL5,NTC1
}; 

const int durt1[] PROGMEM =                   
{
  250,250,250,250,250,250,250,250,
  250,250,250,250,500,500,
  250,250,250,250,250,250,250,250,
  500,500,1000,
  250,250,250,250,250,250,250,250,
  250,250,250,250,500,500,
  250,250,500,250,250,250,250,500,500,1000,

  250,250,250,250,250,250,250,125,125,
  750,250,1000,
  250,250,250,250,250,250,500,500,1500,
  250,250,250,250,250,250,250,125,125,
  750,250,1000,
  250,250,500,250,250,250,250,1500,

  250,250,750,250,500,250,250,
  750,250,500,250,250,500,250,250,250,250,500,500,1000,
  250,250,875,125,500,250,250,500,500,1000,
  250,250,500,250,250,250,250,1500,

  250,250,750,250,500,250,250,
  500,250,250,500,250,250,500,250,250,250,250,250,250,1500,
  250,250,750,250,500,250,250,
  500,250,250,1000,250,250,500,250,250,250,250,2000,

};

const int durt2[] PROGMEM =                   
{
  500,500,500,500,
  500,500,500,500,
  500,500,500,500,
  750,250,500+500,
  500,500,500,500,
  500,500,500,500,
  500,500,500,500,
  750,250,1000,
  500,500,500,500,
  500,250,250,500,500,
  500,250,250,500,500,
  500,500,500,500,
  500,500,500,500,
  500,500,500,250,250,
  500,500,500,500,
  750,250,500+500,
};

const int durt3[] PROGMEM = 
{  
  500,500,500,250,250,     750,250,500,500,     500,500,500,250,250,
  750,250,500,500,     500,500,500,500,          750,250,500,500, 
  500,500,250,250,250,250,    750,250,500,500,     500,500,500,250,250,
  750,250,500,500,    500,500,500,250,250,     750,250,500,500,
  500,500,500,250,250,    500,250,125,125,125,250,    250,250,250,125,250,500,
  250,250,250,250,500,500,    500,500,500,250,250,    750,250,500,500,
  500,500,500,250,250,    750,250,500,500,    500,500,500,500,
  250,250,500,500,250,250,    750,125,250,500,    500,500,500,500,
  500,500,500,500,    500,500,500,500,    250,250,500,500,250,250,
  500,250,250,500,500,    500,500,500,500,    500,500,500,500,
  250,250,500,500,250,250,    500,250,125,250,500,    500,500,500,250,250
};

const int durt4[] PROGMEM =
{
  250,250,500,500,500,1000,
  250,250,500,500,500,1000,
  250,250,500,500,500,500,500,
  250,250,500,500,500,1000,
};  

const int durt5[] PROGMEM =
{
  500,500,500,500,500,500,500,500,500,500,1000,
  500,500,1000,250,250,250,250,500,500,250,250,250,250,   500,500,
  500,500,1000,500,500,1000
};  

int now = 1, t;
bool play = 1;
int length1, length2, length3, length4, length5; 
char buttonPin_stop = 8;
char buttonPin_pre = 2;
char buttonPin_next = 7;
char tonePin = 12;
char led = 5;

void setup() {
  // put your setup code here, to run once:
  length1 = sizeof(tune1)/sizeof(tune1[0]);
  length2 = sizeof(tune2)/sizeof(tune2[0]);
  length3 = sizeof(tune3)/sizeof(tune3[0]);
  length4 = sizeof(tune4)/sizeof(tune4[0]);
  length5 = sizeof(tune5)/sizeof(tune5[0]);
  pinMode(tonePin, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(buttonPin_stop, INPUT_PULLUP);
  pinMode(buttonPin_pre, INPUT_PULLUP);
  pinMode(buttonPin_next, INPUT_PULLUP);
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  display.setTextColor(WHITE);
  display.setTextSize(1);  
  display.setCursor(0,0);  
}

void loop() {
  delay(500);
  // put your main code here, to run repeatedly:
  Serial.println("Song List");
  Serial.println("Da Yu");
  Serial.println("City of Sky");
  Serial.println("Ode to Joy");
  Serial.println("Happy Birthday");
  Serial.println("Tow Tigers");


  while(1)
  {
        delay(2000);
        if (now == 1)
        {
          char f = '0';
          Serial.println("Now: Da Yu");
          display.clearDisplay();
          display.setCursor(0,0);
          display.println("Now: Da Yu");
          display.println("Next: City of Sky");
          display.println("pre    stop    next");
          display.display();
          for (int i = 0; i < length1; i++)
          {
            if (Serial.available())
            {
              f = Serial.read();
              delay(100);
            }
            if (digitalRead(buttonPin_stop) == LOW || f == '1')
            {
              play = !play;
              if (!play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: Da Yu");
                display.println("Next: City of Sky");
                display.println("pre    start    next");
                display.display();
              }
              if (play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: Da Yu");
                display.println("Next: City of Sky");
                display.println("pre    stop    next");
                display.display();
              }
              delay(500);
              t = i;
              f = 0;
            }
            if (digitalRead(buttonPin_next) == LOW || f == '3')
            {
              now ++;
              delay(500);
              break;
            }
            if (digitalRead(buttonPin_pre) == LOW || f == '2')
            {
              now = 5;
              delay(500);
              break;
            }
            if (play)
            {
              tone(tonePin, pgm_read_word(&tune1[i]));

              analogWrite(led, pgm_read_word(&tune1[i])*255/1000);
              delay(pgm_read_word(&durt1[i])*0.8);
              digitalWrite(led, LOW);
              delay(pgm_read_word(&durt1[i])*0.2);
              noTone(tonePin);
            }
            else
            {
              i = t;
            }
          }
        }
         if (now == 2)
        {
          char f = '0';
          Serial.println("Now: City of Sky");
          display.clearDisplay();
          display.setCursor(0,0);
          display.println("Now: City of Sky");
          display.println("Next: Ode to Joy");
          display.println("pre    stop    next");
          display.display();
          for (int i = 0; i < length2; i++)
          {
            if (Serial.available())
            {
              f = Serial.read();
              delay(100);
            }
            if (digitalRead(buttonPin_stop) == LOW || f == '1')
            {
              play = !play;
              if (!play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: City of Sky");
                display.println("Next: Ode to Joy");
                display.println("pre    start    next");
                display.display();
              }
              if (play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: City of Sky");
                display.println("Next: Ode to Joy");
                display.println("pre    stop    next");
                display.display();
              }
              delay(500);
              t = i;
              f = 0;
            }
            if (digitalRead(buttonPin_next) == LOW || f == '3')
            {
              now ++;
              delay(500);
              break;
            }
            if (digitalRead(buttonPin_pre) == LOW || f == '2')
            {
              now --;
              delay(500);
              break;
            }
            if (play)
            {
              tone(tonePin, pgm_read_word(&tune2[i]));
              analogWrite(led, pgm_read_word(&tune2[i])*255/1000);
              delay(pgm_read_word(&durt2[i])*0.8);
              digitalWrite(led, LOW);
              delay(pgm_read_word(&durt2[i])*0.2);
              noTone(tonePin);
            }
            else
            {
              i = t;
            }
          }
        }
         if (now == 3)
        {
          char f = '0';
          Serial.println("Now: Ode To Joy");
          display.clearDisplay();
          display.setCursor(0,0);
          display.println("Now: Ode To Joy");
          display.println("Next: Happy Birthday");
          display.println("pre    stop    next");
          display.display();
          for (int i = 0; i < length3; i++)
          {
            if (Serial.available())
            {
              f = Serial.read();
              delay(100);
            }
            if (digitalRead(buttonPin_stop) == LOW || f == '1')
            {

              play = !play;
              if (!play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: Ode To Joy");
                display.println("Next: Happy Birthday");
                display.println("pre    start    next");
                display.display();
              }
              if (play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: Ode To Joy");
                display.println("Next: Happy Birthday");
                display.println("pre    stop    next");
                display.display();
              }
              delay(500);
              t = i;
              f = 0;
            }
            if (digitalRead(buttonPin_next) == LOW || f == '3')
            {
              now ++;
              delay(500);
              break;
            }
            if (digitalRead(buttonPin_pre) == LOW || f == '2')
            {
              now --;
              delay(500);
              break;
            }
            if (play)
            {
              tone(tonePin, pgm_read_word(&tune3[i]));
              analogWrite(led, pgm_read_word(&tune3[i])*255/1000);
              delay(pgm_read_word(&durt3[i])*0.8);
              digitalWrite(led, LOW);
              delay(pgm_read_word(&durt3[i])*0.2);
              noTone(tonePin);
            }
            else
            {
              i = t;
            }
          }
        }
         if (now == 4)
        {
          char f = '0';
          Serial.println("Now: Happy Birthday");
          display.clearDisplay();
          display.setCursor(0,0);
          display.println("Now: Happy Birthday");
          display.println("Next: Two Tigers");
          display.println("pre    stop    next");
          display.display();
          for (int i = 0; i < length4; i++)
          {
            if (Serial.available())
            {
              f = Serial.read();
              delay(100);
            }
            if (digitalRead(buttonPin_stop) == LOW || f == '1')
            {
              play = !play;
              if (!play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: Happy Birthday");
                display.println("Next: Two Tigers");
                display.println("pre    start    next");
                display.display();
              }
              if (play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: Happy Birthday");
                display.println("Next: Two Tigers");
                display.println("pre    stop    next");
                display.display();
              }
              delay(500);
              t = i;
              f = 0;
            }
            if (digitalRead(buttonPin_next) == LOW || f == '3')
            {
              now ++;
              delay(500);
              break;
            }
            if (digitalRead(buttonPin_pre) == LOW || f == '2')
            {
              now --;
              delay(500);
              break;
            }
            if (play)
            {
              tone(tonePin, pgm_read_word(&tune4[i]));
              analogWrite(led, pgm_read_word(&tune4[i])*255/1000);
              delay(pgm_read_word(&durt4[i])*0.8);
              digitalWrite(led, LOW);
              delay(pgm_read_word(&durt4[i])*0.2);
              noTone(tonePin);
            }
            else
            {
              i = t;
            }
          }
        }
         if (now == 5)
        {
          char f = '0';
          Serial.println("Now: Two Tigers");
          display.clearDisplay();
          display.setCursor(0,0);
          display.println("Now: Two Tigers");
          display.println("Next: Da Yu");
          display.println("pre    stop    next");
          display.display();
          for (int i = 0; i < length5; i++)
          {
            if (Serial.available())
            {
              f = Serial.read();
              delay(100);
            }
            if (digitalRead(buttonPin_stop) == LOW || f == '1')
            {
              play = !play;
              if (!play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: Two Tigers");
                display.println("Next: Da Yu");
                display.println("pre    start    next");
                display.display();
              }
              if (play)
              {
                display.clearDisplay();
                display.setCursor(0,0);
                display.println("Now: Two Tigers");
                display.println("Next: Da Yu");
                display.println("pre    stop    next");
                display.display();
              }
              delay(500);
              t = i;
              f = 0;
            }
            if (digitalRead(buttonPin_next) == LOW || f == '3')
            {
              now = 1;
              delay(500);
              break;
            }
            if (digitalRead(buttonPin_pre) == LOW || f == '2')
            {
              now --;
              delay(500);
              break;
            }
            if (play)
            {
              tone(tonePin, pgm_read_word(&tune5[i]));
              analogWrite(led, pgm_read_word(&tune5[i])*255/1000);
              delay(pgm_read_word(&durt5[i])*0.8);
              digitalWrite(led, LOW);
              delay(pgm_read_word(&durt5[i])*0.2);
              noTone(tonePin);
            }
            else
            {
              i = t;
            }
          }

      }
  }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Windows.Forms;

namespace _9600
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] baud = {"9600"};
            comboBox2.Items.AddRange(baud);
            comboBox2.Text = "9600";
            comboBox1.Text = "COM3";
            comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
            comboBox1.Enabled = false;
            comboBox2.Enabled = false;
            serialPort1.PortName = comboBox1.Text;
            serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
            serialPort1.DataBits = 8;
            serialPort1.Parity = System.IO.Ports.Parity.None;
            serialPort1.StopBits = System.IO.Ports.StopBits.One;
            serialPort1.Open();
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Write("2");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            serialPort1.Write("3");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Write("1");
        }
        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string str = serialPort1.ReadExisting();
            textBox1.AppendText(str);
        }
    }
}

仅作为防丢失存档,未完成,考核内容,请勿转载。

此作者没有提供个人介绍
最后更新于 2020-11-11