# Virtual Piano Help ASAP

**URL:** <https://discourse.processing.org/t/virtual-piano-help-asap/25503>\
**Category:** Libraries\
**Tags:** homework\
**Created:** [November 18, 2020, 3:58am UTC](https://discourse.processing.org/t/virtual-piano-help-asap/25503 "2020-11-18T03:58:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lil\_doggo](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@lil\_doggo](https://discourse.processing.org/u/lil_doggo)\
**Post date:** [November 18, 2020, 3:58am UTC](https://discourse.processing.org/t/virtual-piano-help-asap/25503/1 "2020-11-18T03:58:15Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hi!  
I’m making a virtual piano for a school assignment and since the black keys and white keys overlap, two notes are played when I tap on black keys. How do I fix this so that only the black key is played?

I know I have sound files in this but I don’t know how to attach but if you need to try it out just comment them out. Also, I don’t know how to format code for these topics…

here’s the code:

import processing.sound.\*;  
SoundFile file1;  
SoundFile file2;  
SoundFile file3;  
SoundFile file4;  
SoundFile file5;

void setup() {  
size(150,200);  
file1 = new SoundFile(this, “c5.wav”);  
file2 = new SoundFile(this, “d5.wav”);  
file3 = new SoundFile(this, “e5.wav”);  
file4 = new SoundFile(this, “c-5.wav”);  
file5 = new SoundFile(this, “d-5.wav”);  
}

void draw() {  
//loop for the white and black keys  
for (int x = 0; x \< width + 40; x += 50) {  
whiteKeys(x, 0);  
}  
for (int y = -15; y \< width + 40; y += 50) {  
if ((y != -15)&&(y != 135)&&(y != 335)&&(y != 485)&&(y != 685)) {  
blackKeys(y, 0);  
}

//key labels  
textSize(25);  
text(“C D E”, 15, 180);  
fill(255);  
textSize(18);  
text(“C# D#”, 36, 80);  
text(“Db Eb”, 36, 105);  
}

if (mouseX\>0&&mouseX\<50&&mousePressed) {  
file1.play();//plays the note  
fill(#A0A0A0, 60);//makes the key darker  
rect(0,0,50,200);  
}  
//these allow the keys to “light up” as I move my mouse  
if (mouseX\>0&&mouseX\<50) {  
fill(#A0A0A0, 50);  
rect(0,0,50,200);  
}

if (mouseX\>50&&mouseX\<100&&mousePressed) {  
file2.play();  
fill(#A0A0A0, 60);  
rect(50,0,50,200);  
}  
if (mouseX\>50&&mouseX\<100) {  
fill(#A0A0A0, 50);  
rect(50,0,50,200);  
}  
if (mouseX\>100&&mouseX\<150&&mousePressed) {  
file3.play();  
fill(#A0A0A0, 60);  
rect(100,0,50,200);  
}  
if (mouseX\>100&&mouseX\<150) {  
fill(#A0A0A0, 50);  
rect(100,0,50,200);  
}  
if (mouseX\>35&&mouseX\<65&&mouseY\<115&&mousePressed) {  
file4.play();  
}  
if (mouseX\>85&&mouseX\<115&&mouseY\<115&&mousePressed) {  
file5.play();  
}  
}  
void whiteKeys(int x, int y) {  
pushMatrix();  
stroke(0);  
fill(255);  
rect(x, y, 60, 200);  
fill(0);  
popMatrix();  
}  
void blackKeys(int x, int y) {  
pushMatrix();  
stroke(0);  
fill(0);  
rect(x, y, 30, 115);  
fill(0);  
popMatrix();  
}

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [November 18, 2020, 6:06am UTC](https://discourse.processing.org/t/virtual-piano-help-asap/25503/2 "2020-11-18T06:06:48Z")

</div>

Welcome to the forums!

Have you learned about the else statement?

---

<div class="post-metadata">

**Author:** ![lil\_doggo](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@lil\_doggo](https://discourse.processing.org/u/lil_doggo)\
**Post date:** [November 18, 2020, 3:45pm UTC](https://discourse.processing.org/t/virtual-piano-help-asap/25503/3 "2020-11-18T15:45:58Z")

</div>

Hi!  
Yes, I know what else statements mean but I haven’t been using much of them in my code.

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [November 18, 2020, 4:47pm UTC](https://discourse.processing.org/t/virtual-piano-help-asap/25503/4 "2020-11-18T16:47:52Z")

</div>

Forgot to mention: please format your code. Makes reading it much easier.

I suggest you experiment with if-else’s and else-if’s!
