I am using a NEMA 23 4.25:1 Geared stepper motor with a TB6600 Driver and an original Arduino Mega 2560.
I know everything is wired up correctly (including a 16x2 I2C LCD and 12pos Rotary Switch)
I am trying to create a control box that I can select the amount of rotation of a stepper motor with a rotary switch (NOT a Potentiometer or Rotary encoder) and press one button for CW motion and another button for equal CCW motion and a display to tell me the amount of rotation I have selected
When I run a simple stepper motor sketch which only spins the motor constantly it works as it should
/* Example sketch to control a stepper motor with TB6600 stepper motor driver and Arduino without a library: continuous rotation. More info: https://www.makerguides.com */
// Define stepper motor connections:
#define dirPin 8
#define stepPin 9
void setup() {
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// Set the spinning direction CW/CCW:
digitalWrite(dirPin, HIGH);
}
void loop() {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(60);
digitalWrite(stepPin, LOW);
delayMicroseconds(60);
}
When I run my sketch the LCD displays everything as its meant to and the buttons do send either a CW or CCW signal but the motor just ticks off the steps like a second hand on a clock (I hope that makes sense) I have no idea what is wrong.
#include <LiquidCrystal_I2C.h>
#include <Wire.h> // Library for I2C communication
// Wiring: SDA pin is connected to 20 and SCL pin to 21.
// Connect to LCD via I2C, default address 0x27
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
////4-position rotary switch
const int firstRotaryPin = 23;
const int lastRotaryPin = 34;
// Define stepper motor connections:
#define dirPin 8
#define stepPin 9
int button1 = 2; ////button 1 input
int button2 = 3; ////button 2 input
int input1 = LOW;
int distance = 100;
int StepCounter = 0;
int Stepping = false;
void setup() {
// Initiate the LCD:
lcd.init();
lcd.backlight();
for ( int i = firstRotaryPin; i <= lastRotaryPin; i++) {
pinMode( i, INPUT);
digitalWrite( i, HIGH); // turn on internal pullup resistor
}
pinMode(button1, INPUT); ////for button 1 input
pinMode(button2, INPUT); ////for button 2 input
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
pinMode(2, INPUT);
pinMode(3, INPUT);
}
// returns the position of the rotary switch, 1-12
// or returns 0 if no rotary switch is hooked up
int getRotaryValue() {
for ( int i = firstRotaryPin; i <= lastRotaryPin; i++) {
int val = digitalRead( i ); // look at a rotary switch input
if ( val == LOW ) { // it's selected
return (i - firstRotaryPin + 1); // return a value that ranges 1 - 12
}
}
return 0; // error case
}
void loop() {
int rotaryPos = getRotaryValue();
lcd.setCursor(1, 0); // Set the cursor on the third column and first row.
lcd.print("#1=FWD #2=REV");
if ( rotaryPos == 1 ) {
distance = 425;
lcd.setCursor(2, 1); // Set the cursor on the third column and first row.
lcd.print("1/4 Rotation ");
}
else if ( rotaryPos == 2 ) {
distance = 850;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 1/2 Rotation ");
}
else if ( rotaryPos == 3 ) {
distance = 1275;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 3/4 Rotation ");
}
else if ( rotaryPos == 4 ) {
distance = 1700;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 1 Rotation ");
}
else if ( rotaryPos == 5 ) {
distance = 2125;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 2 Rotation ");
}
else if ( rotaryPos == 6 ) {
distance = 2550;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 3 Rotation ");
}
else if ( rotaryPos == 7 ) {
distance = 2975;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 4 Rotation ");
}
else if ( rotaryPos == 8 ) {
distance = 3400;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 5 Rotation ");
}
else if ( rotaryPos == 9 ) {
distance = 3825;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 7 Rotation ");
}
else if ( rotaryPos == 10 ) {
distance = 4250;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 8 Rotation ");
}
else if ( rotaryPos == 11 ) {
distance = 4675;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 9 Rotation ");
}
else if ( rotaryPos == 12 ) {
distance = 5100;
lcd.setCursor(0, 1); // Set the cursor on the third column and first row.
lcd.print(" 10 Rotation ");
}
input1 = digitalRead(button1);
if (input1 == LOW) { //Checking for button push
}
{
input1 = digitalRead(button2);
if (input1 == LOW) { //Checking for button push
}
if (input1 == HIGH) { //Checking for button push
}
}
if (digitalRead(3) == LOW && Stepping == false)
{
digitalWrite(dirPin, LOW);
Stepping = true;
}
if (digitalRead(2) == LOW && Stepping == false)
{
digitalWrite(dirPin, HIGH);
Stepping = true;
}
if (Stepping == true)
{
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
StepCounter = StepCounter + 1;
if (StepCounter == distance)
{
StepCounter = 0;
Stepping = false;
delay(1000);
}
}
}
I will admit from the start I am very new to this but I just cant wrap my head around what is wrong with my code.
Thanks
Craig