Slow processing?

I just got my first Arduino board, the Duemilanove with a ATMEGA 328 chip. I’m powering the board from my PC via USB.

I’ve done all the basic stuff, LED’s, etc. But when I try to test some of the logic functions I can’t get them to work right. The code I wrote is:

int ledpin=13;
int input=10;
int val=0;

void setup()
pinMode(ledpin, OUTPUT);
pinMode(input, INPUT);

void loop()
val = digitalRead(input);
digitalWrite(ledpin, val);

I’ve been running this with the LED going from pin 13 to the ground and a wire going from the +5V pin to pin 10. The program works as I intended, turning the LED on when the voltage is applied to pin 10.

However, when I REMOVE the voltage from pin 10, the LED stays on for a while, ~ 10 seconds. Is this a coding mistake, a hardware mistake, or what? I can’t belive there isn’t a better way ><


Read about pullup resistors :)

I tried that using 10 ohm and 100 ohm resistors, but neither of them did anything. Am I still looking too high? Maybe I need an even smaller resistor? I'd have thought the LED would have more resistance than that...

Try this:

/* || Using pullup resistor || || Description: || Wire the switch between pin 10 and ground. Now pin 13 should go LOW when the switch is pressed. || || Contributed: || Alexander Brevig */

byte switchPin = 10; byte ledPin = 13;

void setup(){ pinMode(switchPin,INPUT); digitalWrite(switchPin,HIGH); //enable internal pullup resistor pinMode(ledPin,OUTPUT); } void loop(){ digitalWrite( ledPin , digitalRead(switchPin) ); }

I've been running this with ... a wire going from the +5V pin to pin 10.

If you're using +5V as on, you need a "pull down" resistor rather than a pull-up resistor...

10 Ohms and 100 Ohms are way too low resistances! Try 10000 Ohms (brown black orange).

Ok, so, using the internal pull downs, is it possible to make it so that COMPLETING the circut, rather than detaching it, will turn on the LED? Obviously then setting the ledpin to do what the digitalread on the input pin is doesn't work to do that. I've tried things like this, but nothing has worked so far:

int ledpin=13; int input=7; int var2=2; int scan=0;

void setup() { pinMode(ledpin,OUTPUT); scan=digitalRead(input); digitalWrite(input,HIGH); } void loop() { if(scan==HIGH) { digitalWrite(ledpin,LOW); } else { digitalWrite(ledpin,HIGH); }


The “internal” resistors are pull-up, not pull-down. They’re equivalent to a 20Kohm resistor attached to +V. If using internal pullups, then LOW input means pressed, and HIGH input means released. You can use digitalWrite(ledpin, !input) to output the opposite value of input.

If you wire some pull-downs, 10Kohm attached to GND is a good default, as has already been suggested. If using pulldowns, then LOW input means released, and HIGH input means pressed.

Also, putting your code in ... helps maintain the code indentation and makes it easier to scroll or copy large snippets.

Oooh, I see! So the pull resistors are giving the electricity a kind of less attractive default path to take if nothing else is available? Is the only difference between pull up and down if they attach to the 5V pin or the ground pin?

And thanks for the other tips, the !input works great, I didn't know those signs could be used on variables alone like that.

a kind of less attractive default path to take if nothing else is available

That is exactly the way that you should view the concept.

nt ledpin=13;
int input=7;
int var2=2;
int scan=0;

void setup()

void loop()

Beware! That sketch code does not put the "scan=digitalRead(input);" line inside the "loop()" function. It will onlt test the value of the input pin once, before the main loop begins to operate. Subsequent pin changes (i.e. button presses) will be ignored.