Hello,
I have two sketches which work separately but I cannot think of a way in which they would work together. I want sketch 1 to run when sketch 2 is running.
Sketch 1:
int dataPin = 2; //Define which pins will be used for the Shift Register control
int latchPin = 3;
int clockPin = 4;
int bits[] = {B10101010,B01010101}; //The byte sequence
void setup()
{
pinMode(dataPin, OUTPUT); //Configure each IO Pin
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
for (int n = 0; n < 2; n++)
{
digitalWrite(latchPin, LOW); //Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, MSBFIRST, bits[n]); //Send the data
digitalWrite(latchPin, HIGH); //Pull latch HIGH to stop sending data
delay(100);
}
}
Sketch 2:
#include <Servo.h>
const int buttonPin = 2;
int buttonState = 0;
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos; // variable to store the servo position
void setup()
{
pinMode(buttonPin, INPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
buttonState = digitalRead(buttonPin); // Read the button position
if (buttonState == HIGH) {
for(pos = myservo.read(); pos >=20; pos -= 1) { // goes from 90 degrees to 20 degrees in 1 step
myservo.write(pos); // tell servo to go to position in variable 'ONpos'
delay(15); // randomize wait time for the servo to reach the position
}
}
else {
for(pos = myservo.read(); pos <=90; pos += 1) {// goes from 20 degrees to 90 degrees in 1 step
myservo.write(pos); // tell servo to go to position in variable 'OFFpos'
delay(15); // randomize wait time for the servo to reach the position
}
}
}