We get the physical setup for the etch a sketch and run some sample code of drawing rectangle and diamond. The prototype setup is quite simple. Check out the final set up as well as the sample video in our next post
Here is the code:
Rectangle:
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMS.getStepper(200, 2);
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
// FORWARD->clockwise, BACKWARD->counterclockwise
// step(#steps, orientation, step mode)
void setup(){
Serial.begin(9600);
AFMS.begin();
myStepper1->setSpeed(50);
myStepper2->setSpeed(50);
}
void loop() {
myStepper1->step(200, FORWARD, SINGLE);
myStepper2->step(200, FORWARD, SINGLE);
myStepper1->step(200, BACKWARD, SINGLE);
myStepper2->step(200, BACKWARD, SINGLE);
}
Diamond:
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMS.getStepper(200, 2);
void forwardstep1() {
myStepper1->onestep(FORWARD, SINGLE);
}
void backwardstep1() {
myStepper1->onestep(BACKWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {
myStepper2->onestep(FORWARD, SINGLE);
}
void backwardstep2() {
myStepper2->onestep(BACKWARD, SINGLE);
}
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);
void setup()
{
AFMS.begin();
stepper1.setMaxSpeed(200.0);
stepper1.setAcceleration(100.0);
stepper1.moveTo(800);
stepper2.setMaxSpeed(200.0);
stepper2.setAcceleration(100.0);
stepper2.moveTo(800);
}
void loop()
{
if(stepper1.distanceToGo() == 400)
stepper1.moveTo(-stepper1.currentPosition());
if(stepper2.distanceToGo() == 0)
stepper2.moveTo(-stepper2.currentPosition());
stepper1.run();
stepper2.run();
}
No comments:
Post a Comment