1. When I play soccer, I am excited.

2. When I sad, I listen to music.
3. When I play the piano, I am happy.

4. While I play soccer, I am excited.
5. While I sad, I listen to music.
6. While I play the piano, I am happy.
---------------------------------------
Are they all grammatical?
Can we use both 'when' and 'while' in this case? What is the difference between 'when' and'while'?

Numbers 2 and 5 are incorrect. The others are correct.

"sad" is an adjective. In these sentences, a predicate is needed. "I am sad", a predicate adjective. "I sad" has no predicate, so is incorrect.

1. When I play soccer, I am excited.

2. When I am sad, I listen to music.
3. When I play the piano, I am happy.

4. While I play soccer, I am excited.
5. While I am sad, I listen to music.
6. While I play the piano, I am happy.
---------------------------
I corrected the two sentences, and then are all grammatical?

Yes, now all are fine.

Yes, all of the sentences you provided are grammatically correct. Both "when" and "while" can be used in these sentences, but there is a slight difference in meaning between the two.

1. "When I play soccer, I am excited." - This sentence means that every time you play soccer, you feel excited.

2. "When I am sad, I listen to music." - This sentence means that whenever you experience sadness, you turn to music as a method of coping or finding comfort.

3. "When I play the piano, I am happy." - This sentence means that playing the piano brings you joy and happiness.

4. "While I play soccer, I am excited." - This sentence suggests that while you are actively engaged in playing soccer, you feel excitement.

5. "While I am sad, I listen to music." - This sentence indicates that during the period of sadness, you use music as a way to alleviate those feelings.

6. "While I play the piano, I am happy." - This sentence implies that as you engage in playing the piano, you experience happiness.

In general, "when" refers to a specific point in time or a habitual action, whereas "while" implies a sense of simultaneous occurrence or duration of an action.