Pages: [1]   Go Down
Author Topic: N00b: pausing a loop  (Read 688 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1250
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

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

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:


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

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 517
Posts: 26293
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: