Hi
everyone
I'm using:
- Power source 12V 5A
-NEMA 23 stepper motor 200 steps/1.8°
-TB5860 Motor Driver
-Arduino UNO R3
The following code lets me, by pressing a button, to move my motor CW. I have another button that when i press it lets me move my motor CCW, and it will automatically move 300 steps. ( 540°)
I also have connected a limit switch, but that still requires to be programmed in order for it to work.
I found this tutorial online with its code that shows that when a limit switch is pressed, the motor will stop.
I want to replicate that and put it on my code.
The thing is I do not know what to copy and modify exactly from that code and where to put it.
[This is the code im using ][/// defines pins numbers
const int dirPin = 3;
const int stepPin = 2;
const int enPin = 6;
const int limitPin = 7;
const int switchOne = 8;
const int switchTwo = 9;
int p1buttonState = 0; // current state of the button
int lastp1buttonState = 0; // previous state of the button
int p2buttonState = 0; // current state of the button
int lastp2buttonState = 0; // previous state of the button
bool bPress = false;
bool isForward = false;
bool isBackward = false;
void setup() {
Serial.begin(9600);
pinMode( switchOne, INPUT_PULLUP);
pinMode( switchTwo, INPUT_PULLUP);
pinMode(limitPin,INPUT);
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
}
void loop() {
isForward = false;
isBackward = false;
p1buttonState = digitalRead(switchOne);
p2buttonState = digitalRead(switchTwo);
if (p1ButtonPress()) {
digitalWrite(dirPin,HIGH);
delay(5);
}
if (p2ButtonPress()) {
digitalWrite(dirPin,LOW);
delay(5);
}
if( isForward || isBackward ){
for(int x = 0; x < 300; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(1200);
digitalWrite(stepPin,LOW);
delayMicroseconds(1200);
}
}
}
bool p1ButtonPress()
{
bool isPress = false;
// compare the p1buttonState to its previous state
if (p1buttonState != lastp1buttonState) {
// if the state has changed, increment the counter
if (p1buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
isPress = true;
Serial.println("Plaer One score");
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
isForward = true;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastp1buttonState = p1buttonState;
return isPress;
}
bool p2ButtonPress()
{
bool isPress = false;
// compare the p1buttonState to its previous state
if (p2buttonState != lastp2buttonState) {
// if the state has changed, increment the counter
if (p2buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
isPress = true;
Serial.println("Plaer Two score");
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
isBackward = true;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastp2buttonState = p2buttonState;
return isPress;
}]
Regarding the limit switch, the red wire is connected to the arduino 5V
the black wire is connected to Arduino GND
The green/blue, which is the signal, is connected to pin7.
I tried copying
if( digitalRead(limitPin) == HIGH){
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
and to put it in several places of my own code but i get an error message (img attached).
What's highlighted in yellow is what I copied from the other code. I do not know if it is correctly placed or not... or if I have to modify something on it... ( im still very new to all this arduino mambo jambo)
Could anybody please provide me some help/guidance?
Thank you