SHARE
TWEET

Untitled

a guest Jun 25th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // this constant won't change:
  2. const int  buttonPin = 2;    // the pin that the pushbutton is attached to
  3. const int ledPin = 7;       // the pin that the LED is attached to
  4.  
  5. // Variables will change:
  6. int buttonPushCounter = 0;   // counter for the number of button presses
  7. int buttonState = 0;         // current state of the button
  8. int lastButtonState = 0;     // previous state of the button
  9.  
  10. #include <Wire.h>
  11. #include <Adafruit_MotorShield.h>
  12. #include <AccelStepper.h>
  13.  
  14. Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  15. Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
  16.  
  17. void forwardstep1() {
  18.   myMotor->onestep(FORWARD, DOUBLE);
  19. }
  20. void backwardstep1() {
  21.   myMotor->onestep(BACKWARD, DOUBLE);
  22. }
  23.  
  24. AccelStepper Astepper1(forwardstep1, backwardstep1);
  25.  
  26. void setup() {
  27.   // initialize the button pin as a input:
  28.   pinMode(buttonPin, INPUT);
  29.   // initialize the LED as an output:
  30.   pinMode(ledPin, OUTPUT);
  31.   // initialize serial communication:
  32.   Serial.begin(9600);
  33.   AFMS.begin();  // create with the default frequency 1.6KHz
  34.   //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  35.   TWBR = ((F_CPU / 500000l) - 16) / 2; // Change the i2c clock to 400KHz
  36.   Astepper1.setAcceleration(6000.0);
  37.   Astepper1.setMaxSpeed(600);
  38.   Astepper1.setSpeed(600);    // rpm
  39.   //  while (switchIsOff) {
  40.   //turn servo one direction -
  41.   //tell servo this is 0
  42. }
  43.  
  44. void loop() {
  45.   // read the pushbutton input pin:
  46.   buttonState = digitalRead(buttonPin);
  47.   // compare the buttonState to its previous state
  48.   if (buttonState != lastButtonState) {
  49.     // if the state has changed, increment the counter
  50.     if (buttonState == HIGH) {
  51.       // if the current state is HIGH then the button went from off to on:
  52.       buttonPushCounter++;
  53.       Serial.println("on");
  54.       Serial.print("number of button pushes: ");
  55.       Serial.println(buttonPushCounter);
  56.     } else {
  57.       // if the current state is LOW then the button went from on to off:
  58.       Serial.println("off");
  59.   }
  60.   // save the current state as the last state, for next time through the loop
  61.   lastButtonState = buttonState;
  62.  
  63.   if (buttonPushCounter == 10) {
  64.     digitalWrite(ledPin, HIGH);
  65.     Astepper1.moveTo(600);
  66.     Astepper1.setSpeed(600);
  67.     Astepper1.run();
  68.   }
  69.  
  70.   if (buttonPushCounter == 30) {
  71.     digitalWrite(ledPin, LOW);
  72.     Astepper1.moveTo(200);
  73.     Astepper1.setSpeed(600);
  74.     Astepper1.run();
  75.   }
  76.  
  77.   }
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top