Skip to content

Instantly share code, notes, and snippets.

@jackdoerner
Created May 4, 2019 01:56
Show Gist options
  • Save jackdoerner/16f811d62db0e0651f7d246d55114413 to your computer and use it in GitHub Desktop.
Save jackdoerner/16f811d62db0e0651f7d246d55114413 to your computer and use it in GitHub Desktop.
/*
telescopecamera.ino
Driver for McCormick Tracking Camera
Copyright (c) 2019 Jack Doerner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <AccelStepper.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#define FAST_STEP_MODE DOUBLE
#define STEP_MODE DOUBLE
#define STEPS_PER_REV 1000
#define HALFRANGE (STEPS_PER_REV * 20)
#define SPEED (STEPS_PER_REV/8)
#define FASTSPEED (SPEED * 4)
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(STEPS_PER_REV, 1);
Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(STEPS_PER_REV, 2);
bool oneup = false;
bool onedown = false;
bool twoup = false;
bool twodown = false;
bool currentmode = false;
float currentspeed = 0;
void forwardstep1() {
myMotor1->onestep(FORWARD, currentmode ? FAST_STEP_MODE : STEP_MODE);
}
void backwardstep1() {
myMotor1->onestep(BACKWARD, currentmode ? FAST_STEP_MODE : STEP_MODE);
}
void forwardstep2() {
myMotor2->onestep(FORWARD, currentmode ? FAST_STEP_MODE : STEP_MODE);
}
void backwardstep2() {
myMotor2->onestep(BACKWARD, currentmode ? FAST_STEP_MODE : STEP_MODE);
}
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
pinMode(2, INPUT);
digitalWrite(2, HIGH);
pinMode(3, INPUT);
digitalWrite(3, HIGH);
pinMode(4, INPUT);
digitalWrite(4, HIGH);
pinMode(5, INPUT);
digitalWrite(5, HIGH);
pinMode(7, INPUT);
digitalWrite(7, HIGH);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
stepper1.setMaxSpeed(SPEED);
stepper1.setAcceleration(SPEED);
stepper1.setCurrentPosition(0);
stepper2.setMaxSpeed(SPEED);
stepper2.setAcceleration(SPEED);
stepper2.setCurrentPosition(0);
}
void loop() {
if (digitalRead(7) == LOW) {
currentmode=false;
currentspeed=SPEED;
} else {
currentspeed=FASTSPEED;
currentmode=true;
}
if (digitalRead(2) == LOW) {
if (!twoup) {
stepper2.moveTo(HALFRANGE);
stepper2.setSpeed(currentspeed);
}
stepper2.runSpeedToPosition();
twoup = true;
twodown = false;
} else if (digitalRead(5) == LOW) {
if (!twodown) {
stepper2.moveTo(-1*HALFRANGE);
stepper2.setSpeed(-1*currentspeed);
}
stepper2.runSpeedToPosition();
twoup = false;
twodown = true;
} else {
twoup = false;
twodown = false;
}
if (digitalRead(4) == LOW) {
if (!oneup) {
stepper1.moveTo(HALFRANGE);
stepper1.setSpeed(currentspeed);
}
stepper1.runSpeedToPosition();
oneup = true;
onedown = false;
} else if (digitalRead(3) == LOW) {
if (!onedown) {
stepper1.moveTo(-1 * HALFRANGE);
stepper1.setSpeed(-1 * currentspeed);
}
stepper1.runSpeedToPosition();
oneup = false;
onedown = true;
} else {
oneup = false;
onedown = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment