Go Down

### Topic: Newbie here!! Starting a loop while its still finishing? (Read 5003 times)previous topic - next topic

#### Irish_Features

##### May 15, 2013, 08:23 pm
Hi guys

I have an issue and I apologise if its simple and I'm stupid.

I wish to control 6 MOSFETS to change a single phase DC current to square wave 3 phase.

In order to do so I need to run 6 pins but as the loop ends the start must begin before a full stop to stay in phase.

I would rather be pointed in a direction rather than a straight up answer..

Also is his possible??

#### tobyb121

#1
##### May 15, 2013, 08:35 pm
I'm not sure I understand what you are asking here. The time between calls to loop will be tiny (a few clock cycles so just a few microseconds).
While I appreciate you want to work it out for yourself a bit, some more clarity would be needed to allow us to point you in the right direction.

#### Irish_Features

#2
##### May 15, 2013, 08:50 pm
Ok, I'm sorry for the lack of information

I will begin with what i call the start.

I will be using Square Digital Waves here not Analogue

3 Phase AC Sin Wave

in a 50Hz 1 Full Cycle is 2Hz (Positive Wave + Negative Wave)

Now, The First Phase Begins at 0 degrees
The Second Begins at 120 Degrees
The Third at 240 Degrees

However the First Phase ends at 360 degrees (2Hz)
The Second Phase Begins while the  first one is finishing, and third phase begins while the second is finishing.

I need the First Phase to begin as the Third finishes. But so Far all i can get is a loop that doesn't bring in the first wave with the third.

How can i really do this? btw im learning in C

This was my failed attempt.

int PhaseOnePin_P = 22;
int PhaseTwoPin_P = 24;
int PhaseThreePin_P = 26;
float PhaseDelay = 400;
int PhaseDuration = 100;
int BufferDelay = 1;

void setup()
{
pinMode (PhaseOnePin_P, OUTPUT);
pinMode (PhaseTwoPin_P, OUTPUT);
pinMode (PhaseThreePin_P, OUTPUT);
}

void loop ()
{
digitalWrite (PhaseOnePin_P, HIGH);
delay (PhaseDelay);
digitalWrite (PhaseTwoPin_P, HIGH);
delay (PhaseDuration);
digitalWrite (PhaseOnePin_P,LOW);
delay (PhaseDuration*3);
digitalWrite (PhaseThreePin_P, HIGH);
delay (PhaseDuration);
digitalWrite (PhaseTwoPin_P, LOW);
delay (PhaseDuration);
digitalWrite (PhaseOnePin_P, HIGH);
delay (PhaseDuration*2);
digitalWrite (PhaseThreePin_P, LOW);

}

#### C2

#3
##### May 15, 2013, 10:15 pm
There is probably a better way to do it, but when I plot out your signals, both phase one and two are high for 5/12 of a cycle. I think you need to write low before the phase delay.

Code: [Select]
`  digitalWrite (PhaseOnePin_P, HIGH);  delay (PhaseDelay);  digitalWrite (PhaseOnePin_P, LOW);  digitalWrite (PhaseTwoPin_P, HIGH);  delay (PhaseDelay);  digitalWrite (PhaseTwoPin_P, LOW);  digitalWrite (PhaseThreePin_P, HIGH);  delay (PhaseDelay);  digitalWrite (PhaseThreePin_P, LOW);`

#### tobyb121

#4
##### May 15, 2013, 10:19 pm
It should be possible to do it completely with that technique, split your period into 6, then do it as:
1 ON, 2 OFF, 3 ON
1 ON, 2 OFF, 3 OFF
1 ON, 2 ON , 3 OFF
1 OFF, 2 ON, 3 OFF
1 OFF, 2 ON, 3 ON
1 OFF, 2 OFF, 3 ON

If you set the outputs on each part it will always stay in phase perfectly

#### Irish_Features

