#include <Servo.h> //servo library
Servo servoarm9; // create servo object to control a servo
Servo servoswitch10; // create servo object to control a servo
int pos = 0; // variable to store the servo position
// this constant won't change:
const int switchPin8 = 8;
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 7;
const int brakeB = 4;
const int dirA = 12;
const int dirB = 13;
// Variables will change:
int switchCounter = 0; // counter for the number of button presses
int switchState = 0; // current state of the button
int lastSwitchState = 0; // previous state of the button
void setup()
{
pinMode(switchPin8, INPUT); // set the switch pin to be an input
servoarm9.attach(9); // attaches the servo on pin 9 to the servo object
servoswitch10.attach(10); //attaches the servo on pin 10 to the servo object
//Setup Channel A
pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(dirB, OUTPUT); //Initiates Motor Channel B pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel B pin
Serial.begin(9600);
}
void loop()
{
switchState = digitalRead(switchPin8); // read the switch input pin
if (switchState != lastSwitchState) // compare the switchState to its previous state
{
if (switchState == HIGH) // if the state has changed, increment the counter
{
switchCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(switchCounter);
}
else
{
Serial.println("off");
}
}
lastSwitchState = switchState; // save the current state as the last state, for next time through the loop
// turns on the servo every 10 pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (switchCounter % 10 == 0)
{
//Motor A forward @ full speed
digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 255); //Spins the motor on channel A at full speed
//Motor B forward @ full speed
digitalWrite(dirB, HIGH); //Establishes forward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 255);
delay(3000);
digitalWrite(brakeA, HIGH); //Engage the Brake for Channel A
digitalWrite(brakeB, HIGH); //Engage the Brake for Channel B
delay(1000);
//Motor A backwards @ full speed
digitalWrite(dirA, LOW); //Establishes backward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 255); //full speed
//Motor B backwards @ half speed
digitalWrite(dirB, LOW); //Establishes backward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 127); //half speed
delay(3000);
//Motor A forward @ half speed
digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 127); //half speed
//Motor B forward @ full speed
digitalWrite(dirB, HIGH); //Establishes forward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 255); //full speed
delay(3000);
//Motor A backwards @ full speed
digitalWrite(dirA, LOW); //Establishes backward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 255); //full speed
//Motor B backwards @ half speed
digitalWrite(dirB, LOW); //Establishes backward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 127); //half speed
delay(3000);
//Motor A forward @ half speed
digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
analogWrite(pwmA, 127); //half speed
//Motor B forward @ full speed
digitalWrite(dirB, HIGH); //Establishes forward direction of Channel B
digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
analogWrite(pwmB, 255); //full speed
delay(3000);
digitalWrite(brakeA, HIGH); //engage the Brake for Channel A
digitalWrite(brakeB, HIGH); //engage the Brake for Channel B
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees, in steps of 1 degree
servoswitch10.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
else
{
// if the switch is closed:
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees // in steps of 1 degree
servoarm9.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
for(pos = 90; pos>=1; pos-=1) // if the switch is open:// goes from 90 degrees to 0 degrees
servoarm9.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
}