Pages: [1]   Go Down
Author Topic: Odd behavior shiftOut and Servo control  (Read 596 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
#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);

  }
}





« Last Edit: June 19, 2012, 08:42:06 am by RazorFly » Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple answer: lose the delays.

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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8437
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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);

  }

}



« Last Edit: June 19, 2012, 05:06:05 pm by RazorFly » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8437
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: