2015年3月24日火曜日

倉庫番データ2

8
6
4
5
3
1
1
1
1
0
3
0
3
3
1
1
1
2
3
3
0
0
2
3
2
0
2
0
0
2
2
0
0
3
2
0
0
0
0
0
3
0
0
0
3
3
3
0
0
0
3
3



 ■■■■■■■
 ■???? ■
■■■???*■■■
■  *■* * ■
■ **  ■* ■
■    ■   ■
■■■■ ○ ■■■
   ■■■■■

倉庫番データ1

6
6
3
3
0
3
1
3
0
0
0
3
0
3
3
3
3
3
2
0
2
1
1
0
2
0
3
3
3
3
3
2
3
0
0
0
3
1
3
0





 ■?■
 ■ ■■■
■■* *?
? * ■■
■■■*■
  ■?■

230行倉庫番

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>

#define STAGE_NUM 2

void updateGame(int inputData);
void drawData(void);
void loadData(int _stageNum);
int Locate(HANDLE hOut, int x, int y);

int width;
int height;
int data[30][30];
int baseData[30][30];
int cara[2];
int goalCount;
int goal;
int block[30][2];

int main()
{
int stageNum = 0;
loadData(stageNum);
while(true){
drawData();
//Sleep(1000);
while(kbhit()!=1){
}
int t = getch();
if(t == 'r'){
loadData(stageNum);
}else if(t == 'n'){
stageNum++;
if(stageNum >= STAGE_NUM) stageNum = STAGE_NUM-1;
loadData(stageNum);
}else if(t == 'b'){
stageNum--;
if(stageNum < 0) stageNum = 0;
loadData(stageNum);
}
updateGame(t);
}
return 0;
}

void incrementGoalCount(int num){
goalCount+=num;
if(goalCount == goal){
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
Locate(hStdout,0, height+2);
printf("clear!!!");

}

}

void updateGame(int inputData){
if(inputData== 'w'){//上
if(cara[0] > 0){
if(data[cara[0]-1][cara[1]]==0 || data[cara[0]-1][cara[1]]==1){//何もないから大人しく移動
cara[0]-=1;
}else if(data[cara[0]-1][cara[1]]==2){//箱がある
if(cara[0] > 1){
if(data[cara[0]-2][cara[1]]==0 || data[cara[0]-2][cara[1]]==1){//箱の先にも何もないから大人しく移動
if(baseData[cara[0]-1][cara[1]] == 1){//箱が元々?だったから
data[cara[0]-1][cara[1]]=1;
incrementGoalCount(-1);
}else{
data[cara[0]-1][cara[1]]=0;
}
if(data[cara[0]-2][cara[1]]==1){//箱の先が?だったから、ゴール判定だから後ろ
incrementGoalCount(1);
}
data[cara[0]-2][cara[1]]=2;
cara[0]-=1;
}
}

}
}
}else if(inputData== 'z'){//下
if(cara[0] < height-1){
if(data[cara[0]+1][cara[1]]==0 || data[cara[0]+1][cara[1]]==1){//何もないから大人しく移動
cara[0]+=1;
}else if(data[cara[0]+1][cara[1]]==2){//箱がある
if(cara[0] < height-2){
if(data[cara[0]+2][cara[1]]==0 || data[cara[0]+2][cara[1]]==1){//何もないから大人しく移動
if(baseData[cara[0]+1][cara[1]] == 1){//箱が元々?だったから
data[cara[0]+1][cara[1]]=1;
incrementGoalCount(-1);
}else{
data[cara[0]+1][cara[1]]=0;
}
if(data[cara[0]+2][cara[1]]==1){//箱の先が?だったから
incrementGoalCount(1);
}
data[cara[0]+2][cara[1]]=2;
cara[0]+=1;
}
}

}
}
}else if(inputData== 'a'){//左
if(cara[1] > 0){
if(data[cara[0]][cara[1]-1]==0 || data[cara[0]][cara[1]-1]==1){//何もないから大人しく移動
cara[1]-=1;
}else if(data[cara[0]][cara[1]-1]==2){//箱がある
if(cara[1] > 1){
if(data[cara[0]][cara[1]-2]==0 || data[cara[0]][cara[1]-2]==1){//何もないから大人しく移動
if(baseData[cara[0]][cara[1]-1] == 1){//箱が元々?だったから
data[cara[0]][cara[1]-1]=1;
incrementGoalCount(-1);
}else{
data[cara[0]][cara[1]-1]=0;
}
if(data[cara[0]][cara[1]-2]==1){//箱の先が?だったから
incrementGoalCount(1);
}
data[cara[0]][cara[1]-2]=2;
cara[1]-=1;
}
}

}
}

}else if(inputData== 's'){//右
if(cara[1] < width-1){
if(data[cara[0]][cara[1]+1]==0 || data[cara[0]][cara[1]+1]==1){//何もないから大人しく移動
cara[1]+=1;
}else if(data[cara[0]][cara[1]+1]==2){//箱がある
if(cara[1] < width-2){
if(data[cara[0]][cara[1]+2]==0 || data[cara[0]][cara[1]+2]==1){//何もないから大人しく移動
if(baseData[cara[0]][cara[1]+1] == 1){
data[cara[0]][cara[1]+1]=1;
incrementGoalCount(-1);
}else{
data[cara[0]][cara[1]+1]=0;
}
if(data[cara[0]][cara[1]+2]==1){//箱の先が?だったから
incrementGoalCount(1);
}
data[cara[0]][cara[1]+2]=2;
cara[1]+=1;
}
}
}
}
}
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
Locate(hStdout,0, 10);
printf("%d",goalCount);
}

