Defining a function inside a For loop

Hello,

I am stuck in the following code:

For[i = 0, i < 10, i++,

 signal[t_ /; 0 + i <= t <= 0.5/4 + i] = 0;

 signal[t_ /; 0.5/4 + i < t <= 1.5/4 + i] = 1;

 signal[t_ /; 1.5/4 + i < t <= 2.5/4 + i] = 0;

 signal[t_ /; 2.5/4 + i < t <= 3.5/4 + i] = -1;

 signal[t_ /; 3.5/4 + i < t <= 4/4 + i] = 0]

Basically, I want a function which is periodical over the period i. What did I do wrong with defining the function in a loop?

Thanks in advance!
Chris

Solved it already, however if someone got a better way please let me know.

My solution is as follows:

signal[t_] := 
 For[i = 0, i < 10, i++, 
  If[0 <= t <= 0.5/4 + i, Return[0], 
   If[0.5/4 + i < t <= 1.5/4 + i, Return[1],
    If[1.5/4 + i < t <= 2.5/4 + i, Return[0],
     If[2.5/4 + i < t <= 3.5/4 + i, Return[-1],
      If[3.5/4 + i < t <= 4/4 + i, Return[0]
       ]]]]]]

signal[t_] := Floor[Mod[t + 1/2, 1]/3] - Floor[Mod[t,1]/3]

This should be a more manipulable function, and it works anywhere:

I would have found it easier with a more detailed description of your target. A plot like the one pictured helps to clarify the result you’re seeking.

signal[t_] := Piecewise[{{1, 0.5/4 < Mod[t, 1] <= 1.5/4}, {-1, 2.5/4 < Mod[t, 1] <= 3.5/4}}]

 Plot[signal[t], {t, 0, 10}, Exclusions -> None]

Hello David,
thank you for your nice solution!

My target is as follows:
I want to simulate the behavior of an optical switch, therefor it is important to get the function of my waveform generator. The “weird” zero lines in-between will give me additional information whether the switch behaves the same for e.g. +10V and -10V. The way i get the information out of it, is by comparing the amplitude of the optical output to the zero line output.