Hi all, I'm new to arduino and can't get why my code is not working al the time but just sometimes.
I'm making a flipping mechanism for a CNC machine where the wanted rotation is 2.25 shaft rounds what with a 4.5 reduction will be 180 degrees. The motor is connected with a driver and the inputs CW and CCW are connected with a relay to the CNC machine.
When i connect the relay the stepper starts rotating 2.25 rouds and stops but when i disconnect the relay it sometimes goes back (not always). Sometimes when i don't trigger a thing it just starts doing a 2.25 round rotation. Is there a way to make sure this only happens when the new signal is there?
Thanks in advance
int dirPin = 2; //Pin dir+ on motordriver
int stepPin = 3; //Pin pul+ on motordriver
int stepsPerRevolution = 4000; //microstep setting on driver
int Counterclockwise = 4; //inputsignal CCW
int Clockwise = 5; //Inputsignaal CW
#include <AccelStepper.h>;
int motorInterfaceType = 1; //motor is connected with driver
int solenoid;
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
//pin setup
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(Counterclockwise, INPUT_PULLUP);
pinMode(Clockwise, INPUT_PULLUP);
pinMode(solenoid, OUTPUT);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
}
void loop() {
int CW = digitalRead(Clockwise);
int CCW = digitalRead(Counterclockwise);
if ((CW == HIGH)and (CCW == LOW)) {
stepper.moveTo(9000);
stepper.runToPosition();
}
else if ((CCW == HIGH)and (CW == LOW)){
stepper.moveTo(0);
stepper.runToPosition();
}
else {
stepper. disableOutputs();
}
}
Thanks for the response! I added the files i tested the motor with 2 buttons instead of the relays so that's not in use anymore. I hope you can see the mistake.
The lack of twisted pair and tight layout suggests the issue is noise pickup by the
CCW and CW lines - all the high current wiring to the motors and PSU should be twisted pair for minimum EMI generation, and the signal lines each should be
shielded or twisted pair (individually to ground), to minimize EMI pick-up.
For steppers I twist A+ and A- together, also B+ and B-, then the two pairs I twist
together in the other direction - all with a cordless drill - makes a low EMI and
very flexible cable.
To give an update i tried the twisted wires but they didn't solve the problem yet, i found some other things that people around me suggested but it all didn't solve the problem till now! The things i changed are:
Powersuply for arduino also from cnc machine and not from USB (same GND)
Powersuply for driver also from cnc (same GND)
Twisted wires for stepper and CW & CCW lines
I got the hint to use an attachinterupt() function but got an error with this one. In examples the ISR is not declared at all but i get the error ("Senerio" 1 is not declared in this scope) can someone see what i miss?
#include <Arduino.h>
int dirPin = 4; //Pin dir- on motordriver
int stepPin = 5; //Pin pul- on motordriver
int stepsPerRevolution = 800; //microstep setting on driver
int Counterclockwise = 2; //inputsignal CCW
int Clockwise = 3; //Inputsignaal CW
int Flipped = 6;
#include <AccelStepper.h>;
int motorInterfaceType = 1; //motor is connected with driver
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
volatile byte CW = LOW;
volatile byte CCW = LOW;
void setup() {
//pin setup
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(Counterclockwise, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(Clockwise), Senario1, FALLING);
pinMode(Clockwise, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(Counterclockwise), Senerio2, FALLING);
pinMode(Flipped, OUTPUT);
stepper.setMaxSpeed(200);
stepper.setAcceleration(50);
Serial.println("Monitor waiting for signal");
}
void loop() {
if ((CW == HIGH)and (CCW == LOW )) {
Serial.println("Forward");
stepper.moveTo(1800);
stepper.runToPosition();
//Dit signaal zet de driver uit is dus voor energiebesparing
digitalWrite(Flipped, HIGH);
delay(1000);
digitalWrite(Flipped, LOW);
}
else if ((CCW == HIGH)and (CW == LOW)){
Serial.println("Back");
stepper.moveTo(0);
stepper.runToPosition();
//Dit signaal zet de driver uit is dus voor energiebesparing
digitalWrite(Flipped, HIGH);
delay(1000);
digitalWrite(Flipped, LOW);
}
}
void Senario1 {
CW1 = !CW;
}
void Senario2{
CCW2 = !CCW;
}
Are the control buttons distant from the Arduino? If so internal pullups are not enough (very weak), try 4k7 or 2k2 physical pullups on those lines, and even a 100nF to ground on them.
[ Ah, I think I can see the buttons on a bit of breadboard in that pic - add pullups on the breadboard and see if it works ]
The contol buttons on the breadboard are only used for the test! For the actual project the buttons are replaced with relays who are controlled by the CNC. I'll ad psysical pullups but for now it seems to work fine and the problem looked like a too tight setup on the Arduino board. This arduino screwboard seemed like the solution.