N00b: pausing a loop

Hi all,

I'm quite fresh with arduino, and haven't seen a tutorial covering this.

I'm trying to make a switch trigger a servo in the way that it would move it (and turn on the LED at pin 13) and then ignore if the switch turns on or off for a certain amount of time, here 2 seconds.

I thought I would achieve this by inserting a delay(2000) after it reads the pin as being HIGH, but this results in the LED at pin 13 blinking at a 2 second interval.

Any hints are greatly appreciated!

In the end I would like to make the switch trigger a soundfile on the seeduino music shield, and ignore any switch changes until the soundfile has finished...

#include <Servo.h>
Servo servoMain; // Define our Servo


int ledPin = 13; // choose the pin for the LED
int inPin = 8;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status
int reset= 0; // var to not send out to many messages to servo

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
  
  servoMain.attach(5); // servo on digital pin 5
  Serial.println("Ready");
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
    // Run Servo
    servoMain.write(45);
    delay(2000);
    reset= 0;

  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
    if (reset == 0)
    {
    // Run Servo
    servoMain.write(0);
    }
    reset= 1;
    
    
  }
}

Look at the blink without delay example.

This is the same except instead of blinking a LED you don't want to move the servo for a set time. The technique of using millis() is identical.

Hi,

thanks for the tip. I understand the concept, but I still get the same result, the light blinking at the interval.

And even stranger to me, is the output printed, this is without touching the switch at all:
(first int is the previous value of 'val', second is 'val' as gotten fresh from the digital input, third is the updated 'previousVal' as 'val')

Ready
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
writing HIGH
0
1
1
writing LOW
1
0
0
..... and so on.

Heres my updated code:

#include <Servo.h>
Servo servoMain; // Define our Servo


int ledPin = 13; // choose the pin for the LED
int inPin = 9;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status
int previousVal= 1;

long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 2000;           // interval at which to blink (milliseconds)

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
  
  servoMain.attach(5); // servo on digital pin 5
  Serial.begin(9600);    //Start Serial Communications with PC
  Serial.println("Ready");
}

void loop(){
  
  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED  
  
    val = digitalRead(inPin);  // read input value
    
    if (val != previousVal) {
      Serial.println(previousVal);
      Serial.println(val);
      previousVal= val;
      Serial.println(previousVal);
      
    if (val == LOW) {         // check if the input is HIGH (button released)
      digitalWrite(ledPin, HIGH);  // turn LED OFF
      // Run Servo
      //servoMain.write(45);
      Serial.println("writing HIGH");
      previousMillis = currentMillis;

    } else {
      digitalWrite(ledPin, LOW);  // turn LED ON

      // Run Servo
      //servoMain.write(0);
      Serial.println("writing LOW");
      
    
    }
    }
  }
}

How do you have wired your switch? Is it possible that you're measuring a floating pin?

Try this simple addition:'
pinMode(inPin, INPUT); // declare pushbutton as input
digitalWrite (inPin, HIGH); // enable internal pullup resistor, assumes trigger pulls pin Low for a trigger.