Just some quick background info. I'm developing a scanning system for a project that I'm doing at school. I'm new to arduino so I had to learn quickly but its not too tough of a task.
I basically want to serially control the stepper motors to make them move when I give inputs. I'll post my code then explain my problem.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Stepper.h>
Adafruit_MotorShield AFMSbot(0x60); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x61); // Default address, no jumpers
Adafruit_StepperMotor *myStepper0 = AFMSbot.getStepper(200, 2); // bottom motor
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1); // top motor
void setup() {
Serial.begin(9600);// set up Serial library at 9600 bps
AFMSbot.begin(); // Start the bottom shield
AFMStop.begin(); // Start the top shield
myStepper0->setSpeed(5);
myStepper1->setSpeed(5);
Serial.println("Enter A to move motor forward"); //prints to let user to know to press A to move forward
}
void loop(){
int incomingByte;
int motoroneforward;
if (Serial.available()>0) {
incomingByte = Serial.read();
if (incomingByte == 'A'){
motoroneforward = 1;
}
}
if (motoroneforward == 1){
myStepper0->step(10,FORWARD, MICROSTEP);
}
while(1) { } // this makes sure the program doesn't just loop endlessly
}
As opposed to waiting for me to enter A and move forward 10 steps, the program just moves the stepper motor forward 10 steps and then stops. Anything entered from the serial monitor is neglected.
I'm really new to this and having a lot of trouble. Any help is appreciated.
I'm new to arduino so I had to learn quickly but its not too tough of a task.
Apparently too quickly to read the rules of how to use this forum posted at the top of every forum section.
As opposed to waiting for me to enter A and move forward 10 steps, the program just moves the stepper motor forward 10 steps and then stops. Anything entered from the serial monitor is neglected.
Yes that is what you wrote.
while(1) { } // this makes sure the program doesn't just loop endlessly
at this point no other code can run the program enters an infinite loop so you can not move the motor any more and you can not respond to anything you send to the serial port.
In other words that code just runs once and stops. I doubt it does what you say, or if it does that is not the code.
I'm sorry for not following the rules. At the same time that was extremely rude, its not like I was trying to be malicious.
I fixed the post.
Anyways, since I have you're attention. Maybe you can take a moment away from needlessly insulting a stranger and perhaps helping me? That is what these forums are for right? Helping each other?
If I don't put this:
while(1) { } // this makes sure the program doesn't just loop endlessly
Even if i write a simple code telling a motor to move 100 steps (or even 1 step), it doesn't stop moving. Do you know a solution for this?
I thought the code I posted was supposed to wait for me to enter A in the serial monitor then move. Obviously I'm not understanding something (which is why I'm here). How could I modify this code to do what I want?
I'm sorry for not following the rules. At the same time that was extremely rude, its not like I was trying to be malicious.
Do you want help or not, insulting the person you hope to get it from is a bad stratagy. No you were not being malicious but failing to spot something so obvious is a bit of a test as to if you can understand any answer given here.
There is nothing in that code that will wait for anything, it runs once and hits the sand trap and goes no further.
The loop function is supposed to do just that loop forever. You have to write code that does what you want when you want it. A stepping motor will only move when you issue that function call, if the code can not get to it then there is no movement.
You are using the value of a variable without it ever being assigned, never a good idea, so when you are initialising it set it's value. Then while you set the variable to one you never change it so once triggered it will go on forever.
You are best calling the motor move function when you detect a letter A rather than setting a flag and then going off the value off that flag later.