# Air core gauge control

**URL:** <https://forum.arduino.cc/t/air-core-gauge-control/591286>\
**Category:** Programming\
**Created:** [May 13, 2019, 12:42am UTC](https://forum.arduino.cc/t/air-core-gauge-control/591286 "2019-05-13T00:42:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hross6502](https://avatars.discourse-cdn.com/v4/letter/h/f05b48/32.png) [@hross6502](https://forum.arduino.cc/u/hross6502)\
**Post date:** [May 13, 2019, 12:42am UTC](https://forum.arduino.cc/t/air-core-gauge-control/591286/1 "2019-05-13T00:42:32Z")

</div>

Hello,

I've found this brilliant code to control an air core gauge. The thing is that this code runs over and over. Is there any way that someone can show me how to add code to control it manually with a potentiometer?

```cpp
//the mathematical constant pi, because we are using radians instead of degrees.

// 360 degrees = 2*pi, 180 = pi, 90 = pi/2, etc.
const float pi = 3.14159;

const float stepSize = 0.01; //This is the step size for incrementing the angle (in radians)

//this is the maximum resolution of the PWM output.
const int maxAnalogRes = 255;

/* Define the pins the meter is connected to. We need 1 PWM to vary the voltage in
* each coil, and two more pins to reverse the polarity in that coil. So a total
* of 6 pins (3 per coil).
*/
const int aircore1SinPin = 9; //this controls the voltage to the "sine" coil
const int aircore1CosPin = 10; //this controls the voltage to the "cosine" coil
const int aircore1SinDirPin1 = 2;  //these two control the polarity to the "sine" coil
const int aircore1SinDirPin2 = 4;
const int aircore1CosDirPin1 = 7;  //these two control the polarity to the "cosine" coil
const int aircore1CosDirPin2 = 8;

// milliseconds between each angular step.
int delayTime = 40; 
    
void setup()
{
 //Set the pins to OUTPUT
 pinMode(aircore1SinPin, OUTPUT); 
 pinMode(aircore1CosPin, OUTPUT); 
 pinMode(aircore1SinDirPin1, OUTPUT);
 pinMode(aircore1SinDirPin2, OUTPUT);
 pinMode(aircore1CosDirPin1, OUTPUT);
 pinMode(aircore1CosDirPin2, OUTPUT);
 
 // Set the initial direction as "forward". Of course it could be "reverse" depending on which lead you connected :-).
 digitalWrite(aircore1SinDirPin1, HIGH); 
 digitalWrite(aircore1SinDirPin2, LOW);
 digitalWrite(aircore1CosDirPin1, HIGH);
 digitalWrite(aircore1CosDirPin2, LOW);
}

void loop()                    // run over and over again
{
  //Rotate 0 through 360 degrees (OK, really 2*pi radians, but who cares?)
 for(float i = 0; i < (2*pi); i = i + stepSize){
  setMeterPosition(aircore1SinPin, aircore1SinDirPin1, aircore1SinDirPin2, aircore1CosPin, aircore1CosDirPin1, aircore1CosDirPin2, i);
  delay(delayTime);          // waits for delayTime milliseconds    
 }

 //Rotate 360 through 0 degrees
 for(float i = (2*pi); i > 0; i = i - stepSize){ 
  setMeterPosition(aircore1SinPin, aircore1SinDirPin1, aircore1SinDirPin2, aircore1CosPin, aircore1CosDirPin1, aircore1CosDirPin2, i);
  delay(delayTime);          // waits for delayTime milliseconds
 }
}

// setMeterPosition() - put it at the angle in radians
void setMeterPosition(int sinPin, int sinDirPin1, int sinDirPin2, int cosPin, int cosDirPin1, int cosDirPin2, float pos){

 // Calculate the voltage on the PWM pins based on the angle we want
 float sinCoilValue = maxAnalogRes*sin(pos);
 float cosCoilValue = maxAnalogRes*cos(pos);
 
 // change the polarity of the coil depending on the sign of the voltage level
 if (sinCoilValue<=0) { 
  digitalWrite(sinDirPin1, LOW); //Change to LOW if using relays
  digitalWrite(sinDirPin2, HIGH); //comment this line out if using relays
 } else {
  digitalWrite(sinDirPin1, HIGH); //Change to HIGH if using relays
  digitalWrite(sinDirPin2, LOW); //comment this line out if using relays
 }
 if (cosCoilValue<=0) { 
  digitalWrite(cosDirPin1, LOW); //Change to LOW if using relays
  digitalWrite(cosDirPin2, HIGH); //comment this line out if using relays
 } else {
  digitalWrite(cosDirPin1, HIGH); //Change to HIGH if using relays
  digitalWrite(cosDirPin2, LOW); //comment this line out if using relays
 }

 // take the absolute value because analogWrite doesn't take negatives
 sinCoilValue = abs(sinCoilValue);
 cosCoilValue = abs(cosCoilValue);

 analogWrite(sinPin, sinCoilValue);
 analogWrite(cosPin, cosCoilValue);
}]

```

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [May 13, 2019, 11:16am UTC](https://forum.arduino.cc/t/air-core-gauge-control/591286/2 "2019-05-13T11:16:43Z")

</div>

> hross6502:  
> I've found this brilliant code to control an air core gauge. The thing is that this code runs over and over. Is there any way that someone can show me how to add code to control it manually with a potentiometer?

What is an air core gauge?

What does the present program do?

What exactly do you mean by "control it manually with a potentiometer"?

...R

---

<div class="post-metadata">

**Author:** ![hross6502](https://avatars.discourse-cdn.com/v4/letter/h/f05b48/32.png) [@hross6502](https://forum.arduino.cc/u/hross6502)\
**Post date:** [May 13, 2019, 11:20am UTC](https://forum.arduino.cc/t/air-core-gauge-control/591286/3 "2019-05-13T11:20:35Z")

</div>

This: [Air-core motor test (arduino) on Vimeo](https://vimeo.com/96496868)

Cannot find the code this guy used.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [May 13, 2019, 4:19pm UTC](https://forum.arduino.cc/t/air-core-gauge-control/591286/4 "2019-05-13T16:19:10Z")

</div>

This is what I got from Vimeo

> Player error  
> The player is having trouble. We’ll have it back up and running as soon as possible.

If you would like us to take the trouble to help you why don't YOU take the trouble to answer my simple questions?

...R

---

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [May 13, 2019, 4:58pm UTC](https://forum.arduino.cc/t/air-core-gauge-control/591286/5 "2019-05-13T16:58:28Z")

</div>

Replace void loop() {...} with:

```cpp
const int potentiometerPin = A0;

void loop() {
  int a = analogRead(potentiometerPin);

  float f = 2*pi * a/1024;

 setMeterPosition(aircore1SinPin, aircore1SinDirPin1, aircore1SinDirPin2, aircore1CosPin, aircore1CosDirPin1, aircore1CosDirPin2, f);
}

```

---

<div class="post-metadata">

**Author:** ![hross6502](https://avatars.discourse-cdn.com/v4/letter/h/f05b48/32.png) [@hross6502](https://forum.arduino.cc/u/hross6502)\
**Post date:** [May 13, 2019, 6:09pm UTC](https://forum.arduino.cc/t/air-core-gauge-control/591286/6 "2019-05-13T18:09:09Z")

</div>

Thank you Robin2. I apologize.

Thank you for taking the time to help me, Morgan.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:49pm UTC](https://forum.arduino.cc/t/air-core-gauge-control/591286/7 "2021-05-05T23:49:23Z")

</div>


