切换通过但不完成操作处理

时间:2016-11-29 02:22:25

标签: function switch-statement processing

import processing.serial.*;  // add the serial library 
Serial myPort;        // the serial port to monitor  
PFont f;
PrintWriter output;
int pianoBColor = 0;
int numKeys = 6;
int sizeW = 900;
int sizeH = 830;
int pianoBackgroundW = sizeW*8/9;
int pianoBackgroundH = sizeH/3;
int blackSpace = 30;
int Empty1;
int Empty2;
int whitePressed=20;
int blackPressed = 255;
int whiteUnpressed = 255;
int blackUnpressed = 0;
int divider = 2;
int roundness = 10;
int pianoSpacingH =(sizeH - pianoBackgroundH) ;
int pianoSpacingW = (sizeW-pianoBackgroundW)/2;
int pianoKeyH = pianoBackgroundH - divider*2 ;
int pianoKeyW = (780/numKeys) - divider*8;
int num;

void setup ()
{
  size(900, 830);  // set the window size, will need to hardcode for Processing 3 compatibility
  background(0, 255, 250);
  println(Serial.list());  // list all available serial ports 
  // myPort = new Serial(this, Serial.list()[0], 115200);  // define input port 
  // myPort.clear(); 
  f = createFont("Arial", 50, true);
  RecorderDrawer();
  PianoDrawer();


}

void draw ()
{
delay(1000);
PianoPress(1, 255,200);
println("fr");
delay(1000);
PianoPress(1,0,255);

}