void drawData(void){
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

Locate(hStdout,0, 0);
for(int i=0;i<width+2;i++){
printf("■");
}
Locate(hStdout,0, height+1);
for(int i=0;i<width+2;i++){
printf("■");
}
for(int i=0;i<height;i++){
Locate(hStdout,0, i+1);
printf("■");
for(int j=0;j<width;j++){
if(data[i][j] == 0)printf(" ");
if(data[i][j] == 1)printf("・");
if(data[i][j] == 2)printf("□");
if(data[i][j] == 3)printf("■");
}
printf("■");
}
Locate(hStdout,cara[1]*2+2, cara[0]+1);
printf("○");
}

void loadData(int _stageNum){
goal=0;
goalCount=0;

HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
Locate(hStdout,0, height+2);
printf("         ");

char fileNum[256];
sprintf(fileNum,"stage%d.txt",_stageNum);

FILE *fp;
if ((fp = fopen(fileNum, "r")) == NULL) {
printf("file open error!!\n");
exit(EXIT_FAILURE); /* (3)エラーの場合は通常、異常終了する */
}
fscanf(fp,"%d",&width);
fscanf(fp,"%d",&height);
fscanf(fp,"%d",&cara[0]);
fscanf(fp,"%d",&cara[1]);
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
fscanf(fp,"%d",&data[i][j]);
baseData[i][j] = data[i][j];
if(data[i][j] == 1){
block[goal][0]=j;
block[goal][1]=i;
goal++;
}
}
}
fclose(fp);
}

int Locate(HANDLE hOut, int x, int y)
{
    COORD dwPos;
    dwPos.X = (SHORT)x;
    dwPos.Y = (SHORT)y;

    if (SetConsoleCursorPosition(hOut, dwPos) == 0)
        return -1;
    else
        return 0;
}

2015年3月7日土曜日

paplioテトリス

メガウイング アーケードを用いて

-----

#include "VGA.h"

#define HSync WING_A_1
#define VSync WING_A_0
#define Red0 WING_A_7
#define Red1 WING_A_5
#define Green0 WING_A_6
#define Green1 WING_A_4
#define Blue0 WING_A_2
#define Blue1 WING_A_3
#define RIGHT_PIN WING_B_11
#define LEFT_PIN WING_B_8
#define ROTATE_PIN WING_B_9
#define DOWN_PIN WING_B_10

int right_old_val = 0;
int right_val = 0;
int left_old_val = 0;
int left_val = 0;
int rotate_old_val = 0;
int rotate_val = 0;
int down_old_val = 0;
int down_val = 0;

int spaseX=3;
int spaseY=5;

