Private Sub RotationalIndicator1_ValueChanged(sender As Object, e As EventArgs) Handles RotationalIndicator1.ValueChanged Dim turns As Single = RotationalIndicator1.Value / 360 RotationalIndicator1.Text = turns.ToString("0.0") End Sub
int Start_Angle; // The first compass reading to mark our beginning rotation. // signed int, will range 0 to 359int Last_Angle; // preserve the value of the last reading. // signed int, will range 0 to 359int Current_Angle; // computed position for where we are now. // signed int, will range -720 to +720void Init_Rotation(int Current_Pos);void Get_Rotation(int Current_Pos); void main(void){ Init_Rotation(10); Get_Rotation(20); Init_Rotation(10); Get_Rotation(350); Init_Rotation(90); Get_Rotation(271); Init_Rotation(20); Get_Rotation(10); Init_Rotation(350); Get_Rotation(10); Init_Rotation(271); Get_Rotation(90); }void Init_Rotation(int Current_Pos){ // set Start_Angle, Last_Angle, // and Current_Angle to the current reading Current_Angle = Last_Angle = Start_Angle = Current_Pos; } void Get_Rotation(int Current_Pos){ // will take the current reading, // compute the Current_Angle, // then update Last_Angle, and Current_Angle int Delta; // compute delta angle Delta = Current_Pos - Last_Angle; // put Delta into range of // -180 <= angle <= 180 if (Delta > 180) Delta -= 360; if (Delta < -180) Delta += 360; // update Current_Angle Current_Angle += Delta; // update last reading with our current reading Last_Angle = Current_Pos; }
void main(void){ Init_Rotation(10); Get_Rotation(20); Init_Rotation(10); Get_Rotation(350); Init_Rotation(90); Get_Rotation(271); Init_Rotation(20); Get_Rotation(10); Init_Rotation(350); Get_Rotation(10); Init_Rotation(271); Get_Rotation(90); }
Dim Delta As Integer Dim Last_Angle As Integer = 360 Dim Total_Turns As Single Private Sub RotationalIndicator1_ValueChanged(sender As Object, e As EventArgs) Handles RotationalIndicator1.ValueChanged Dim Current_Angle As Integer = RotationalIndicator1.Value '* Determine Delta (CW or CCW) If Current_Angle > 180 AndAlso Last_Angle = 360 Then Delta = Last_Angle - Current_Angle ElseIf Current_Angle <= 180 AndAlso Last_Angle = 360 Then Delta = Current_Angle Else Delta = Current_Angle - Last_Angle End If Total_Turns = Total_Turns + ( Delta / 360 ) RotationalIndicator1.Text = Total_Turns.ToString("0.0") If Current_Angle = 0 Then Last_Angle = 360 Else Last_Angle = Current_Angle End If End Sub
CCW = ((Target>Current) AND (Target-Current < 180)) OR ((Current>Target) AND (Current-Target>=180))CW = ((Target>Current) AND (Target-Current >= 180)) OR ((Current>Target) AND (Current-Target<180))
That code looks like somebody was testing certain values.Maybe try the following code, it has not been tested by me and might need some corrections:Code: [Select] Dim Delta As Integer Dim Last_Angle As Integer = 360 Dim Total_Turns As Single Private Sub RotationalIndicator1_ValueChanged(sender As Object, e As EventArgs) Handles RotationalIndicator1.ValueChanged Dim Current_Angle As Integer = RotationalIndicator1.Value '* Determine Delta (CW or CCW) If Current_Angle > 180 AndAlso Last_Angle = 360 Then Delta = Last_Angle - Current_Angle ElseIf Current_Angle <= 180 AndAlso Last_Angle = 360 Then Delta = Current_Angle Else Delta = Current_Angle - Last_Angle End If Total_Turns = Total_Turns + ( Delta / 360 ) RotationalIndicator1.Text = Total_Turns.ToString("0.0") If Current_Angle = 0 Then Last_Angle = 360 Else Last_Angle = Current_Angle End If End Sub