void PianoPress(int num, int blackKeysColor, int whiteKeysColor)
{
  // 1, 8 black key covers the right hand
  // 7, 12  black key covers the left hand
  // 2,4,6,9,11 black keys
  // 3,5,10 white keys covered by both sides
  switch(num)
  { 
  case 1:
    fill (whiteKeysColor);
    println("front");
    rect(pianoSpacingW +divider/2, pianoSpacingH+divider, pianoKeyW, pianoKeyH, roundness) ;
   println("debug");
    fill (blackKeysColor);
    println("back");
    rect(pianoSpacingW +divider/2+divider+pianoKeyW*2/3-2, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;
    println("debug");
    break;

    case 2:
    fill (blackKeysColor);
    println("case2");
    rect(pianoSpacingW +divider/2+divider+pianoKeyW*2/3-2, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;
     delay(100);
    break;

    case 3:
    fill (whiteKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW, pianoSpacingH+divider, pianoKeyW, pianoKeyH, roundness) ; 
    fill (blackKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW*4/6, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    rect(pianoSpacingW +divider/2+divider+pianoKeyW*10/6, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    break;

     case 4:
    fill (blackKeysColor);
    rect(pianoSpacingW +divider/2+divider+pianoKeyW*10/6, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    break;

     case 5:
    fill (whiteKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW*2, pianoSpacingH+divider, pianoKeyW, pianoKeyH, roundness) ; 
    fill (blackKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW*16/6+4, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    rect(pianoSpacingW +divider/2+divider+pianoKeyW*10/6, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    break;  

  case 6:
    fill (blackKeysColor);
     println("case6");
    rect(pianoSpacingW +divider/2+pianoKeyW*16/6+4, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    delay(100);
    break;

  case 7:
    fill (whiteKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW*3, pianoSpacingH+divider, pianoKeyW, pianoKeyH, roundness) ;
  fill (blackKeysColor);
    rect(pianoSpacingW+pianoKeyW*16/6+4, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;
    break;

case 8:
    fill (whiteKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW*4, pianoSpacingH+divider, pianoKeyW, pianoKeyH, roundness) ;
    fill (blackKeysColor);
    rect(pianoSpacingW+pianoKeyW*28/6+9, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;
    break;

  case 9:
    fill (blackKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW*28/6+8, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    break;

      case 10:
    fill (whiteKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW*5, pianoSpacingH+divider, pianoKeyW, pianoKeyH, roundness) ; 
    fill (blackKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW*28/6, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    rect(pianoSpacingW +divider/2+pianoKeyW*34/6+10, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    break;

  case 11:
    fill (blackKeysColor);
    rect(pianoSpacingW +divider/2+pianoKeyW*34/6+10, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;  
    break;

     case 12:
    fill (whiteKeysColor);
     println("case12");
    rect(pianoSpacingW +divider/2+pianoKeyW*6, pianoSpacingH+divider, pianoKeyW, pianoKeyH, roundness) ; 
    fill(blackKeysColor);
    rect(pianoSpacingW+pianoKeyW*34/6+10, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;
     delay(100);
    break;
  }
}


// set up functions
void PianoDrawer()
{
  fill(pianoBColor);
  rect( pianoSpacingW, pianoSpacingH, pianoBackgroundW, pianoBackgroundH, roundness);
  WhiteKeyDrawer();
  BlackKeyDrawer();
}

void WhiteKeyDrawer()
{
  for (int i = 0; i <= numKeys; i = i+1)
  {
    fill (255);
    rect(pianoSpacingW +divider/2+i*pianoKeyW, pianoSpacingH+divider, pianoKeyW, pianoKeyH, roundness) ;
  }
}
void BlackKeyDrawer()
{

  for (int i = 0; i <= numKeys; i = i+1)
  {
    if (i<=3)
    {
      Empty1 = i/3;
    } else
    {
      Empty1 = (i-3)/3;
    }
    Empty2 = i/7;
    println( Empty1, " ", Empty2);
    if (Empty1 == 0 && Empty2 == 0)
    {
      fill (0);
      rect(pianoSpacingW +divider/2+divider*i+pianoKeyW*2/3 + pianoKeyW*i, pianoSpacingH+divider, pianoKeyW/2, pianoKeyH/2, roundness) ;
    }
  }
}

void RecorderDrawer()
{
  fill(255);
  rect( pianoSpacingW-5, blackSpace-5, pianoBackgroundW+10, (sizeH-pianoBackgroundH)-100, roundness); 
  //stroke(255);
  fill(60);
  rect( pianoSpacingW, blackSpace, pianoKeyW*2/3, (sizeH-pianoBackgroundH)-blackSpace-80);
  for (int i = 0; i <= numKeys-2; i = i+1)
  {
    rect( pianoSpacingW+pianoKeyW*2/3+pianoKeyW*i/2, blackSpace, pianoKeyW/2, (sizeH-pianoBackgroundH)-blackSpace-80);
  }
  rect( pianoSpacingW+pianoKeyW*19/6, blackSpace, pianoKeyW*5/6, (sizeH-pianoBackgroundH)-blackSpace-80);
  rect( pianoSpacingW+pianoKeyW*4, blackSpace, pianoKeyW*5/6, (sizeH-pianoBackgroundH)-blackSpace-80);

  for (int i = 0; i <= numKeys-4; i = i+1)
  {
    rect(pianoSpacingW+pianoKeyW*29/6-10 +pianoKeyW*i/2, blackSpace, pianoKeyW/2, (sizeH-pianoBackgroundH)-blackSpace-80);
  }
  rect( pianoSpacingW+pianoKeyW*25/4, blackSpace, pianoKeyW*3/4+2, (sizeH-pianoBackgroundH)-blackSpace-80);       
  fill(100, 20, 200);
  textFont(f);
  textAlign(CENTER);
  text("ARDUINO PIANO PLAYER", sizeW/2, sizeH-pianoBackgroundH-20);
}

你好,我正在尝试制作一个能够记录笔画和时间长度的钢琴键盘并播放它。我正在排除其他代码之前的故障排除,并且似乎在按下的键颜色更改时出现问题。我让它通过一个功能,并根据开关情况改变键的颜色。它通过代码但处理屏幕没有经历颜色变化。我该如何解决?

1 个答案:

答案 0 :(得分:0)

您不应该像delay()函数那样调用draw()函数。这导致你每2秒只获得1帧,这肯定会搞砸你的用户输入。

相反,要么使用millis()frameCount变量来执行基于时间的操作。