Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this function in side a loop , so i want to count number of transmation .
from exaplpe , when zero_cross move form 1 to 0 , it shlould count , and when it moves from 0 to 1 it should count too.

What I have tried:

int Zero_Cross;

if(BackFromPowerLoss > 20)
{
Zero_Cross =0 ;
}
else
{
Zero_Cross = 1 ;
}

}
Posted
Updated 21-Jan-18 22:51pm

1 solution

A simple way to do this is to have two extra variables, one to track the crossing from 0 to 1 and the other to track the crossing from 1 to 0. The thing you will need to take into consideration is that you only want to increment these values when Zero_Cross changes. As an example:
C
int oneToZero;
int zeroToOne;
int Zero_Cross;
if (BackFromPowerLoss > 20)
{
  if (Zero_Cross == 1)
  {
    oneToZero++;
  }
  Zero_Cross = 0;
}
You should be able to fill the rest in from that snippet. The key thing is that you need to check what the previous value of Zero_Cross is before you change it.
 
Share this answer
 
Comments
BaselAla 22-Jan-18 4:59am    
here u8PowerLoss_old will store the value of zero_cross , so i need to use the u8PowerLoss_old variables to help me to find the transmaition.
static int u8PowerLoss_old = 0;
int Zero_Cross;

if(BackFromPowerLoss > 20)
{
Zero_Cross =0 ;
}
else
{
Zero_Cross = 1 ;
}
   u8PowerLoss_old = Zero_Cross; // this is a static variable , 
}
Pete O'Hanlon 22-Jan-18 5:03am    
So, replace if (Zero_Cross == 1) with if (u8PowerLoss_old == 1) in my example above.
BaselAla 22-Jan-18 5:10am    
thank you (:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900