You are on page 1of 4

int

e 1
int
1
int
e 2
int
2
int
e 3
int
3

PhaseA1 = 5;

// the number of the pin controlling the High side of Phas

PhaseA2 = 6;

// the number of the pin controlling the Low side of Phase

PhaseB1 = 7;

// the number of the pin controlling the High side of Phas

PhaseB2 = 8;

// the number of the pin controlling the Low side of Phase

PhaseC1 = 9;

// the number of the pin controlling the High side of Phas

PhaseC2 = 10;

// the number of the pin controlling the Low side of Phase

int ledStateA1 = LOW;


s on or off
int ledStateA2 = LOW;
int ledStateB1 = HIGH;
int ledStateB2 = HIGH;
4 OFF
int ledStateC1 = LOW;
int ledStateC2 = LOW;
long previousMillis = 0;
ated
int sensorPin = A0;
ling phase rotation
int sensorValue = 0;
long wavelength = 100;

// ledState used to set whether that MOSFET i


// LOW side MOSFETS are on with a HIGH
// HIGH side MOSFETS are on with a LOW
// At any given time 2 MOSFETS should be ON,

// will store last time a cycle finished was upd


// Pin which reads output from the switch control
// Initial sensor value
// wavelength of the output

void setup() {
// set the digital pin as output:
pinMode(PhaseA1,
pinMode(PhaseA2,
pinMode(PhaseB1,
pinMode(PhaseB2,
pinMode(PhaseC1,
pinMode(PhaseC2,

OUTPUT);
OUTPUT);
OUTPUT);
OUTPUT);
OUTPUT);
OUTPUT);

}
void loop()
{
unsigned long currentMillis = millis();
sensorValue = analogRead(sensorPin);
if(sensorValue>500){
PhaseA1 = 5;
// the number of the LED pin
PhaseA2 = 6;
PhaseB1 = 7;
PhaseB2 = 8;
}
else{
PhaseA1 = 7;
// the number of the LED pin
PhaseA2 = 8;
PhaseB1 = 5;
PhaseB2 = 6;
}

int sensorValueMap =map(sensorValue, 150, 850, 1, 11); // maps sensorValues i


nto 11 different cases.
// If the potentiomete
r did not have resistors
// than the second two
numbers would be 0 and 1024

switch(sensorValueMap){
ent by potentiometer.
case 1:
wavelength = 15;
wavelength is 15ms.
break;
case 2:
wavelength = 30;
wavelength is 30ms...
break;
case 3:
wavelength = 100;
break;
case 4:
wavelength = 500;
break;
case 5:
wavelength = 1000;
break;
case 6:
wavelength = 5000;
break;
case 7:
wavelength = 1000;
break;
case 8:
wavelength = 500;
break;
case 9:
wavelength = 100;
break;
case 10:
wavelength = 30;
break;
case 11:
wavelength = 15;
break;

// switches wavelength depending on value s


// if the sensorValue is in case 1, than the

// if the sensorValue is in case 2, than the

unsigned long elapsed = currentMillis - previousMillis; // elapsed is the time


since the end of the last cycle
int pulses =map(elapsed, 0, 2*wavelength, 0, 12);
// maps the elapsed tim
e into 13 possible cases
switch (pulses){
case 0:
ledStateA1=HIGH;
// Sets the state for the A MOSFETs so the output from t
he inverter follows the correct waveform

ledStateA2=LOW;
ledStateB1=HIGH;
B is Negative, phase C
ledStateB2=HIGH;
ledStateC1=LOW;
ledStateC2=LOW;
digitalWrite(PhaseA1,
digitalWrite(PhaseA2,
digitalWrite(PhaseB1,
digitalWrite(PhaseB2,
digitalWrite(PhaseC1,
digitalWrite(PhaseC2,
break;
case 1:
ledStateA1=LOW;
ledStateA2=LOW;
digitalWrite(PhaseA1,
digitalWrite(PhaseA2,
ledStateC1=HIGH;
ledStateC2=LOW;
digitalWrite(PhaseC1,
digitalWrite(PhaseC2,
break;
case 3:
ledStateB1=HIGH;
ledStateB2=LOW;
digitalWrite(PhaseB1,
digitalWrite(PhaseB2,
ledStateC1=HIGH;
ledStateC2=HIGH;
digitalWrite(PhaseC1,
digitalWrite(PhaseC2,
break;
case 5:
ledStateB1=LOW;
ledStateB2=LOW;
digitalWrite(PhaseB1,
digitalWrite(PhaseB2,
ledStateA1=HIGH;
ledStateA2=LOW;
digitalWrite(PhaseA1,
digitalWrite(PhaseA2,
break;
case 7:
ledStateA1=HIGH;
ledStateA2=HIGH;
digitalWrite(PhaseA1,
digitalWrite(PhaseA2,
ledStateC1=HIGH;
ledStateC2=LOW;
digitalWrite(PhaseC1,
digitalWrite(PhaseC2,
break;
case 9:
ledStateC1=LOW;
ledStateC2=LOW;
digitalWrite(PhaseC1,
digitalWrite(PhaseC2,
ledStateB1=HIGH;
ledStateB2=HIGH;

// Case 0 is time = 0
// From time=0 to time=wavelength/12 phase A is 0, phase
is Positive

ledStateA1);
ledStateA2);
ledStateB1);
ledStateB2);
ledStateC1);
ledStateC2);
// Case 1 is time = wavelength/12
// Phase A is switching to Pos
ledStateA1);
ledStateA2);
// Phase C is switching to 0
ledStateC1);
ledStateC2);
// Case 3 is time = 3*wavelength/12
// phase B is switching to 0
ledStateB1);
ledStateB2);
//Phase C is switching to negative
ledStateC1);
ledStateC2);
// Case 5 is time = 5*wavelength/12
// Phase B is switching to positive
ledStateB1);
ledStateB2);
// Phase A is switching to 0
ledStateA1);
ledStateA2);
// Case 7 is time = 7*wavelength/12
// Phase A is switching to negative
ledStateA1);
ledStateA2);
// Phase C is switching to 0
ledStateC1);
ledStateC2);
// Case 9 is time = 9*wavelength/12
// phase C is switching to positive
ledStateC1);
ledStateC2);
// phase B is switching to negative

digitalWrite(PhaseB1, ledStateB1);
digitalWrite(PhaseB2, ledStateB2);
break;
case 11:
// Case 11 is time = 11*wavelength/12
ledStateA1=HIGH;
//Phase A is switching to 0
ledStateA2=LOW;
digitalWrite(PhaseA1, ledStateA1);
digitalWrite(PhaseA2, ledStateA2);
ledStateB1=HIGH;
// Phase B is switching to negative
ledStateB2=HIGH;
digitalWrite(PhaseB1, ledStateB1);
digitalWrite(PhaseB2, ledStateB2);
break;
case 12:
// Case 12 is time = 12*wavelength/12 or the end of t
he program
previousMillis = currentMillis;
break;
}
if(pulses>12){
previousMillis = currentMillis;
// prevents program from crashing i
f it misses case 12
}
}

You might also like