Unexpected Behavior with code

My code listens for a KNOCK and then moves a servo to a position, waits, and then moves it back and waits again. The problem is that it seems to be detecting KNOCKS and moving the servo while it it supposed to be "delaying" for x number of milliseconds. I read the following in the reference section for delay().

"No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt."

"Certain things do go on while the delay() function is controlling the Atmega chip however, because the delay function does not disable interrupts. Serial communication that appears at the RX pin is recorded, PWM (analogWrite) values and pin states are maintained, and interrupts will work as they should."

Any ideas?

Thanks!

Matt

// load servo library and create servo object
#include <Servo.h> 
Servo myServo;

// constants
const int knockSensor = A2; // the piezo is connected to analog pin 1
const int threshold = 3;  // threshold value to decide when the detected sound is a knock or not
const int servoPin = 6;  // the servo is connected to digital pin 6

// variables
int sensorReading = 0;      // variable to store the value read from the sensor pin

void setup() {
 myServo.attach(servoPin);  // attaches the servo on pin to the servo object 
 myServo.write(servoStartPos);
 delay(12000);
 Serial.begin(9600);       // serial port
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);    
  // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {
    Serial.println(sensorReading);
    Serial.println("1.5 Second delay");
    delay(1500);
    myServo.write(90);
    delay(12000);
    myServo.write(0);
    delay(75000);
  }
}

Your code looks alright... What exactly is happening with your servo? Do you have maybe a movie or something to show? I'm confused as to what exactly is happening...

baum

The servo will only stay at 90 degrees for about a second when it it supposed to delay for 12000 ms

It does not wait the 75000 ms. I can tap on the piezo when it is supposed to be delayed and it will move to 90 degrees. It is basically like the delays aren't there.

Thanks!

Matt

Your code doesn't compile, it's missing a definition for 'servoStartPos' in the setup function. Is there any other code missing?

Also, how are you powering the servo? If being powered from an arduino +5vdc pin, then perhaps it is drawing too much current from the board causing a reset condition and then restarting from the beginning? Powering a servo directly from an arduino board is a very iffy thing.

Lefty

The servo will only stay at 90 degrees for about a second when it it supposed to delay for 12000 ms

Are you sure that your arduino is not resetting when the servos move. This can happen if you are powering the servo and arduino from the same supply and you haven't got enough decoupling. This is because the servo generates interference.

To test this put a couple of blinks of the pin 13 LED in the setUp() section and see if they blink when the servo moves.

that's it...it is resetting. Thanks Baum, lefty and Mike!

    delay(75000);

The delay function takes an unsigned long argument, but literals are interpreted as ints unless the compiler is directed otherwise. 75000 will not fit in an int. If you really want to delay for 75 seconds, use:

    delay(75000UL);

Thanks Paul!

The servo is rated for 6 volts. If I power it with 4AA batteries should I hook up this way?

Servo

  • Pwr - to 4AAs
  • Grnd - to Arduino w/ neg from 4AAs
  • Signal - to Arduino pin

Thanks!

Matt

mthurman:
The servo is rated for 6 volts. If I power it with 4AA batteries should I hook up this way?

Servo

  • Pwr - to 4AAs
  • Grnd - to Arduino w/ neg from 4AAs
  • Signal - to Arduino pin

Thanks!

Matt

yes, a little more clearly:

Battery +6vdc (4 AA in series) to servo(s) power lead, usually red
Servo signal to Arduino output pin
Servo ground (usually black) to negative of battery pack
Negative battery pack to arduino ground pin

Lefty

Thanks!