Pages: [1] 2   Go Down
Author Topic: Newbie here!! Starting a loop while its still finishing?  (Read 1219 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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??
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
 
}
Logged

0
Offline Offline
Full Member
***
Karma: 2
Posts: 226
#include˂baby.h˃™
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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);
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
 
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley the way I suggested:
Code:
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);
}
Logged

0
Offline Offline
Full Member
***
Karma: 2
Posts: 226
#include˂baby.h˃™
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.



* 3-phase.png (42.07 KB, 269x480 - viewed 72 times.)
« Last Edit: May 16, 2013, 04:31:51 pm by C2 » Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11672
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

LADS!!!!!

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);
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11672
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The voltage is max 5kW but I have not decided just yet...

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

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??
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 20, 2013, 06:53:31 pm by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That would be a very easy change to make:
Code:
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);
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33358
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: