This is the three phase code so far when i was using the UNO
int PWMUH = 11; // LED connected to digital pin 11
int PWMVH = 10; //LED connected to digital pin 10
int PWMVL = 9; //LED connected to digital pin 9
int PWMWH = 6; //LED connected to digital pin 6
int PWMWL = 5; //LED connected to digital pin 5
int PWMUL = 3; //LED connected to digital pin 3
//These Are crossed to ensure that the higher frequency set below is associated with the W phase upper and lower
//set the start positions of the various waves
int PHASEUupper = 255;
int PHASEUlower = 0;
int PHASEVupper = 0;
int PHASEVlower = 85;
int PHASEWupper = 0;
int PHASEWlower = 85;
/*
The Below sets the intial direction of travel
this variable is adjusted below to vary the
top and bottom half of each phase
Set the directions of travel
*/
int DirectionUpper1 = 1;
int DirectionUpper2 = 0;
int DirectionUpper3 = 0;
int DirectionLower1 = 0;
int DirectionLower2 =-1;
int DirectionLower3 = 1;
void setup()
{
/*
The below adjusts the time function of Timer 0, 1, 2
It must be noted that when adjusting the timers in this way
to adjust frequency functions such as delay() are effected,
for prototype testing this is adequate.
*/
TCCR0B = TCCR0B & 0b11111000 | 0x02; //Timer 0 Adjustment this will give a frequency value of 7812.5Hz on pins 5&6
TCCR1B = TCCR1B & 0b11111000 | 0x02; //Timer 1 Adjustment this will give a frequency value of 3906.25Hz on pins 9&10
TCCR2B = TCCR2B & 0b11111000 | 0x02; //Timer 2 Adjustment this will give a frequency value of 3906.25Hz on pins 11&3
}
void loop()
{
/*
assign the various integers (above)
to the various pins.
0-255 represents 0-100% Duty Cycle
*/
analogWrite(PWMWH, PHASEWupper);
analogWrite(PWMWL, PHASEWlower);
analogWrite(PWMVH, PHASEVupper);
analogWrite(PWMVL, PHASEVlower);
analogWrite(PWMUH, PHASEUupper);
analogWrite(PWMUL, PHASEUlower);
/*
In the below each phase is checked to see whether
it has reached the top or bottom half of the phase,
when it has it will then ramp up the other half of the phase
*/
if ((PHASEUupper == 255) & (DirectionUpper1 == 1)) { DirectionUpper1 = -1; }
if ((PHASEUupper == 0) & (DirectionUpper1 == -1)) { DirectionUpper1 = 0; DirectionLower1 = +1; } if ((PHASEVupper == 255) & (DirectionUpper2 == 1)) { DirectionUpper2 = -1; }
if ((PHASEVupper == 0) & (DirectionUpper2 == -1)) { DirectionUpper2 = 0; DirectionLower2 = + 1; } if ((PHASEWupper == 255) & (DirectionUpper3 == 1)) { DirectionUpper3 = -1; }
if ((PHASEWupper== 0) & (DirectionUpper3 == -1)) { DirectionUpper3 = 0; DirectionLower3 = +1; } if ((PHASEUlower == 255) & (DirectionLower1 == 1)) { DirectionLower1 = -1; }
if ((PHASEUlower == 0) & (DirectionLower1 == -1)) { DirectionLower1 = 0; DirectionUpper1 = 1; } if ((PHASEVlower == 255) & (DirectionLower2 == 1)) { DirectionLower2 = -1; }
if ((PHASEVlower == 0) & (DirectionLower2 == -1)) { DirectionLower2 = 0; DirectionUpper2 = 1; } if ((PHASEWlower == 255) & (DirectionLower3 == 1)) { DirectionLower3 = -1; }
if ((PHASEWlower == 0) & (DirectionLower3 == -1)) { DirectionLower3 = 0; DirectionUpper3 = 1; }
/*
This section is where the waves are powered up or down.
If you add a positive value to a number, it increments.
But if you add a negative number it /de/crements.
In this way, the pos/neg flipping above makes the waves
travel in the right direction when we do the addition
down here.
*/
PHASEUupper = PHASEUupper + DirectionUpper1;
PHASEVupper = PHASEVupper + DirectionUpper2;
PHASEWupper = PHASEWupper + DirectionUpper3;
PHASEUlower = PHASEUlower + DirectionLower1;
PHASEVlower = PHASEVlower + DirectionLower2;
PHASEWlower = PHASEWlower + DirectionLower3;
/*
A delay can be implemented here when testing with LED lights, however this is effected by the adjustment to the timers above to set frequency.
only use the delay function when the functions in the Void setup loop is commented out.
delay(100);
*/
}