// arduino 1 master serovo
#include <Servo.h>
Servo servo;
const int stepPin = 6; // Step pin for the stepper driver
const byte interruptPin = 3; // emergency stop input
const byte stopbutton = 2; // emergency stop input
const int ar1out = 8; // start command pin from arduino 1 to arduino 2 pin 8
const int ar2out = 9; // stop command pin from arduino 1 to arduino 2 pin number 3
const int but1 = 4; // button 1 start
const int but2 = 5; // button 2 stop
const int enablepin = 11;
const int initialspeed = 11; // not used
int flag1,flag2,runflag;
float delayTime =0;
// changeable below parameters
const int startdelay = 3000; // arduino 2 time
const int startdelay2 = 3000; // steper start time after relay start
const int speed1 = 550; // 50percent speed in pulse per second
const int time1= 5000; // step1 stepper 0 to 50 time
const int time2 = 5000; //step2 stepper 50 to 100 time
void setup()
{
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(enablepin, OUTPUT);
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), intr, CHANGE);
attachInterrupt(digitalPinToInterrupt(stopbutton), stop, CHANGE);
pinMode(but1, INPUT);
pinMode(but2, INPUT);
pinMode(ar1out, OUTPUT);
pinMode(ar2out, OUTPUT);
digitalWrite(enablepin, LOW);
digitalWrite(ar1out, LOW);
digitalWrite(ar2out, HIGH);
delay(1000);
}
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void loop()
{
delayTime = 0;
Serial.println("strated");
digitalWrite(ar1out, LOW);
flag1= digitalRead(but1);
if((flag1 == 1) && (runflag ==0)
{
runflag = 1;
digitalWrite(ar2out, LOW);
digitalWrite(ar1out, HIGH);
delay(1000);
digitalWrite(ar1out, LOW);
flag1=0;
runflag = 1;
delay(startdelay);
digitalWrite(enablepin, HIGH);
controlStepper(initialspeed, speed1, time1);
}
else if(runflag == 1)
{
idle();
}
}
void controlStepper(int initialSpeed, int finalSpeed, int accelTime)
{
// Calculate the total speed change and direction
float speedChange = finalSpeed - initialSpeed;
unsigned long startTime = millis();
unsigned long elapsedTime = 0;
while (elapsedTime <= accelTime)
{
// Update elapsed time
elapsedTime = millis() - startTime;
// Calculate the current speed based on elapsed time using linear interpolation
float currentSpeed = initialSpeed + speedChange * (float(elapsedTime) / accelTime);
// Make sure speed stays within the valid range
currentSpeed = constrain(currentSpeed, min(initialSpeed, finalSpeed), max(initialSpeed, finalSpeed));
// Calculate the delay between steps in microseconds
delayTime = 1000000.0 / currentSpeed;
// Generate a single step pulse if currentSpeed is above 0
if (currentSpeed > 0)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(delayTime / 2);
digitalWrite(stepPin, LOW);
delayMicroseconds(delayTime / 2);
}
else
{
break; // Stop if speed reaches 0
}
}
// Ensure the stepper motor stops completely by setting the stepPin to LOW
digitalWrite(stepPin, LOW);
// Return to exit the function after completing the cycle
return;
}
void intr() {
digitalWrite(ar1out, LOW);
digitalWrite(ar2out, HIGH);
digitalWrite(stepPin, LOW);
flag1= 0;
flag2 = 0;
digitalWrite(enablepin, HIGH);
delay(1000);
resetFunc();
}
void idle() {
digitalWrite(stepPin, HIGH);
delayMicroseconds(delayTime / 2);
digitalWrite(stepPin, LOW);
delayMicroseconds(delayTime / 2);
}
void stop() {
controlStepper(speed1, 0, time2); // Decelerate from 2000 to 0 steps/sec in 3 seconds
delay(stoptime);
controlStepper(11, speed1, time2); // Decelerate from 2000 to 0 steps/sec in 3 seconds
}