#include <AccelStepper.h>
// AccelStepper Setup
AccelStepper stepperL4(1, 2, 3); // Defines a stepper and the pins it will use
// Define the Pins used
#define step_pin 2 // Uno Pin 2 connected to A4988 STEP pin
#define dir_pin 3 // UNO Pin 3 connected to A4988 DIRECTION pin
#define home_switch 9 // UNO Pin 9 connected to Home Switch (MicroSwitch)
int direction; // Variable to set Rotation (CW-CCW) of the motor
int steps; // Used to set HOME position after Homing is completed
int analogPin = A0; // potentiometer wiper (middle terminal) connected to analog pin 0
int val = 0;
void setup() {
stepperL4.setMaxSpeed(2000);
stepperL4.setAcceleration(6000);
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(home_switch, INPUT_PULLUP);
pinMode(7, OUTPUT);
// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switch)) { // Do this until the switch is activated
digitalWrite(dir_pin, HIGH); // (HIGH = CCW / LOW = CW)
digitalWrite(step_pin, HIGH);
delay(1); // Delay to slow down speed of Stepper
digitalWrite(step_pin, LOW);
delay(1);
}
while (!digitalRead(home_switch)) { // Do this until the switch is not activated
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(1); // More delay to slow even more while moving away from switch
digitalWrite(step_pin, LOW);
delay(1);
}
steps=0; // Reset position variable to zero
}
void loop()
//Bandswitching
{
val = analogRead(analogPin);
if(val<5)
{
stepperL4.moveTo(-8000); //160m Inductor Position
stepperL4.run();
}
else if(val>138 && val<150)
{
stepperL4.moveTo(-7000); //80m Inductor Position
stepperL4.run();
}
else if(val>284 && val<300)
{
stepperL4.moveTo(-6000); //40m Inductor Position
stepperL4.run();
}
else if(val>430 && val<450)
{
stepperL4.moveTo(-5000); //20m Inductor Position
stepperL4.run();
}
else if(val>578 && val<600)
{
stepperL4.moveTo(-4000); //17m Inductor Position
stepperL4.run();
}
else if(val>724 && val<740)
{
stepperL4.moveTo(-3000); //15m Inductor Position
stepperL4.run();
}
else if(val>870 && val<890)
{
stepperL4.moveTo(-2000); //12m Inductor Position
stepperL4.run();
}
else if(val>1010)
{
stepperL4.moveTo(-1000); //10m Inductor Position
stepperL4.run();
}
}