#include <Arduino.h>
#include <ResponsiveAnalogRead.h>
// 74HC4067 Analog Multiplexer for Pots
int PotSel_1 = 2;
int PotSel_2 = 3;
int PotSel_3 = 4;
int PotSel_4 = 5;
// define the pin you want to use
const int ANALOG_PIN = A0;
// make a ResponsiveAnalogRead object, pass in the pin, and either true or false depending on if you want sleep enabled
// enabling sleep will cause values to take less time to stop changing and potentially stop changing more abruptly,
// where as disabling sleep will cause values to ease into their correct position smoothly and more accurately
ResponsiveAnalogRead analog(ANALOG_PIN, true);
ResponsiveAnalogRead analog1(ANALOG_PIN, true);
// Setup -------------------------------------------------------
void setup()
{
// init PotSel 74HC4067 Analog Multiplexer
pinMode(PotSel_1, OUTPUT);
pinMode(PotSel_2, OUTPUT);
pinMode(PotSel_3, OUTPUT);
pinMode(PotSel_4, OUTPUT);
digitalWrite(PotSel_1, LOW);
digitalWrite(PotSel_2, LOW);
digitalWrite(PotSel_3, LOW);
digitalWrite(PotSel_4, LOW);
// init ADC Input for Pots
analogReadResolution(10);
analog.setAnalogResolution(1024);
}
// read Pots ---------------------------------------------------
void read_Pot()
{
if (readPot_Timer > 5)
{
readPot_Timer = 0;
// select Pot
digitalWrite(PotSel_1, LOW);
digitalWrite(PotSel_2, LOW);
digitalWrite(PotSel_3, LOW);
digitalWrite(PotSel_4, LOW);
// read channel 1
analog.update();
// read channel 2
digitalWrite(PotSel_1, HIGH);
analog1.update();
if (analog.hasChanged() && analog1.hasChanged())
{
int value_A = analog.getValue(); // value_A 0 - 1022
int value_B = analog1.getValue(); // value_B 0 - 1022
Serial.print(value_A);
Serial.print(" ");
Serial.print(value_B);
Serial.print(" ");
// Compute current angle and dial position
double fx = ((double)value_A / 511.5) - 1; // range -1 to +1
double fy = ((double)value_B / 511.5) - 1;
double angle = atan2(fx, fy)/PI; // range -pi to +pi
Serial.println(angle);
//int dialPosition = map(angle, -PI, PI, 0, 128);
}
}
}
// Loop ---------------------------------------------------
void loop()
{
read_Pot();