I am having an issue using a button to control LEDs/Servo.
This is what I am attempting:
-
press button Once and is HIGH
a) "servo moves" -
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);
}
}