hey guys. I really need help with my code.
I am trying to control the speed of the first code just like the second one. I was wondering if you guys have a suggestion. This is my first time using Arduino, I am a little bit lost
Thank you
// Define pins
int reverseSwitch = 2; // Push button for reverse
int driverPUL = 7; // PUL- pin
int driverDIR = 6; // DIR- pin
int spd = A0; // Potentiometer
// Variables
int pd = 500; // Pulse Delay period
boolean setdir = LOW; // Set Direction
// Interrupt Handler
void revmotor (){
setdir = !setdir;
}
void setup() {
pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
}
void loop() {
pd = map((analogRead(spd)),0,1023,2000,50);
digitalWrite(driverDIR,setdir);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
Into this code:
#include <Stepper.h>
const int stepsPerRevolution = 360; // change this to fit the number of steps per revolution
int t = 0;
int q = 0;
int r = 0;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
// set the speed at 20 rpm:
myStepper.setSpeed(25);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
while(q < 5) {
// step one revolution in one direction:
Serial.println("counterclockwise(push)");
myStepper.step(-75);
// delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise(relese)");
myStepper.step(75);
delay(1000);
q++;
}
while(r < 5) {
// step one revolution in one direction:
Serial.println("counterclockwise(push)");
myStepper.step(-50);
delay(10);
// step one revolution in the other direction:
Serial.println("counterclockwise(relese)");
myStepper.step(50);
delay(10);
r++;
}
while(t < 5) {
// step one revolution in one direction:
Serial.println("counterclockwise(push)");
myStepper.step(-100);
// delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise(relese)");
myStepper.step(100);
delay(1000);
t++;
}
}