const int pot = A0;
const int LED_RED = 9;
const int windowSize = 10;
int index = windowSize - 1;
int readings[windowSize];
int total = 0;
const float oneAngle = 270. / 255;
int convertSensorValue = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Инициализация последовательного порта
pinMode(LED_RED, OUTPUT);
pinMode(pot, INPUT);
}
int determineRelativeAngle(const int& convertSensorValue)
{
float angle = oneAngle * convertSensorValue;
int res = angle - 135;
return res;
}
int filter(const int& sensorValue)
{
total -= readings[index];
readings[index] = sensorValue;
total += readings[index];
int average = total / windowSize;
--index;
index = (index < 0) ? windowSize - 1 : index;
return average;
}
void loop() {
// put your main code here, to run repeatedly:
int sensorValue = analogRead(pot);
int average = filter(sensorValue);
convertSensorValue = map(average, 0, 1023, 0, 255);
analogWrite(LED_RED, convertSensorValue);
int relativeAngle = determineRelativeAngle(convertSensorValue);
Serial.println(relativeAngle);
delay(100);
}