I have made a setup with Arduino Uno, Nema 17, T8 lead screw, IR Sensor and a DC motor. I am trying to establish a code which:
- Resets Home position of the Machining Head on the lead screw
- Has an action button, which operates only when the position of the Head is on the lead screw, goes down the specified depth
- Takes input from user to run the DC Motor (to rotate the tool on the machine head)
- Takes second input to stop rotating and return to the reset position.
I am facing one major problem and two minor problems:
Major Problem: To remove an error which was later resolved, I added the line: action_permit=0 Now due to some reason (I have pulled a lot of hairs trying to understand this), when I remove this action_permit line, the DC Motor rotation is stopped. I have to include this line to rotate my DC Motor.
Minor Problem: To get input from the user as to control when the DC motor starts and stops, I included the code which accepts 'y' for yes (start rotating the DC motor) and 'n' as no (stop rotation and return to home position)
Minor Problem: I am not sure how to control the PWM, I have copied the code from a video, but do not know how to reach maximum and minimum rpm; also I understand the variation between PWM value and RPM is not linear. Any feedback on how to precisely control the rpm of the motor would be great
int IR_signal=2;
int DC_ENB=11;
int DC_IN3=4;
int DC_IN4=5;
int STE_dir=6;
int STE_step=7;
int STE_ena=10;
int button_reset=8;
int button_action=9;
//define arduino pin
int action_permit;//0-action part is not allowed to run. 1-action part is allowed to run
int IRon;
int COUNT;//the amount of steps that the machine head should go
int HEIGHT;//total height that the machine head should go
int i;
int reset_on;
int action_on;
String Switch_DC;
//parameter
void setup() {
// put your setup code here, to run once:
pinMode(IR_signal,INPUT);
pinMode(button_reset,INPUT);
pinMode(button_action,INPUT);
//input part
pinMode(DC_ENB,OUTPUT);
pinMode(DC_IN3,OUTPUT);
pinMode(DC_IN4,OUTPUT);
pinMode(STE_dir,OUTPUT);
pinMode(STE_step,OUTPUT);
pinMode(STE_ena,OUTPUT);
//output part
action_permit=0;// ban the action part at the beginning
COUNT=0;
HEIGHT=0;
//original value of parameters
//digitalWrite(DC_ENB,HIGH);
analogWrite(DC_ENB,200);
digitalWrite(STE_ena,LOW);
digitalWrite(DC_IN3,HIGH);
digitalWrite(DC_IN4,HIGH);
//original pin output
Serial.begin(9600);
// start the monitor
}
void loop() {
// put your main code here, to run repeatedly:
reset_on=digitalRead(button_reset);
action_on=digitalRead(button_action);
//read the signal from the button
if(reset_on==0)//reset button is pressed
{
delay(10);
// preventing manual error
if(reset_on==0)
{
RES();
}
}
if(action_on==0)//action button is pressed
{
delay(10);
// preventing manual error
if(action_on==0)
{
ACT();
}
}
}
void RES()//reset part
{
while(action_permit==0)//when 0, this "while" loop will keep running
{
IRon=digitalRead(IR_signal);// read the signal from IR sensor
if(IRon==1)// machine head is not in original position
{
delay(1);
//ensure the signal is not happened by a BUG
IRon=digitalRead(IR_signal);
if(IRon==1)
{
digitalWrite(STE_dir,LOW);
digitalWrite(STE_step,HIGH);
delay(1);
digitalWrite(STE_step,LOW);
delay(1);
//if machine head is not in original position, it will go up for one step
}
}//if HIGH, go up
else if(IRon==0)
{
action_permit=1;
COUNT=0;
}//once machine head reaches the original position, controller will get out this loop and allow action part to run
}
}
void ACT()//action part
{
if(action_permit==1)
{
for(i=0;i<3523;i+=1)
{
digitalWrite(STE_dir,HIGH);
digitalWrite(STE_step,HIGH);
delay(1);
digitalWrite(STE_step,LOW);
delay(1);
}
delay(100);
for(i=0;i<100;i+=1)
{
digitalWrite(STE_step,HIGH);
delay(10);
digitalWrite(STE_step,LOW);
delay(10);
}
action_permit=0;
Serial.println("Please press y to start tool rotation");
while((Serial.available()==0))
{
//waiting for input
}
Switch_DC=Serial.readString();
if (Switch_DC == "y")
action_permit=0;
{
analogWrite(DC_ENB,200);
digitalWrite(DC_IN3,HIGH);
digitalWrite(DC_IN4,LOW);
delay(2000);
}
Serial.println("Please press n to stop tool rotation");
while((Serial.available()==0))
{
//waiting for input
}
Switch_DC=Serial.readString();
if(Switch_DC == "n")
action_permit=0;
{
analogWrite(DC_ENB,200);
digitalWrite(DC_IN3,LOW);
digitalWrite(DC_IN4,LOW);
}
digitalWrite(STE_dir,LOW);
for(i=0;i<4523;i+=1)
{
digitalWrite(STE_step,HIGH);
delay(1);
digitalWrite(STE_step,LOW);
delay(1);
}
action_permit=0;
}
}