Controlling the Motorshied v2 with a Display


I am new in this forum, and I am a beginner of Arduino. I realized a small project with a Arduino and the motorshield V2 and the DAVID 3D scanner. The code came from the DAVID community.

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Adafruit_PWMServoDriver.h>

int val; //Variable to store the content of the COM port
int count = 0; // Variable to count the scans
int limit = 12; // Means the number of scans/round
int StepsPerRound = 200; // Depends on the stepper’s + gear’s resolution

// 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 *myMotor = AFMS.getStepper(200, 2);

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps

AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz

myMotor->setSpeed(10); // 10 rpm

void loop() {
if (Serial.available() > 0) {
val =; // read if theres a message in the port and store it in the communication variable
if (val == ‘T’ && count < limit){
myMotor->step(StepsPerRound/limit, FORWARD, SINGLE); //Performs a turntable motion - This is needed for my Adafruit MotorShield on the Arduino
myMotor->release(); // Releases the stepper - This is needed for my Adafruit MotorShield on the Arduino
count +=1; //
if (val == ‘2’ && count > 0 && count < limit){
Serial.println(“S”); // Triggers the next scan when DAVID has sent its “ready” message
if (val == ‘2’ && count == limit){
count = 0; //Just to reset things when you finished a whole round
if (val == ‘1’){
count = 0; //To reset the counter after a faulty try switch to DAVID#s Calibration mode and return to ScanningMode
val = 0; // To refresh the COM port input

Every think is working fine when the scan of the David is starting. I just thinking about optimization, for example to control the lot of turn with a display and buttons. At the moment I am controlling the turn by " int limit = 12; // Means the number of scans/round"

but is there any possibility to control it with a display and buttons?

I would be very happy to find somebody who is helping me.

You don't control anything with a display do you? Yes you can control it with buttons, you need to plan what buttons do what.

You need to read the how to use this forum sticky post to see how to post code and ask a question.

That code is quite poorly structured. All the testing should be inside the if available scope not outside it. The code wastes time testing a value of zero.