Go Down

Topic: Project 12 bad servo? (Read 602 times) previous topic - next topic

grayfox42

Hello, I have updated the code on line 53 with the numberOfKnocks = 0. It seems the code and circuit work. 
Except with the servo connected my value reading are 113 to 199, with random drops to 93, 46, and 16 flashing the yellow Led and unlocking without tapping.
With servo out of circuit values are 2 to 4 and tapping 3 times flash the yellow Led and turn on the green Led, unlocking.
Same result when center wire of servo connected to pin 9.
With center wire and gnd. connected values 1 to 6 spiking randomly to 12, 13, and 14, flashing yellow Led and unlocking without tapping.
All wiring and code is right out of the project book except for the added line of code.
Oh yes, I am very new to this and have many hours in this hour project. Do I in fact have a bad servo?
Thank for the help in advance
      JP

pert

If anyone is looking at this who doesn't have the book, the code should also be available via the Arduino IDE at File > Examples > 10.StarterKit_BasicKit > p12_KnockLock or here:
https://github.com/arduino/Arduino/blob/master/build/shared/examples/10.StarterKit_BasicKit/p12_KnockLock/p12_KnockLock.ino
Code: [Select]
/*
  Arduino Starter Kit example
  Project 12 - Knock Lock

  This sketch is written to accompany Project 12 in the Arduino Starter Kit

  Parts required:
  - 1 megohm resistor
  - 10 kilohm resistor
  - three 220 ohm resistors
  - piezo
  - servo motor
  - push button
  - one red LED
  - one yellow LED
  - one green LED
  - 100 uF capacitor

  created 18 Sep 2012
  by Scott Fitzgerald
  Thanks to Federico Vanzati for improvements

  http://www.arduino.cc/starterKit

  This example code is part of the public domain.
*/

// import the library
#include <Servo.h>
// create an instance of the Servo library
Servo myServo;

const int piezo = A0;      // pin the piezo is attached to
const int switchPin = 2;    // pin the switch is attached to
const int yellowLed = 3;    // pin the yellow LED is attached to
const int greenLed = 4;    // pin the green LED is attached to
const int redLed = 5;   // pin the red LED is attached to

// variable for the piezo value
int knockVal;
// variable for the switch value
int switchVal;

// variables for the high and low limits of the knock value
const int quietKnock = 10;
const int loudKnock = 100;

// variable to indicate if locked or not
bool locked = false;
// how many valid knocks you've received
int numberOfKnocks = 0;

void setup() {
  // attach the servo to pin 9
  myServo.attach(9);

  // make the LED pins outputs
  pinMode(yellowLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);

  // set the switch pin as an input
  pinMode(switchPin, INPUT);

  // start serial communication for debugging
  Serial.begin(9600);

  // turn the green LED on
  digitalWrite(greenLed, HIGH);

  // move the servo to the unlocked position
  myServo.write(0);

  // print status to the Serial Monitor
  Serial.println("the box is unlocked!");
}

void loop() {

  // if the box is unlocked
  if (locked == false) {

    // read the value of the switch pin
    switchVal = digitalRead(switchPin);

    // if the button is pressed, lock the box
    if (switchVal == HIGH) {
      // set the locked variable to "true"
      locked = true;

      // change the status LEDs
      digitalWrite(greenLed, LOW);
      digitalWrite(redLed, HIGH);

      // move the servo to the locked position
      myServo.write(90);

      // print out status
      Serial.println("the box is locked!");

      // wait for the servo to move into position
      delay(1000);
    }
  }

  // if the box is locked
  if (locked == true) {

    // check the value of the piezo
    knockVal = analogRead(piezo);

    // if there are not enough valid knocks
    if (numberOfKnocks < 3 && knockVal > 0) {

      // check to see if the knock is in range
      if (checkForKnock(knockVal) == true) {

        // increment the number of valid knocks
        numberOfKnocks++;
      }

      // print status of knocks
      Serial.print(3 - numberOfKnocks);
      Serial.println(" more knocks to go");
    }

    // if there are three knocks
    if (numberOfKnocks >= 3) {
      // unlock the box
      locked = false;

      // move the servo to the unlocked position
      myServo.write(0);

      // wait for it to move
      delay(20);

      // change status LEDs
      digitalWrite(greenLed, HIGH);
      digitalWrite(redLed, LOW);
      Serial.println("the box is unlocked!");

      numberOfKnocks = 0;
    }
  }
}

// this function checks to see if a detected knock is within max and min range
bool checkForKnock(int value) {
  // if the value of the knock is greater than the minimum, and larger
  // than the maximum
  if (value > quietKnock && value < loudKnock) {
    // turn the status LED on
    digitalWrite(yellowLed, HIGH);
    delay(50);
    digitalWrite(yellowLed, LOW);
    // print out the status
    Serial.print("Valid knock of value ");
    Serial.println(value);
    // return true
    return true;
  }
  // if the knock is not within range
  else {
    // print status
    Serial.print("Bad knock value ");
    Serial.println(value);
    // return false
    return false;
  }
}


grayfox42, it actually sounds like the code in the book is slightly different from the code in the book, since you say you needed to add numberOfKnocks = 0 to the code, while in the example sketch there is already the line:
51:
Code: [Select]
int numberOfKnocks = 0;
Please try running the unmodified example sketch File > Examples > 10.StarterKit_BasicKit > p12_KnockLock and let us know if you have the same results.

grayfox42

Thanks for the help. I did notice that the example contained the code the book left out. I have loaded the example code and the results are as follows.
With servo in the circuit, green Led lights, serial data reads box unlocked.
Pushed switch button red Led lights, serial data reads bad knock value 156 to 168, and no response to tap. Stays red. No movement from servo.
Then I simply unplug the servo and the arduino board, plug the board back in and and the green Led lights, serial data reads box unlocked. I push the switch button the red Led lights, serial data reads a value of 1 to 2 or3. It then responds to taps with the yellow Led flashing and after 3 taps and flashes, green Led comes on and box is unlocked. tap values were 21, 46, and 58. This is the same as it did with the code I copied from the book with the added line of code. The problem seems to be from the 156 to 168 value that is always present with the servo plugged in.
Also the servo worked fine for project 5 the mood cue.
Any ideas? Bad servo? I hate to give up on a project.
JP  

grayfox42

One more update, just built project 5 again and the servo worked as before. This is still a mystery.
JP

Go Up