Pages: [1]   Go Down
Author Topic: Slow processing?  (Read 914 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ><

Thanks!
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read about pullup resistors smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
« Last Edit: March 30, 2009, 07:22:01 pm by metaphysics1221 » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:

Quote

/*
|| 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) );
}
 
« Last Edit: March 30, 2009, 07:29:36 pm by AlphaBeta » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6381
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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...
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
}
--------------------------------------------------------------------------------
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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, [glow]![/glow]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 [code]...[/code] helps maintain the code indentation and makes it easier to scroll or copy large snippets.
« Last Edit: March 31, 2009, 01:41:26 pm by halley » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
nt 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);
  }
}

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.
Logged

Pages: [1]   Go Up
Jump to: