const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int referenceValue = 128; // default output value
int varianceValue = 0; // how far may the outputValue deviate from the referenceValue?
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
varianceValue = map(sensorValue, 0, 1023, 0, 127);
outputValue = random(referenceValue - varianceValue, referenceValue + varianceValue);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor:");
Serial.print(sensorValue);
Serial.print(",output:");
Serial.println(outputValue);
delay(5000 - sensorValue*3);
}