How to make a servo sweep once back and forth with the same keyboard key.

In your first program change this

if (val == 'd' && poser > 0)

to this

else if (val == 'd' && poser > 0)

so that it only applies one of the options at any one time

...R