Servo, buttons, lights and arduino = problem

Just want to hang out with other Makers and chat about stuff? This is the place to do it.
Post Reply
Jed Hodson
Posts: 71
Joined: Wed Apr 24, 2013 5:14 am
Location: New South Wales, Australia

Servo, buttons, lights and arduino = problem

Post by Jed Hodson » Tue Jan 21, 2014 4:51 am

Hi,
I'm currently working on building a lock mechanism that monitors the state of a control line to then trigger a servo to move 90° to allow the user to unlock the door (Along with turning a light on). It then (is meant to) monitor the state of a reed switch to know when to lock the door and switch the lights off. But instead of doing that it is constantly going through the lock/unlock procedure regardless of the incoming signal or reed switch. I'm using an arduino mega and it all on a breadboard. The code should explain the pins and everything else. I have double checked wiring and all is good.
The Code:

Code: Select all

/*
Sketch to operate a servo door lock.
 © 21/01/2014 Jed Hodson
 */

#include <Servo.h> 

int pos = 0;
int doorState = 0;
int OPEN;
int CLOSED;
const int buzzer = 2;
const int lighting = 3;
const int doorReed = 4; 
const int lockTrig = 5;
const int ledPin = 13;

Servo lockservo; 

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(lighting, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(doorReed, INPUT);
  pinMode(lockTrig, INPUT);
  lockservo.attach(9);
  doorState = CLOSED;

  ledFlash(); //Flash led twice and sound buzzer when setup is complete
  delay(500);
  ledFlash();
  soundBuzzer();
  delay(2000);
}

void loop() {
  if(digitalRead(lockTrig == HIGH)) {
    doorState = OPEN;
  }else {
    doorState = CLOSED;
  }
  if (doorState == OPEN) {
    for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
    {                                  // in steps of 1 degree 
      lockservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    }
    digitalWrite(lighting, HIGH);
    ledFlash();
    soundBuzzer();
    delay(2000);
  }

  if(doorState == OPEN) {
    doorWait();
  }
}

void doorWait() {
  if(digitalRead(doorReed == HIGH)) {
    doorState = CLOSED;
    for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees 
    {                                
      lockservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    }
    digitalWrite(lighting, LOW);
    ledFlash();
    soundBuzzer();
    delay(500);

  }

  else{
    doorWait();
  }
}

void ledFlash() {
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
}

void soundBuzzer() {
  digitalWrite(buzzer, HIGH);
  delay(1000);
  digitalWrite(buzzer, LOW);
}

Jed Hodson

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Servo, buttons, lights and arduino = problem

Post by angusgr » Wed Jan 22, 2014 3:30 am

Hi Jed,

A couple of things spring to mind:

You may have to "debounce" the signal from the Reed switch to avoid it triggering many times on a single change. There's a good explanation of it on Wikipedia:
http://en.wikipedia.org/wiki/Debounce#Contact_bounce

A simple code example on the Arduino site here:
http://arduino.cc/en/Tutorial/Debounce

And an Arduino library to take care of it here:
https://github.com/thomasfredericks/Bou ... ino-Wiring

I often just delay() for a short time (say 250ms) after a switch triggers, as a "poor man's debouncing", if timing is not terribly important.

If debouncing doesn't take care of it, can you please post a sketch of the circuit schematic you are using? What is each side of the reed switch connected to?

- Angus

Jed Hodson
Posts: 71
Joined: Wed Apr 24, 2013 5:14 am
Location: New South Wales, Australia

Re: Servo, buttons, lights and arduino = problem

Post by Jed Hodson » Wed Jan 22, 2014 9:35 am

Hi angusgr,
Adding a delay helped. I also changed a lot of things around and used the while statement.
The code is now

Code: Select all

/*
Sketch to operate a servo door lock.
 © 22/01/2014 Jed Hodson
 */

#include <Servo.h> 

int pos = 0;
int val1 = 0;
int val2 = 0;
int buzzer = 2;
int lighting = 3;
int doorReed = 4; 
int lockTrig = 5;
int ledPin = 13;

Servo lockservo; 

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(lighting, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(doorReed, INPUT);
  pinMode(lockTrig, INPUT);
  lockservo.attach(9);

  ledFlash(); //Flash led twice and sound buzzer when setup is complete
  delay(500);
  ledFlash();
  soundBuzzer();
  delay(2000);
}

void loop() {
  val1 = digitalRead(lockTrig);
  if (val1== HIGH) { //If lock triggered 
    for(pos = 90; pos>=1; pos-=1)     // Servo goes from 90 degrees to 0 degrees 
    {                                
      lockservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    }
    ledFlash();
    soundBuzzer();
    digitalWrite(lighting, HIGH);
    delay(5000); //Wait for door to open fully
    val2 = digitalRead(doorReed);
    while(val2== LOW) { //Wait until Door is closed
      val2 = digitalRead(doorReed);
      delay(50);
    }
    delay(3000); //Wait 3re seconds for door to full close


    for(pos = 0; pos < 90; pos += 1)  // Servo goes from 0 degrees to 90 degrees 
    {                                  // in steps of 1 degree 
      lockservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    }
    ledFlash();
    soundBuzzer(); 
    digitalWrite(lighting, LOW);  
  }
  delay(50);
}

void ledFlash() {
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
}

void soundBuzzer() {
  digitalWrite(buzzer, HIGH);
  delay(500);
  digitalWrite(buzzer, LOW);
}


Jed Hodson

Post Reply