void setup_pin_select() {
  pinMode(HSync,OUTPUT);
  digitalWrite(HSync,HIGH);
  outputPinForFunction(HSync, 15);
  pinModePPS(HSync, HIGH);

  pinMode(VSync,OUTPUT);
  digitalWrite(VSync,HIGH);
  outputPinForFunction(VSync, 14);
  pinModePPS(VSync, HIGH);   
  
  pinMode(Red0,OUTPUT);
  digitalWrite(Red0,HIGH);
  outputPinForFunction(Red0, 9);
  pinModePPS(Red0, HIGH);   

  pinMode(Red1,OUTPUT);
  digitalWrite(Red1,HIGH);
  outputPinForFunction(Red1, 8);
  pinModePPS(Red1, HIGH); 
  
  pinMode(Green0,OUTPUT);
  digitalWrite(Green0,HIGH);
  outputPinForFunction(Green0, 11);
  pinModePPS(Green0, HIGH); 

  pinMode(Green1,OUTPUT);
  digitalWrite(Green1,HIGH);
  outputPinForFunction(Green1, 10);
  pinModePPS(Green1, HIGH); 

  pinMode(Blue0,OUTPUT);
  digitalWrite(Blue0,HIGH);
  outputPinForFunction(Blue0, 13);
  pinModePPS(Blue0, HIGH); 

  pinMode(Blue1,OUTPUT);
  digitalWrite(Blue1,HIGH);
  outputPinForFunction(Blue1, 12);
  pinModePPS(Blue1, HIGH);     
}

void setup(){
setup_pin_select();
  for(int i=2;i<=17;i++){
    pinMode(i,OUTPUT);
    digitalWrite(i,LOW);
  }
  pinMode(RIGHT_PIN,INPUT);
  pinMode(LEFT_PIN,INPUT);
  pinMode(ROTATE_PIN,INPUT);
  pinMode(DOWN_PIN,INPUT);
  //randomSeed(analogRead(5));
  VGA.begin(VGAWISHBONESLOT(9),CHARMAPWISHBONESLOT(10));
VGA.clear();
}

