Arduino Uno sketch for testing Desynn type position indicators

Back to instrument simulation page.

Here I have used a 36 point lookup table / array to simulate a sine wave in 10 degree steps.

The code samples this array at three points evenly spaced 120 degrees apart to generate the amplified values of the three phases.

The position of the sampling along the array is determined by an input, in this case a potentiometer for testing.

Serial.print is included for diagnostics, but is not needed. phase_x_value variables were included for this reason, and also could be omitted to simplify the code a touch.

Note: This code could easily be modified to generate a simple three phase variable frequency motor driver (VSD / Variable Speed Drive).

/*
Desynn aircraft instrument signal generator.

2017-06-21 Test program written using potentiometer input, also with serial print for diagnostics.

Scott Bouchard UK www.scottbouch.com
*/

const int an_in_pin = A2;     // Define analogue input
int an_in = 0;                // raw input value
int in_scaled;                // scaled input value to array index
int pwm_out_1 = 3;            // Declare board output pin
int pwm_out_2 = 5;            // Declare board output pin
int pwm_out_3 = 6;            // Declare board output pin
int phase_2_offset = 12;      // Lookup offset figure for phase 2
int phase_3_offset = 24;      // Lookup offset figure for phase 3
int phase_1_value = 0;        // Value of output 1
int phase_2_value = 0;        // Value of output 2
int phase_3_value = 0;        // Value of output 3
int array_lookup_1 = 0;       // Output 1 array lookup point
int array_lookup_2 = 12;      // Output 1 array lookup point
int array_lookup_3 = 24;      // Output 1 array lookup point
int sine_index = 0;           // Sine wave index counter.
int sine_index_max = 36;      // The sine wave is broken into 36 values.
int sine_array[]=
  {128,150,171,191,209,225,238,247,253,255,253,247,238,225,209,191,
  171,150,128,105,84,64,46,30,17,8,2,0,2,8,17,30,46,64,84,105,128};

void setup() {
  pinMode(pwm_out_1, OUTPUT);
  pinMode(pwm_out_2, OUTPUT);
  pinMode(pwm_out_3, OUTPUT);
  Serial.begin(9600);
  }

void loop() {
  an_in = analogRead(an_in_pin);           // reads the input (value between 0 and 1023)
  in_scaled = map(an_in, 0, 1023, 0, sine_index_max);  // scale input for array lookup


  phase_2_offset = in_scaled + 12;
  phase_3_offset = in_scaled + 24;



  array_lookup_1 = in_scaled;              // No offset on the first phase.

  if (phase_2_offset < sine_index_max)
  {array_lookup_2 = phase_2_offset;}       // Offset on second phase means lookop value
  else                                     // may overrun the end, so needs to start again
  {array_lookup_2 = phase_2_offset - sine_index_max;}  // at the beginning.

  if (phase_3_offset < sine_index_max)      // Same comment as above.
  {array_lookup_3 = phase_3_offset;}
  else
  {array_lookup_3 = phase_3_offset - sine_index_max;}



  phase_1_value = sine_array[array_lookup_1];//Conduct lookup and pass to phase vlaue
  analogWrite(pwm_out_1, phase_1_value);     // Write the output to the pin
  
  phase_2_value = sine_array[array_lookup_2];//Conduct lookup and pass to phase vlaue
  analogWrite(pwm_out_2, phase_2_value);     // Write the output to the pin

  phase_3_value = sine_array[array_lookup_3];//Conduct lookup and pass to phase vlaue
  analogWrite(pwm_out_3, phase_3_value);     // Write the output to the pin
  
  Serial.print(", out 1: ");
  Serial.print(phase_1_value);
  Serial.print(", out 2: ");
  Serial.print(phase_2_value);
  Serial.print(", out 3: ");
  Serial.print(phase_3_value);
  Serial.print("\n");
  
}