0

I am trying to write a simple control program for an Arduino Uno for an experiment I'm running at work. Quite simply it just needs to read if an input pin is high, if it is wait 10 milliseconds to turn an output pin high, hold for 10 milliseconds then go low, else the output pin is low.

My problem is that when I run this it ignores the initial delay altogether, and the output pin stays high for several seconds before going low. (using delayMicroseconds)

void setup()
{
pinMode(8, INPUT);
pinMode(13, OUTPUT);
}

void loop()
{
  if (digitalRead(8) == HIGH)
  {
    delayMicroseconds(10000);  //wait 10 milliseconds
    digitalWrite(13, HIGH);   // Pump on
    delayMicroseconds(10000);   // holds for pulse width of 10 millisecond
    digitalWrite(13, LOW);   // Pump off
  }
  else
  {
  }
}

I've tried setting up something simpler for debugging using the delay function to wait for a second, then turn output pin high, wait for a seconds, then turn output pin low. I did this so I could visually debug using the arduino's built in LED. the result is that it actually continues to run the loop 3 times after the input pin goes low. (using delay)

void setup()
{
pinMode(8, INPUT);
pinMode(13, OUTPUT);
}

void loop()
{
  if (digitalRead(8) == HIGH)
  {
    delay(1000);  //wait 1 second
    digitalWrite(13, HIGH);   // Pump on
    delay(1000);                  // hold for 1 second
    digitalWrite(13, LOW);   // Pump off
  }
  else
  {
  }
}

I can't seem to figure out why it's doing this. I've looked all over and can't seem to find information about why this would happen. I might be missing something really simple, I am not an experienced coder, I just write what I need to run experiments. I've tried reading and writing to the pin register directly using c code, and switching from an if statement to a while loop, none of them fixed the problem. Any insight is greatly appreciated.

4
  • Can you connect the board to a PC via USB port to use Serial interface for debugging? Also: have you tried to set pin 13 initially to LOW in setup()? Commented Jul 21, 2014 at 19:00
  • Also pin 13 behaves somewhat differently because of the attached LED/resistor. I would suggest trying another pin. Also using (or turning on) a pull up resistor. Also set pin to LOW in setup(). Commented Jul 21, 2014 at 19:40
  • 1
    That is about as simple a program possible. If the input was locked high, the output would toggle 10 ms on/ 10 ms off. Do you have an oscilloscope connected to the output that allows you to see the difference between rapid toggle vs 100% on? Your second test programs suggests that the input pin is floating high longer than you expect. If the input is connected to a push button to +5, then you need a pull down resistor. Commented Jul 22, 2014 at 6:18
  • Pull-down resistor seemed to solve it, thank you! Commented Jul 22, 2014 at 15:13

1 Answer 1

1

You should look at the internal pull-up resistors on the Arduino. You can debounce the signal from your button entirely with software:

void setup() {
    pinMode(2, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(2) == LOW)  // NOTE THAT PULLUPS REVERSE YOUR LOGIC
  {
    delay(1000);  //wait 1 second
    digitalWrite(13, HIGH);
    delay(1000);                
    digitalWrite(13, LOW);  
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.