Arduino digitalRead problem

When I use the digitalRead() function, it stays as 'high' for about 5 seconds after I have pushed the button, then goes back down to 'low'. How can I stop this from happening? It impairs my program.

I have an ATmega168.

My program code:

[edit]

#include <ps2.h>
#include <Servo.h> 

PS2 mouse(6, 5);
int button = 2;

int buttonstate;
int servox = 0;
int servoy = 0;
int minimum = 0;
int maximum = 180;

Servo xservo; 
Servo yservo; 

void mouse_init()
{
  mouse.write(0xff);
  mouse.read();
  mouse.read();
  mouse.read();
  mouse.write(0xf0);
  mouse.read();
  delayMicroseconds(100);
}

void setup()
{
  pinMode(button, INPUT);
  xservo.attach(11);
  yservo.attach(10);
  Serial.begin(4800);
  mouse_init();
}

void loop()
{
  char mstat;
  char mx;
  char my;
  mouse.write(0xeb);
  mouse.read();
  mstat = mouse.read();
  mx = mouse.read();
  my = mouse.read();  
  buttonstate = digitalRead(button);
  if (buttonstate != HIGH) {
    servox = servox + (mx / 5);
    if (servox < minimum) {
       servox = 0;
    }
    if (servox > maximum) {
       servox = 180;
    } 
    servoy = servoy + (my / 5);
    if (servoy < minimum) {
       servoy = 0;
    }
    if (servoy > maximum) {
       servoy = 180;
    }
  }
  else {
    Serial.print("X: ");
    Serial.print(servox, DEC);
    xservo.write(servox);
    Serial.print(" ");
    Serial.print("Y: ");
    Serial.print(servoy, DEC);
    yservo.write(servoy);
    Serial.println();
    buttonstate = LOW;
  }
  delay(20);
}

[/edit]

From looking at your code, the digitalRead function does not seem to be at fault. Perhaps you should tell us about what kind of device is connected to digital pin 2.

How have you wired your switch to the digital input pin? It sounds like you failed to use an external pull-rown resistor and your input pin is 'floating' when you release the switch.

Lefty

connected to digital 2 is just a simple button, and I know the button isn't broken because i tried using just a plain wire for +5 to digital 2 and it still did the same: read HIGH for 5 seconds after removing the wire from digital 2.

connected to digital 2 is just a simple button

And what about a pull-up/down?

A

pull-up/down

switch wouldn't make a difference. The hardware is not the problem. I used a wire from +5 to digital 2, plugging it in to digital 2 to simulate button press, and taking it out of digital 2 to simulate button release, and it still did the same, registered the button or "button" as pushed 5 seconds after it was pushed.

If your switch is connected from Pin 2 to VCC (+5V) then you need to also connect a resistor (1k-10k is a good value -- its not critical) from Pin 2 to GND.

When your button is pushed then it connects pin 2 to VCC (5v) and the pin is high.

When the button is not pushed the resistor "pulls the pin down" -- hence the term "pulldown" -- to Ground (0V).

With nothing connected to the pin it is "floating" and it's input level is indeterminate. Circuit noise might make it high or low.

The hardware is not the problem.

Yes it is, all physical mechanical switches require pull-ups or pull-downs to operate as digital input signals. Reread the post reponses and or visit the Arduino reference/playground section for explanations and examples on how to provide proper pull-ups or pull-downs.

Lefty

Works great now, thanks!
And thanks for teaching me about pull-up resistors. I never knew about those. I'm new to electronics.

Also, is there a way to clear the serial monitor screen with a command from the arduino?

No, there isn't. The window is cleared by restarting the serial monitor, which restarts the Arduino.

However, you can send blank lines, which make sure that there is everything has scrolled off the top before sending new data.

The purpose of the serial monitor window is to aid in debugging the application. Having the application clear the screen would defeat this purpose.

Also, is there a way to clear the serial monitor screen with a command from the arduino?

The built in serial monitor is rather limited, but is still useful for debugging and quick checks. The thing is, that once that Arduino IDE finishes uploading a sketch, it disconnects from the serial port. Therefore one can lanuch any capable PC based serial terminal program and communicate with the already connected Arduino board.

For Windows I recommend brey terminal, it's powerful and free, a nice combination. :wink: The brey terminal program has a disconnect button so that it's easy to stop the communications program, and then proceed to re-edit a sketch and upload again, all without having to open and close either the Arduion IDE or brey terminal program.

The brey terminal program has macro commands so you can preload commands meant for your Arduino sketch to simulate actual operation, etc. It also can send or receive files and lots of other cool stuff.

So even with marginal evolutional improvements made to the Arduino serial monitor, it will never match the capablities of a best of class terminal programs.

Lefty