I am new to arduino and trying some different projects. But something is not behaving as supposed to. No matter what I do with LEDs, it seems like a dormant blinking sketch stays in the arduino no matter what other new scripts I upload. It is like the other scripts become embedded in a blinking script.
I have tried the following script with a 50K potentiometer and a red LED and a resistor:
int potPin = 0;
int potValue = 0;
int led = 9;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
potValue = analogRead(potPin);
analogWrite(led, potValue/4);
delay(10);
}
When I turn up and down the pot, the LED brightness is turned up and down as supposed to. But when I leave the pot alone, the LED blinks continually like the previous sketch is still there. I have tried to remove the "delay(10);", but it makes no difference. I have also tried to change the pin number. No difference.
I have tried to use different boards but it seems to be the same problem on all of them. This includes UNO R3, Mega 2560 and Micro.
I have tried installing different versions of the arduino software. This does not make any differences.
I have tried resetting the arduinos. This does not make any difference. It is like older sketches are not removed when uploading new ones.
The USB is plugged right into the computer. I have tried to unplug, replug and restart the software before uploading a new sketch.
I am using a iMac computer running Yosemite with the appropriate SE java.
Being a new Arduino'er (is that a word?), I have run into some similar newbie problems.
You say you have tried this on different boards, with different versions of Arduino. Have you tried different hardware? Maybe your pot is faulty and when it's not being moved, it tends to short out.
Try some debugging tools within your code. Add some serial.prints to output your values each loop. See if you can find some irregularities there. You can see if all your values and inputs are what they should be. That alone will help you isolate the problem.
You seem to have eliminated the extraordinary causes, try working on the simple ones. Replace each piece of hardware until the problem goes away. I doubt it's in the simple code you posted.
Do all uploaded sketches make LED's on PIN 9 blink? Or is it only this one. Does the LED still blink if attached to unassigned pins? What if you move it to another pin and assign it there?
You don't tear down and rebuild your house when you blow a fuse, you find the blown fuse, replace it and find the source of the blown fuse.
As suggested, print the value of potValue after you read the pot. Does it vary as expected when you move the pot and stay relatively stable when you do not move it ?
How is the pot wired ?
How is the LED wired and what value resistor have you used with it ?
No faulty wires or connections. Did test it before I wrote.
Different arduinos are different hardware as mentioned.
I can upload the sketch to the different boards. But the result is the same. The LED blink every sec instead of being turned on all the time (apart from when the pot is turned all down). So it is like the sketch is being embedded into the previous sketch.
groundfungus:
If you do add in serial prints to debug, you will need to change potPin to something other than pin 0 as that is the RX pin on the Mega328 chips/
"potPin" is, presumably, "potentiometerPin", so there's nothing wrong with putting it on analogue pin 0.
The Atmega ADC prefers its potentiometers to have a resistance of 10k (or less). You will probably get noisy results with a 50k pot. You could try averaging the values.
For testing I would also increase the delay() - perhaps to delay(500) - to see what happens.