Sorry, but parts of this code make no sense to me.
digitalWrite(ledPin, LOW);
delay (3);
digitalWrite(ledPin, LOW);
Why set a pin low, wait three microseconds, and set it low again? Do you have reason to believe it didn't work the first time?
if (analogValue <= 4) {
delay (997);
}
else if (analogValue <= 1){
Now, assuming that analogValue was 0 or 1. The first if would be true (<=4). Anything following the else will not execute so why test if analogValue <=1? This Portion of code will NEVER run.
If you write a function like
void doBlink(int onDelay, int offDelay)
{ digitalWrite(ledPin, HIGH);
delay (onDelay);
digitalWrite(ledPin, LOW);
delay (offDelay); }
You could shorten your program considerably (and use less programming Memory!) by replacing calls such as
else if (analogValue <= 918){
digitalWrite(ledPin, HIGH);
delay (800);
digitalWrite(ledPin, LOW);
delay (200);
}
With
else if (analogvalue <= 918)
doBlink(800, 200);
You could even create an Array with the individual threshold values, the on delay and the off delay and Loop through the Array instead of having all those if else Statements.
I wouldn't have even thought this line would compile
else (analogValue <= 1020);{
else, not followed by "if" but with a conditional test? What?
Why do you initialize the Serial port (using a lot of Memory to do so) and then not use it? I don't see any Serial.write or Serial.writeln Statements so why is the Serial port initialized.
Actually, if you included something like Serial.writelin(analogValue); you might also discover why the LED is blinking. The analog port will return a value between 0 and 1023. Theoretically, with the analog port attached to ground it should be getting a Zero. But is it really?