void loop(){
int i, j,k,c,d,e,l,m,o,g,yon,next,t,p,n,w,x,y,h,f[49],a[21],b[299];

/*nとwとaの初期化*/
h=300;
for(n=0;n<21;n++){
a[n]=0;
}
w=0;
for(k=0;k<23;k++){
for(g=0;g<13;g++){
b[13*k+g]=0;
}
}
for(g=0;g<13;g++){
b[g]=1;
}


next=rand() % 7;
        drawArea();

        while(true){
kesu2(f);
yon=next;
next=rand() % 7;
katakime(f,next);
kaku2(f);
katakime(f,yon);
k=5;
t=0;
p=0;
for (i = 1; i <=20 ; i++) {
sayuu(i,&k,f,b,&j,h);
/*さっそく描画(?*/
sayuu2(f,b,&k,&t,i);
t=k;
if(p!=1){
kaku( k, i, f);
}
/*時間稼ぎ*/
for (j = 0; j <= h; j++) {
if(p!=1){
                                        delay(1);
sayuu(i,&k,f,b,&j,h);
sayuu2(f,b,&k,&t,i);
if(t!=k){
kesu( t, i, f);
t=k;
kaku( k, i,f);
}
}
}
stop(f, b, &p, &t, k, i, a, h);
sorou(a,b);
if(p!=1){
/*とりあえず、消し去ろう*/
if(p!=1){
kesu( k, i, f);
t=k;
}
}
}
}
return;
}
void drawArea(void)
{
        int i;
VGA.begin(VGAWISHBONESLOT(9),CHARMAPWISHBONESLOT(10));
VGA.setBackgroundColor(BLACK);
VGA.setColor(RED);
/*横軸*/
for(i=0;i<30;i++){
VGA.printtext((i)*spaseX,(0)*spaseY,"-");
VGA.printtext((i)*spaseX,(21)*spaseY,"-");
}
for(i=30;i<41;i++){
VGA.printtext((i)*spaseX,(0)*spaseY,"-");
VGA.printtext((i)*spaseX,(2)*spaseY,"-");
VGA.printtext((i)*spaseX,(7)*spaseY,"-");
}
/*縦軸*/
for(i=0;i<22;i++){
VGA.printtext((1)*spaseX,(i)*spaseY,"|");
VGA.printtext((30)*spaseX,(i)*spaseY,"|");
}
VGA.printtext((40)*spaseX,(1)*spaseY,"|");

for(i=3;i<7;i++){
VGA.printtext((40)*spaseX,(i)*spaseY,"|");
}
VGA.printtext((32)*spaseX,(1)*spaseY,"NEXT");
}
int sorou(int *a,int *b){
int c,d,e;
for(c=1;c<21;c++){
if(a[c]==13){
for(d=c;d<20;d++){
for(e=0;e<13;e++){
b[13*d+e]=b[13*d+13+e];
a[d]=a[d+1];
b[260+e]=0;
}
}

VGA.begin(VGAWISHBONESLOT(9),CHARMAPWISHBONESLOT(10));
VGA.clear();
VGA.setBackgroundColor(BLACK);
VGA.setColor(RED);
drawArea();
/*全部描く*/
for(d=1;d<21;d++){
for(e=0;e<13;e++){
if(b[13*d+e]==1){  
VGA.printtext((e+e+3)*spaseX,(21-d)*spaseY,"*");
}
}
}
}
}
}
int zikan(int h)
{
delay(1000);
}
int kaitenhuka(int *f,int k,int i,int *b)
{
int m,n;
for(m=0;m<7;m++){
for(n=0;n<7;n++){
if(f[7*n+m]==1){
if(k-3+m<0){
return 0;
}
if(k-3+m>12){
return 0;
}
if((i-3+n)<1){
return 0;
}
if(b[(21-(i-3+n))*13+k-3+m]==1){
return 0;
}
}
}
}
return 1;
}
int migikaiten(int *f,int k,int i,int *b)
{
int g[49],m,n,t;
for(m=0;m<7;m++){
for(n=0;n<7;n++){
g[m*7+6-n]=f[n*7+m];
}
}
t=kaitenhuka(g,k,i,b);
if(t==1){
kesu(k,i, f);
for(m=0;m<7;m++){
for(n=0;n<7;n++){
f[m*7+n]=g[m*7+n];
}
}
kaku(k,i, f);
}
}
int stop(int *f,int *b,int *p,int *t,int k,int i,int *a,int h)
{
int m,n,o,q,r,s;
for(n=0;n<7;n++){
for(m=0;m<7;m++){
if(f[7*n+m]==1){
if(*p!=1){
if(b[(20-(i-3+n))*13+k-3+m]==1){
*t=k;
for(o=0;o<7;o++){
for(q=0;q<7;q++){
a[21-(i-3+o)]=a[21-(i-3+o)]+f[7*o+q];
}
}
for(r=0;r<7;r++){
for(s=0;s<7;s++){
b[(21-(i-3+r))*13+k-3+s]=b[(21-(i-3+r))*13+k-3+s]+f[7*r+s];
}
}
*p=1;
zikan(h);
}
}
}
}
}
}
int sayuu2(int *f,int *b,int *k,int *t,int i)
{
int m,n;
for(m=0;m<7;m++){
for(n=0;n<7;n++){
if(f[7*n+m]==1){
if(*t<*k){
if(*k-3+m<0){
*k=*k-1;
}
if(*k-3+m>12){
*k=*k-1;
}
if(b[(21-(i-3+n))*13+*k-3+m]==1){
*k=*k-1;
}
}
}
}
}
for(m=0;m<7;m++){
for(n=0;n<7;n++){
if(f[7*n+m]==1){
if(*t>*k){
if(*k-3+m<0){
*k=*k+1;
}
if(*k-3+m>12){
*k=*k+1;
}
if(b[(21-(i-3+n))*13+*k-3+m]==1){
*k=*k+1;
}
}
}
}
}
}
int sayuu(int i,int *k,int *f, int *b,int *j,int h)
{
int l,t,s[7],m,n,o,p,q,r;
l=*k;

for(n=0;n<7;n++){
s[n]=0;
}
o=1;
p=1;
q=0;
r=0;

for(m=0;m<7;m++){
for(n=0;n<7;n++){
s[m]=s[m]+f[n*7+m];
}
}

for(n=0;n<7;n++){
q=q+1;
if(s[n]<1){
if(o==q){
o=o+1;
}
}
}

for(n=6;n>=0;n--){
r=r+1;
if(s[n]<1){
if(p==r){
p=p+1;
}
}
}

right_val=digitalRead(RIGHT_PIN);
left_val=digitalRead(LEFT_PIN);
rotate_val=digitalRead(ROTATE_PIN);
down_val=digitalRead(DOWN_PIN);
if(right_val == HIGH && right_old_val == LOW){
if(l<13-(5-p)){
l=l+1;
}
}
if(left_val == HIGH && left_old_val == LOW){
if(l>0-4+o){
l=l-1;
}
}
if(rotate_val == HIGH && rotate_old_val == LOW){
migikaiten(f,l,i,b);
}
        //if(down_val == HIGH && down_old_val == LOW){
if(down_val == HIGH){
*j=*j+h;
}
right_old_val = right_val;
left_old_val = left_val;
rotate_old_val = rotate_val;
down_old_val =down_val;

*k=l;
}
int kaku(int k,int i,int *f)
{
int m,n;
VGA.begin(VGAWISHBONESLOT(9),CHARMAPWISHBONESLOT(10));
VGA.setBackgroundColor(BLACK);
VGA.setColor(RED);
for(m=0;m<7;m++){
for(n=0;n<7;n++){
if(f[m*7+n]==1){
if(i-3+m!=0){
VGA.printtext((n+n+k+k-3)*spaseX,(i-3+m)*spaseY,"*");
                                }
}
}
}
}


