Odd behavior shiftOut and Servo control

I am having an issue using a button to control LEDs/Servo.

This is what I am attempting:

  1. press button Once and is HIGH
    a) "servo moves"

  2. release button
    a) "servo moves back"
    b) LEDS in sequence for x then stay lit for 10 seconds
    c) command sent to reset sequence
    ============================================
    This is what is happening
    as soon as i press the button the leds light and the delays interfere with the servo movement(becomes jittery).

*** I tried running the && line as an else() statement...same issue

Suggestions would be nice:

#include <Servo.h>
#define clock 3
#define data 2
#define clock2 5
#define data2 4
#define clock3 7
#define data3 6
long interval = 125;     
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;   
int x = 50;
// this constant won't change:
const int  buttonPin = 13;    // the pin that the pushbutton is attached to
const int ledPin = 9;   

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

Servo myservo;  // create servo object to control a servo 
const int open= 85;
const int close = 31;
int val; 


byte zero = B00000000;
byte one  = B10000000;
byte two   = B11000000;
byte three = B11100000;
byte four  = B11110000;
byte five  = B11111000;
byte six   = B11111100;
byte seven = B11111110;

int y = 150;



void setup()
{
  myservo.attach(10); 
  pinMode(clock, OUTPUT); // make the clock pin an output
  pinMode(data, OUTPUT); // make the data pin an output3
  pinMode(clock2, OUTPUT); // make the clock pin an output
  pinMode(data2, OUTPUT); // make the data pin an output3
  pinMode(clock3, OUTPUT); // make the clock pin an output
  pinMode(data3, OUTPUT); // make the data pin an output3
  pinMode(8, OUTPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin (9600);  
  
}

void loop()
{
  unsigned long currentMillis = millis();
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
       Serial.println (buttonPushCounter);
      myservo.write(open);
    }
  

if(buttonState == LOW && buttonPushCounter == 1) {

    myservo.write(closeTrap); 
    delay(30);
    shiftOut(data, clock, LSBFIRST, one);
    delay(x);
    shiftOut(data, clock, LSBFIRST, two);
    delay(x);
    shiftOut(data, clock, LSBFIRST, three);
    delay(x);
    shiftOut(data, clock, LSBFIRST, four);
    delay(x);
    shiftOut(data, clock, LSBFIRST, five);
    delay(x);
    shiftOut(data, clock, LSBFIRST, six);
    delay(x);
    shiftOut(data, clock, LSBFIRST, seven);
    delay(x);
    shiftOut(data2, clock2, LSBFIRST, one);
    delay(x);
    shiftOut(data2, clock2, LSBFIRST, two);
    delay(x);
    shiftOut(data2, clock2, LSBFIRST, three);
    delay(x);
    shiftOut(data2, clock2, LSBFIRST, four);
    delay(x);
    shiftOut(data2, clock2, LSBFIRST, five);
    delay(x);
    shiftOut(data2, clock2, LSBFIRST, six);
    delay(x);
    shiftOut(data2, clock2, LSBFIRST, seven);
    delay(x);
    shiftOut(data3, clock3, LSBFIRST, one);
    delay(x);
    shiftOut(data3, clock3, LSBFIRST, two);
    delay(x);
    shiftOut(data3, clock3, LSBFIRST, three);
    delay(x);
    shiftOut(data3, clock3, LSBFIRST, four);
    delay(x);
    shiftOut(data3, clock3, LSBFIRST, five);
    delay(x);
    shiftOut(data3, clock3, LSBFIRST, six);
    delay(x);
    shiftOut(data3, clock3, LSBFIRST, seven);
    delay(x);
    digitalWrite(8, HIGH);
    delay(x);
 
   delay(1000);
    buttonPushCounter = 0;  
  }
  }
  lastButtonState = buttonState;

  if (buttonPushCounter == 0) {

    shiftOut(data, clock, LSBFIRST, zero);
    shiftOut(data2, clock2, LSBFIRST, zero);
    shiftOut(data3, clock3, LSBFIRST, zero);
    digitalWrite(8, LOW);
    digitalWrite(ledPin, LOW);
    myservo.write(close);

  }
}

Simple answer: lose the delays.

You will get much better results by examining the millis() return value and using that for your timing.

Well the delays worked just fine for what i needed before i used a shift register to sequence my leds.

RazorFly:
Well the delays worked just fine for what i needed before i used a shift register to sequence my leds.

Delays work fine when you're trying to do just 1 single task. Once you start attempting to perform multiple actions, delay() just throws all sorts of wrenches into the works.

The delay() function screws with interrupts, the Servo library uses timer interrupts, put those together and there's a good chance you will have jittery servos.

the delays worked just fine for what i needed before i used a shift register to sequence my leds.

Are you saying that if you take the exact same code and comment out all the shiftOut() calls the servo doesn't jitter?


Rob

This is what i had...then decided to run a shift register and it worked fine:

#include <Servo.h>

long interval = 1;     
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;   
int x = 50;
// this constant won't change:
const int  buttonPin = A0;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

Servo myservo;  // create servo object to control a servo 
const int open = 85;
const int close = 31;
int val; 

void setup() {
  pinMode(ledPin, OUTPUT);  
  myservo.attach(1); 
  pinMode(buttonPin, INPUT); 
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {

      buttonPushCounter++;
      myservo.write(open); 
      delay(10);
    } 
    else {
      
      myservo.write(close); 
      delay(x);
      digitalWrite(2, HIGH);
      delay (x);
      digitalWrite(3, HIGH);
      delay (x);
      digitalWrite(4, HIGH);
      delay (x);
      digitalWrite(5, HIGH);
      delay (x);
      digitalWrite(6, HIGH);
      delay (x);
      digitalWrite(7, HIGH);
      delay (x);
      digitalWrite(8, HIGH);
      delay (x);
      digitalWrite(9, HIGH);
      delay (x);
      digitalWrite(10, HIGH);
      delay (x);
      digitalWrite(11, HIGH);
      delay (x);
      digitalWrite(12, HIGH);
      delay (x);
      
      {
        unsigned long currentMillis = millis();
      if(currentMillis - previousMillis > interval) {
        // save the last time you blinked the LED 
        previousMillis = currentMillis;   

        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
          ledState = HIGH;
        else
          ledState = LOW;

        // set the LED with the ledState of the variable:
        digitalWrite(13, ledState);
      }
      }
    }
  }

  lastButtonState = buttonState;

  if (buttonPushCounter == 2) {
    buttonPushCounter = 0;
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);

  }

}

dont know what i did...but it works now.....no changes in code....maybe a loose wire...but thanks for your help.

Thanks as always

That happens, wiggling wires is one of the first debugging techniques.


Rob