This is my code for generating a TTL wave with frequency 30 Hz and modulate it with FSK and carrier frequency 400
f=30;
T = 1/f;
t = linspace(0, T*10, 1000);
y = (1/2)*square(t/T*2*pi)+(1/2);
plot(t, y);
hold on;
fc=400;
df=20;
y_m = cos(2.*pi.*(fc+(2.*y).*df).*t);
plot(t,y_m);
hold off;
First of all, I have phase discontinuity when TTL changes from 0 to 1 or viser versa, and the second problem is that the domain of modulated signal is not the same every where and it changes...
How Can I solve these problems?

For solving the problem of discontinuities, you should make sure the condition for Continuous-Phase FSK (CPFSK) is met; that's the frequency deviation in rad/s should be an integral multiple of
pi/T, which translates into1/2Tin Hz. If you choose arbitrary values forΔf, you should expect discontinuities.For the second problem of non-constant amplitude, you should increase the number of points for the cosine calculation to be smooth enough.