int kesu(int k,int i,int *f)
{
int m,n;
VGA.begin(VGAWISHBONESLOT(9),CHARMAPWISHBONESLOT(10));
VGA.setBackgroundColor(BLACK);
VGA.setColor(BLACK);
for(m=0;m<7;m++){
for(n=0;n<7;n++){
if(f[m*7+n]==1){
if(i-3+m!=0){
VGA.printtext((n+n+k+k-3)*spaseX,(i-3+m)*spaseY,"*");
}
}
}
}
}
int kaku2(int *f)
{
int m,n;
VGA.begin(VGAWISHBONESLOT(9),CHARMAPWISHBONESLOT(10));
VGA.setBackgroundColor(BLACK);
VGA.setColor(RED);
for(m=0;m<7;m++){
for(n=0;n<7;n++){
if(f[m*7+n]==1){
VGA.printtext((31+n+n-3)*spaseX,(2+m)*spaseY,"*");
}
}
}
}

int kesu2(int *f)
{
int m,n;
VGA.begin(VGAWISHBONESLOT(9),CHARMAPWISHBONESLOT(10));
VGA.setBackgroundColor(BLACK);
VGA.setColor(BLACK);
for(m=0;m<2;m++){
for(n=0;n<4;n++){
VGA.printtext((33+n+n-1)*spaseX,(4+m)*spaseY,"*");
}
}
}

int katakime(int *f,int yon)
{
int n;
for(n=0;n<49;n++){
f[n]=0;
}
if(yon==0){
f[16]=1;
f[24]=1;
f[25]=1;
f[23]=1;
}
if(yon==1){
f[18]=1;
f[24]=1;
f[25]=1;
f[23]=1;
}
if(yon==2){
f[23]=1;
f[24]=1;
f[25]=1;
f[26]=1;
}
if(yon==3){
f[17]=1;
f[24]=1;
f[25]=1;
f[23]=1;
}
if(yon==4){
f[17]=1;
f[24]=1;
f[23]=1;
f[18]=1;
}
if(yon==5){
f[17]=1;
f[24]=1;
f[25]=1;
f[16]=1;
}
if(yon==6){
f[17]=1;
f[18]=1;
f[25]=1;
f[24]=1;
}
}

2015年1月13日火曜日

Piano-パソコン側

//javac -cp C:\xbee\rxtx-2.1-7-bins-r2\Windows\ch-rxtx-2.2-20081207-win-x64\RXTXcomm.jar ArduinoPiano.java
//java -cp C:\xbee\rxtx-2.1-7-bins-r2\Windows\ch-rxtx-2.2-20081207-win-x64\RXTXcomm.jar;. -Djava.library.path=C:\xbee\rxtx-2.1-7-bins-r2\Windows\ch-rxtx-2.2-20081207-win-x64 ArduinoPiano

