Hello all,
For a Uni project I've been tasked with writing a program to control a stepper motor in a low torque and high torque configuration. It should also be able to change direction off of a momentary push button switch (and maintain direction change until pressed again) and speed adjustable.
Unfortunately I can't use the stepper library for the low torque function as it energizes 2 coils of the stepper motor simultaneously. Therefore, I've managed to write a program for the stepper motor where it energizes 1 coil at a time and to make it speed adjustable, however I can't figure out how to make the push button switch act as a toggle switch. I will use the stepper library though for the high torque function once I get that far.
I've broken the Fwd and Rev direction of the code into sub routines as I have more to add later on, such as, on, off and high torque push button switches, which I believe will easier to code if broken down into sub routines. Until I can get this right though I can't progress.
The code below has been amended multiple times now and I still can't quite get it but this is stripped back to the basics. As it is written below I have to hold the push button on to get the motor to rotate in the other direction, but I need it as a toggle type function in that every time the button is pressed the direction changes and remains changed until pressed again.
I'm completely new to Arduino and coding in general so any help would be greatly appreciated.
// Defines the connecting ports
int portIN1 = 8;
int portIN2 = 9;
int portIN3 = 10;
int portIN4 = 11;
const int SPEED_CONTROL = A0; // Potentiometer to control the speed of the motor.
const int button = 4; // Direction control button is connected to Arduino pin 4
void setup() {
pinMode(portIN1, OUTPUT);
pinMode(portIN2, OUTPUT);
pinMode(portIN3, OUTPUT);
pinMode(portIN4, OUTPUT);
pinMode(button, INPUT_PULLUP);} // configure button pin as input with internal pull up enabled
void loop()
{
if(digitalRead(button)==1){lowTorqueFwd();}
if(digitalRead(button)==0){lowTorqueRev();}
}
void lowTorqueFwd(){
// Instructs the Arduino to read the potentiometer value using the 10 bit ADC.
int sensorReading = analogRead(SPEED_CONTROL);
// Using an Arduino map command the value of the potentiometer from 0 to 100 and call it motorSpeed.
// The map command will scale the analogue input from 0 to 100 in relation to the ADC 0 to 1023 resolution.
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
digitalWrite(portIN1, HIGH);
digitalWrite(portIN2, LOW);
digitalWrite(portIN3, LOW);
digitalWrite(portIN4, LOW);
delay(motorSpeed);
digitalWrite(portIN1, LOW);
digitalWrite(portIN2, HIGH);
digitalWrite(portIN3, LOW);
digitalWrite(portIN4, LOW);
delay(motorSpeed);
digitalWrite(portIN1, LOW);
digitalWrite(portIN2, LOW);
digitalWrite(portIN3, HIGH);
digitalWrite(portIN4, LOW);
delay(motorSpeed);
digitalWrite(portIN1, LOW);
digitalWrite(portIN2, LOW);
digitalWrite(portIN3, LOW);
digitalWrite(portIN4, HIGH);
delay(motorSpeed);
}
void lowTorqueRev(){
int sensorReading = analogRead(SPEED_CONTROL);
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
digitalWrite(portIN1, LOW);
digitalWrite(portIN2, LOW);
digitalWrite(portIN3, LOW);
digitalWrite(portIN4, HIGH);
delay(motorSpeed);
digitalWrite(portIN1, LOW);
digitalWrite(portIN2, LOW);
digitalWrite(portIN3, HIGH);
digitalWrite(portIN4, LOW);
delay(motorSpeed);
digitalWrite(portIN1, LOW);
digitalWrite(portIN2, HIGH);
digitalWrite(portIN3, LOW);
digitalWrite(portIN4, LOW);
delay(motorSpeed);
digitalWrite(portIN1, HIGH);
digitalWrite(portIN2, LOW);
digitalWrite(portIN3, LOW);
digitalWrite(portIN4, LOW);
delay(motorSpeed);
}