I am currently programming a CNC machine using NEMA 17 Stepmotors.
At first i tried the demo just to see how this works and it was working fine at the beginning and without me changing anything it suddenly didnt go fine at all.
This is the code:
#include <Servo.h>
//Nema 17 stepsPerRev = 200;
// Y axis 100 Steps = 1,4 cm
// X axis 100 steps = 2,0 cm
const int estop = 30;
const int stepsFor_1cm = 1; //just for now, actually 80 for Y
const int StepX = 2;
const int DirX = 5;
const int StepY = 3;
const int DirY = 6;
volatile boolean active = false;
//const int StepZ = 4;
//const int DirZ = 7;
//--------------------------------------
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void touch(int stayDown_ms) //simulate the touch for CNC (z-axis)
{
for (pos = 80; pos >=15; pos -= 1) { // goes from 0 degrees to 60 degrees, takes pen down
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
// currPos=pos;
Serial.println(pos);
delay(15); // waits 15 ms for the servo to reach the position
}
delay(stayDown_ms); //stay down for how many milliseconds
for (pos = 15; pos<= 75; pos += 1) {// goes from 60 degrees to 0 degrees in steps of 1 degree, takes pen up
myservo.write(pos); // tell servo to go to position in variable 'pos'
// Serial.println(pos);
delay(15); // waits 15 ms for the servo to reach the position
}
}
void xMove_mm_right(int cm)
{
digitalWrite(DirX,HIGH);
for(int x = 0; x<stepsFor_1cm * cm ; x++)
{
// Serial.println(x);
digitalWrite(StepX,HIGH);
delayMicroseconds(500);
digitalWrite(StepX,LOW);
delayMicroseconds(500);
Serial.println(x);
}
// delay(1000);
}
void xMove_mm_left(int cm)
{
digitalWrite(DirX,LOW);
for(int x = 0; x<stepsFor_1cm * cm; x++)
{
// Serial.println(x);
digitalWrite(StepX,HIGH);
delayMicroseconds(500);
digitalWrite(StepX,LOW);
delayMicroseconds(500);
Serial.println(x);
}
// delay(1000);
}
void yMove_mm_back(int cm)
{
digitalWrite(DirY,HIGH);
for(int x = 0; x<stepsFor_1cm * cm ; x++)
{
digitalWrite(StepY,HIGH);
delayMicroseconds(500);
digitalWrite(StepY,LOW);
delayMicroseconds(500);
}
// delay(1000);
}
void yMove_mm_forward(int cm)
{
digitalWrite(DirY,LOW);
for(int x = 0; x<stepsFor_1cm * cm; x++)
{
digitalWrite(StepY,HIGH);
delayMicroseconds(500);
digitalWrite(StepY,LOW);
delayMicroseconds(500);
}
// delay(1000);
}
//---------------------------------------
void setup() {
Serial.begin(115200);
pinMode(StepX,OUTPUT);
pinMode(DirX,OUTPUT);
pinMode(StepY,OUTPUT);
pinMode(DirY,OUTPUT);
myservo.attach(45);
// pinMode(StepZ,OUTPUT);
// pinMode( DirZ,OUTPUT);
}
//----------------------------------------
void loop() {
xMove_mm_right(100);
//delay(1000);
//xMove_mm_left(80);
//delay(1000);
//yMove_mm_back(100);
//delay(1000);
//yMove_mm_forward(100);
//delay(1000);
//
//touch(700);
//
//delay(100);
exit(0);
}
So in real life what happens is that if i want to move 100 steps to the right it moves more than hundred and on 2 times, so it would go 20 steps to the right stop for half a second and then go again for like 100 steps.
Could someone help ? Thanks in advance !
because i dont want it to run forever 100 steps at a time. Because I am just testing the machine i want it to exit the arduino loop after it completes the functions in loop().
Yeah at the very beginning i got that too from 0-100 not missing an integer and doing the 100 steps without interruption. And suddenly without me changing anything in the code it did what i described at the very beginning.
I get the expected result from the code you posted, running on an Arduino Uno.
If you don't, the code you are running is not what you posted.
The obvious experiment is to download the code you posted, give it a different name, and try running it.
Of course, motor noise or an inadequate electrical setup may be causing the Arduino to malfunction and/or reset, so with the power off, disconnect the motors and try again.
It's literally the same code. And what's worse is that it had worked properly (as expected) but i don't know what happened, it just stopped working right and i got the result i posted.
This happened to me before, then all by itself it began working properly and now it's not.
What do you mean by giving the code another name ? The ino file or the functions ?
When i upload a blank file and get an error, or generally get an error in any other inofile or in this ino file, then it runs correctly but it runs the previous execution. So for example if i had written to move left and uploaded it and it didnt work properly, and then upload the same code with a planted error or another code with an error in it runs the previously uploaded file and it runs even correctly ?
You are trying to drive a stepper at 1000 steps a second from a standstill - this won't work.
Its essential to ramp the step rate up and down so the motor doesn't have to break the laws of physics. If you are new to steppers I'd strongly advise looking at one of the libraries that handles motor acceleration and deceleration correctly, like AccelStepper.
You are also calling a Serial function in the middle of a timing-critical loop, which might risk stalling the motor if the Serial buffer fills up. Hopefully that isn't going to happen as you have set the baudrate to a fast enough baud rate 115200.
[ For loops work. Whatever else is happening you can rely on that, otherwise everyone would have noticed! ]
Is you project built on solderless bread board. If so that is exactly the sort of thing you would expect because the push connections are not all that reliable.
The problem is it worked properly at the beginning. My Code is actually inspired by a demo for this shield which was also working. I also tried a simple for loop without anything just serial.println (as shown below):
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0;i<=100;i++)
{
Serial.println(i);
}
exit(0);
}
Hi,
I tried your code and got your output.
If I write your code like this;
bool for1OnceStatus = false;
void setup()
{
Serial.begin(115200);
Serial.println("===== for loop - one and only one ping =====");
}
void loop()
{
if (for1OnceStatus == false)
{
for (int i = 0; i <= 100; i++)
{
Serial.println(i);
}
for1OnceStatus = true;
}
}
It works as you want it.
I'm no serial comms expert, but I believe the Uart buffer has not completed its many 115200 speed data packets when the code encounters exit(0).
But I'm no serial expert.
Hi,
This works, so my hypothesis must be somewhat correct.
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop()
{
// put your main code here, to run repeatedly:
for (int i = 0; i <= 100; i++)
{
Serial.println(i);
delay(1);
}
exit(0);
}
If this code is going to be a stepping instruction, you will need more than 1ms delay between step commands anyway.