Arduino Uno takes too long to digitalRead/Write.

Hi, I have this simple code here which outputs power from port 13 if HIGH power is in 7. However, I found that it takes 8 seconds (yes, 8!) to run the loop function and realize that a condition has changed. Is there some way to speed this up? I know I can use traditional C statements for digitalWrite (such as PORTB |= _BV(PB5); ) but I am not sure if there is anything else I'm doing wrong. Otherwise, the code works fine, it just takes way too damn long to run loop().

int motorPin = 13;
int buttonPin = 7;
int val = 0;

void setup() {
  pinMode(motorPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  val = digitalRead(buttonPin);
  if (val == HIGH) {
    digitalWrite(motorPin, HIGH);
  } else {
    digitalWrite(motorPin, LOW);
  }
}

You might want to change void main() to void setup()

manor_royal:
You might want to change void main() to void setup()

Whoops, my bad! It's not like that in the code, I swear. I couldn't copy and paste since the computers at my work are blocked here, so I had to manually type it.

Have you got any pullups or pulldowns on the input pin? It might be that you think the pin changed but since you can't guarantee its state when the button's not pressed, it might not actually be changing since you don't know what it already was. So it might be doing a long bit of looping until it actually sees a real change.

If you use pinMode(buttonPin, INPUT_PULLUP); then you'll use the built in resistor: an unpressed button is definitely high then, and pressed is definitely low.