#5
##### May 16, 2013, 06:17 am
C2* if i turn off my pin before the phase delay the phase will not get any signal will it not?. It needs to get signal for 5/12 then a delay of 2/12 then negative on 5/12, This is to do with the way the MOSFET's are working, if by chance i turn the positive and negative on at the same time it will be a dead short and i will have dust for MOSFET's. Also at 50Hz and a delay of 200 micro seconds between cross overs, I will never see the delay with a motor.

I managed to work it out by using some If statements, but I'm still seeing some little problems. One is that my PhaseThreePin_P turns on at the start, even though i stated only after the count reaches 1.

Could i have a digitalRead so that it will correct its self in it goes out of phase?

How difficult would it be to time everything with the on board Oscillator??

There also must be a Much cleaner way to write this?

void loop ()
{
digitalWrite (PhaseOnePin_P, HIGH);
if (count = 1);
{
digitalWrite(PhaseThreePin_P,HIGH);
count = 0;
}
if (count =1);
{
delay(PhaseDuration);
digitalWrite (PhaseThreePin_P, LOW);
}
delay (PhaseDuration*2);
digitalWrite (PhaseTwoPin_P, HIGH);
delay (PhaseDuration);
digitalWrite (PhaseOnePin_P,LOW);
delay (PhaseDuration*2);
digitalWrite (PhaseThreePin_P, HIGH);
delay (PhaseDuration);
digitalWrite (PhaseTwoPin_P, LOW);
delay (PhaseDuration*2);
digitalWrite (PhaseOnePin_P, HIGH);

#### tobyb121

#6
##### May 16, 2013, 08:40 am
Those if statements are wrong. if (count =1) will always be true, = assigns the value of count and then returns the result of the assignment, it should be a ==, i.e. if (count==1), which is a comparison. Also you shouldn't have semi-colons at the end of your if() line, that terminates the if statement there.

There also must be a Much cleaner way to write this?

There is the way I suggested:
Code: [Select]
`void loop(){   setPins(HIGH,LOW,HIGH);   delay(Period_over_6);   setPins(HIGH,LOW,LOW);   delay(Period_over_6);   setPins(HIGH,HIGH,LOW);   delay(Period_over_6);   setPins(LOW,HIGH,LOW);   delay(Period_over_6);   setPins(LOW,HIGH,HIGH);   delay(Period_over_6);   setPins(LOW,LOW,HIGH);   delay(Period_over_6);}void setPins(char pin1,char pin2,char pin3){   digitalWrite(Phase1Pin_P,pin1);   digitalWrite(Phase2Pin_P,pin2);   digitalWrite(Phase3Pin_P,pin3);}`

#### C2

#7
##### May 16, 2013, 11:29 pmLast Edit: May 16, 2013, 11:31 pm by C2 Reason: 1
I'm not sure how it should be, but I've plotted both mine and toby's output. I added what I suspect could be the sine wave of an LRC circuit on mine. I'd like to figure out how it's done as well as I do have a few 3-phase motors I'd like to run.

#### MarkT

#8
##### May 17, 2013, 02:55 am
Usually you drive 2 phases at a time, the other being open-circuit.  That does require 6 pins to
control all 6 MOSFETs of the 3-phase bridge.  Its called trapezoidal drive.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### Irish_Features

#9
##### May 17, 2013, 06:16 pm

Thank you very much, im so happy with this!

I just need to code in an On switch and Off Switch
My final Code For the phase shift is:

int Period_over_6 = 50;
int Phase1Pin_P = 22;
int Phase2Pin_P = 24;
int Phase3Pin_P = 26;
int Phase1Pin_N = 23;
int Phase2Pin_N = 25;
int Phase3Pin_N = 27;

void setup (){
pinMode (Phase1Pin_P, OUTPUT);
pinMode (Phase2Pin_P, OUTPUT);
pinMode (Phase3Pin_P, OUTPUT);
pinMode (Phase1Pin_N, OUTPUT);
pinMode (Phase2Pin_N, OUTPUT);
pinMode (Phase3Pin_N, OUTPUT);
}

void loop(){
setPins_P(HIGH,LOW,HIGH);
setPins_N(LOW,HIGH,LOW);
delay(Period_over_6);
setPins_P(HIGH,LOW,LOW);
setPins_N(LOW,HIGH,HIGH);
delay(Period_over_6);
setPins_P(HIGH,HIGH,LOW);
setPins_N(LOW,LOW,HIGH);
delay(Period_over_6);
setPins_P(LOW,HIGH,LOW);
setPins_N(HIGH,LOW,HIGH);
delay(Period_over_6);
setPins_P(LOW,HIGH,HIGH);
setPins_N(HIGH,LOW,LOW);
delay(Period_over_6);
setPins_P(LOW,LOW,HIGH);
setPins_N(HIGH,HIGH,LOW);
delay(Period_over_6);

}

void setPins_P(char pin1,char pin2,char pin3){
digitalWrite(Phase1Pin_P,pin1);
digitalWrite(Phase2Pin_P,pin2);
digitalWrite(Phase3Pin_P,pin3);
}

void setPins_N(char pin4,char pin5,char pin6){
digitalWrite(Phase1Pin_N,pin4);
digitalWrite(Phase2Pin_N,pin5);
digitalWrite(Phase3Pin_N,pin6);
}

#### MarkT

#10
##### May 18, 2013, 12:37 am
That's the same square wave drive, not trapezoidal.

BTW how are your MOSFETs wired - are they all n-channel?  Using suitable drivers with bootstrapped gate drive
voltage?  Or are the high-side MOSFETs p-channel?  What voltage?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### Irish_Features

#11
##### May 19, 2013, 10:30 pm
The voltage is max 5kW but I have not decided just yet...

After some thought... That code won't do what I want

The crossover time between MOSFETS needs to be small but it cannot be the same as the phase duration... I want a variable option, with time....

An analogue input (potentiometer) to serial read the value....

BUT....

I cannot run 3 voids at one time can I??? Or is there another option??

#### PeterH

#12
##### May 20, 2013, 03:38 amLast Edit: May 21, 2013, 01:53 am by PeterH Reason: 1
I don't know what you mean by 'a variable option, with time' but if you want a timed overlap / underlap between the outputs for different phases switching, then simply handle that in the same way you're handling the rest of the timing.

Quote

I cannot run 3 voids at one time

No you can't, and please stop calling them 'voids'. They are functions. Loop() is a function. The word void is an adjective - it is the return type of the function. Void loop() is a function named loop which has a void return type.

#### tobyb121

#13
##### May 20, 2013, 09:52 am
That would be a very easy change to make:
Code: [Select]
`void loop(){   int val=analogRead(A0);   Period_over_6=map(val,0,1023,MIN_PERIOD,MAX_PERIOD);   setPins_P(HIGH,LOW,HIGH);   setPins_N(LOW,HIGH,LOW);   delay(Period_over_6);   setPins_P(HIGH,LOW,LOW);   setPins_N(LOW,HIGH,HIGH);   delay(Period_over_6);   setPins_P(HIGH,HIGH,LOW);   setPins_N(LOW,LOW,HIGH);   delay(Period_over_6);   setPins_P(LOW,HIGH,LOW);   setPins_N(HIGH,LOW,HIGH);   delay(Period_over_6);   setPins_P(LOW,HIGH,HIGH);   setPins_N(HIGH,LOW,LOW);   delay(Period_over_6);   setPins_P(LOW,LOW,HIGH);   setPins_N(HIGH,HIGH,LOW);   delay(Period_over_6);}`

#### Grumpy_Mike

#14
##### May 20, 2013, 10:06 am
You have not said how these are wired up. You were also asked about the voltage and you answered with a power.
You need to show what hardware you have before you can even begin to write code for it.
Normally the hardware for this would be a h- bridge not just FETs.

Go Up