import java.io.*;
import gnu.io.*;
import java.awt.*;
import javax.sound.sampled.*;
public class ArduinoPiano{
static int SAMPLE_RATE = 44100; // 44.1KHz
static int BPM = 120;
static SourceDataLine line;
public static void main( String arg[] ){
try{
int counter=0;
//シリアルポートを確保する
//使用するCOMポートを取得
CommPortIdentifier comID = CommPortIdentifier.getPortIdentifier( "COM13" );
//COMポートを開く
CommPort commPort = comID.open("hoge",2000);
//シリアルポートのインスタンスを生成
SerialPort port = (SerialPort)commPort;

//シリアルポートの設定
//ボーレート、データビット数、ストップビット数、パリティを設定
port.setSerialPortParams( 9800,SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE );
//フロー制御(今回は行わない)
port.setFlowControlMode( SerialPort.FLOWCONTROL_NONE );
InputStream in = port.getInputStream();
while (true) {
byte[] buffer = new byte[1024];
int numRead = in.read(buffer);
if (numRead == -1) {
break;
} else if (numRead == 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// 割り込まれても何もしない
}
}
String xx = new String(buffer, "UTF-8");
String s2 = "do";
if(xx.startsWith(s2)){
playdo();
}
s2 = "re";
if(xx.startsWith(s2)){
playre();
}
s2 = "mi";
if(xx.startsWith(s2)){
playmi();
}
s2 = "fa";
if(xx.startsWith(s2)){
playfa();
}
s2 = "so";
if(xx.startsWith(s2)){
playso();
}
s2 = "ra";
if(xx.startsWith(s2)){
playra();
}
s2 = "si";
if(xx.startsWith(s2)){
playsi();
}
s2 = "ta";
if(xx.startsWith(s2)){
playddo();
}
}
port.close();
}catch( Exception e ){
System.out.println( "エラーです:" + e );
}
}
public static void playdo()
throws LineUnavailableException {
        calcFrequency();
        // Initialize Audio
        AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        line = (SourceDataLine)AudioSystem.getLine(info);
        // Play
        line.open();
        line.start();
        // 演奏
        int n8 = getSampleCount(8/*分音符*/);
        writeNote(getNotenoToFreq(6,C), n8);
        /*writeNote(getNotenoToFreq(6,E), n8);
        writeNote(getNotenoToFreq(6,E), n4);
        writeNote(getNotenoToFreq(6,F), n8);
        writeNote(getNotenoToFreq(6,D), n8);
        writeNote(getNotenoToFreq(6,D), n4);
        writeNote(getNotenoToFreq(6,C), n8);
        writeNote(getNotenoToFreq(6,D), n8);
        writeNote(getNotenoToFreq(6,E), n8);
        writeNote(getNotenoToFreq(6,F), n8);
        writeNote(getNotenoToFreq(6,G), n8);
        writeNote(getNotenoToFreq(6,G), n8);
        writeNote(getNotenoToFreq(6,G), n4);*/
        line.drain(); // 終了まで待機
}
public static void playre()
throws LineUnavailableException {
        calcFrequency();
        // Initialize Audio
        AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        line = (SourceDataLine)AudioSystem.getLine(info);
        // Play
        line.open();
        line.start();
        // 演奏
        int n8 = getSampleCount(8/*分音符*/);
        writeNote(getNotenoToFreq(6,D), n8);
        line.drain(); // 終了まで待機
}
public static void playmi()
throws LineUnavailableException {
        calcFrequency();
        // Initialize Audio
        AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        line = (SourceDataLine)AudioSystem.getLine(info);
        // Play
        line.open();
        line.start();
        // 演奏
        int n8 = getSampleCount(8/*分音符*/);
        writeNote(getNotenoToFreq(6,E), n8);
        line.drain(); // 終了まで待機
}
public static void playfa()
throws LineUnavailableException {
        calcFrequency();
        // Initialize Audio
        AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        line = (SourceDataLine)AudioSystem.getLine(info);
        // Play
        line.open();
        line.start();
        // 演奏
        int n8 = getSampleCount(8/*分音符*/);
        writeNote(getNotenoToFreq(6,F), n8);
        line.drain(); // 終了まで待機
}
public static void playso()
throws LineUnavailableException {
        calcFrequency();
        // Initialize Audio
        AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        line = (SourceDataLine)AudioSystem.getLine(info);
        // Play
        line.open();
        line.start();
        // 演奏
        int n8 = getSampleCount(8/*分音符*/);
        writeNote(getNotenoToFreq(6,G), n8);
        line.drain(); // 終了まで待機
}
public static void playra()
throws LineUnavailableException {
        calcFrequency();
        // Initialize Audio
        AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        line = (SourceDataLine)AudioSystem.getLine(info);
        // Play
        line.open();
        line.start();
        // 演奏
        int n8 = getSampleCount(8/*分音符*/);
        writeNote(getNotenoToFreq(6,A), n8);
        line.drain(); // 終了まで待機
}
public static void playsi()
throws LineUnavailableException {
        calcFrequency();
        // Initialize Audio
        AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        line = (SourceDataLine)AudioSystem.getLine(info);
        // Play
        line.open();
        line.start();
        // 演奏
        int n8 = getSampleCount(8/*分音符*/);
        writeNote(getNotenoToFreq(6,B), n8);
        line.drain(); // 終了まで待機
}
public static void playddo()
throws LineUnavailableException {
        calcFrequency();
        // Initialize Audio
        AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
        line = (SourceDataLine)AudioSystem.getLine(info);
        // Play
        line.open();
        line.start();
        // 演奏
        int n8 = getSampleCount(8/*分音符*/);
        writeNote(getNotenoToFreq(7,C), n8);
        line.drain(); // 終了まで待機
}

