To write a good melody, the first thing you need to decide is the note that will perform the function of a tonic. The tonic represents the still center around which the other notes of the melody tend to circle. When performing the function of a tonic in this way, that note is called the keynote, and the music is said to be in that particular key.
The Scale and Mode
Another important feature of melody is the scale in which it is written. Given that a particular note has been chosen to be the tonic, the next thing to decide is the scale in which the melody will be written. The keyboard has a range of 12 notes, and it is very rare to find tonal melodies that use all 12. Melodies generally use a selection of five (pentatonic), six (hexatonic), seven (heptatonic), or sometimes eight (octatonic) notes of the 12-note chromatic scale.
The Tonic and Dominant
Once you have chosen a key and a suitable scale to work with, the next thing to take into account is the tonic and the dominant relationships within that scale. The dominant is the fifth degree of the major or minor scale. Tonic and dominant are related to each other as a polarity. The tonic note is the point of repose in the melody, which means that it is the note that usually brings melodic phrases to a close. For this reason, it is sometimes called the home note. The dominant, on the other hand, is the pole of activity around which the melody tends to circle before it returns back to the tonic. Generally, a melody will start with the tonic or one of the notes of the tonic triad, head up toward the dominant and circle around it, and then gradually make its way back toward the tonic.