#define ANALOG_IN 0 #define PEAK 3.0 #define CONVERGE 10 // Define PIN 13 for LED int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { unsigned char duty; unsigned char avg; unsigned char result; signed char diff; int val = analogRead(ANALOG_IN); result = val; // Find the difference and converge the average diff = result - avg; avg += diff / CONVERGE; // Determine if it’s a peak if( result > avg * PEAK ) duty = (100*result)/avg; else duty = 0; if (duty != 0 ) { digitalWrite(ledPin, LOW); } else { digitalWrite(ledPin, HIGH); //Add a delay to make the wire lighting visible delay(10); } }