// 実際に波形を書き込む
    static void writeNote(double frequency, int sampleCount) {
        byte[] b = new byte[sampleCount];
        double amplitude = SAMPLE_RATE / frequency; // 波長
        for (int i = 0; i < b.length; i++) {
            double r = i / amplitude;
            b[i] = (byte)((Math.round(r) % 2 == 0) ? 100 : -100);
        }
        // 再生(バイト列を line に書き込む)
        line.write(b, 0, b.length);
    }
    // 音の長さを計算する
    static int getSampleCount(int nLength/*n分音符*/) {
        double n4 = (60.0 / BPM) * SAMPLE_RATE; // 四分音符のサンプル数
        double n1 = n4 * 4; // 全音符のサンプル数
        return (int)Math.round(n1 / nLength);
    }
        // 音名を定数で表したもの
    static int C=0,CS=1,D=2,DS=3,E=4,F=5,FS=6,G=7,GS=8,A=9,AS=10,B=11;
    // 音名から NoteNo を計算する
    static int getNoteNo(int octave, int noteName) {
        return octave * 12 + noteName;
    }
    // 周波数を計算してテーブルにセットする
    static double note_freq[] = new double[128];
    static void calcFrequency() {
        // 半音(2の12乗根)を計算
        double r = Math.pow(2.0, 1.0 / 12.0);
        // A(NoteNo=69) より上の音を計算
        note_freq[69] = 440.0; // A のノート
        for (int i = 70; i < 128; i++) {
            note_freq[i] = note_freq[i-1] * r;
        }
        // A(NoteNo=69) より下の音を計算
        for (int i = 68; i >= 0; i--) {
            note_freq[i] = note_freq[i+1] / r;
        }
        return;
    }
    static double getNotenoToFreq(int octave, int noteName) {
        return note_freq[getNoteNo(octave, noteName)];
    }

}

Piano-arduino側

const int dobutton = 6;
const int rebutton = 7;
const int mibutton = 8;
const int fabutton = 9;
const int sobutton = 10;
const int rabutton = 11;
const int sibutton = 12;
const int ddobutton = 13;
int leftbuttonState = LOW;

void setup(){
  pinMode(dobutton,OUTPUT);
  pinMode(rebutton,OUTPUT);
  pinMode(mibutton,OUTPUT);
  pinMode(fabutton,OUTPUT);
  pinMode(sobutton,OUTPUT);
  pinMode(rabutton,OUTPUT);
  pinMode(sibutton,OUTPUT);
  pinMode(ddobutton,OUTPUT);
  Serial.begin(9600) ;
}

void loop(){
 
  leftbuttonState = digitalRead(dobutton);
   if(leftbuttonState == HIGH){
     Serial.println("do");
   }
   leftbuttonState = digitalRead(rebutton);
   if(leftbuttonState == HIGH){
     Serial.println("re");
   }
   leftbuttonState = digitalRead(mibutton);
   if(leftbuttonState == HIGH){
     Serial.println("mi");
   }
   leftbuttonState = digitalRead(fabutton);
   if(leftbuttonState == HIGH){
     Serial.println("fa");
   }
   leftbuttonState = digitalRead(sobutton);
   if(leftbuttonState == HIGH){
     Serial.println("so");
   }
   leftbuttonState = digitalRead(rabutton);
   if(leftbuttonState == HIGH){
     Serial.println("ra");
   }
   leftbuttonState = digitalRead(sibutton);
   if(leftbuttonState == HIGH){
     Serial.println("si");
   }
   leftbuttonState = digitalRead(ddobutton);
   if(leftbuttonState == HIGH){
     Serial.println("t");
   }
  delay(300);
}

Piano