Hello Members
I am building DIY filament extruder and need help. The problem for me is a sketch that supports an even reel winding system. I succeeded to get moving guide from one limit switch to the other without a break. The idea is for the guide to move 1.75 or 2.85 mm with every spool turn, depending on the diameter of the filament produced. I need to add impulse generator (magnetic switch) activated with each revolution of the spool and two way switch changing guide movement distance from 1.75 to 2.85mm. How to add an additional switches to the sketch? And change continous motor movement into momentary?
I attached existing sketch:
// Declare Stepper Motor used pins
const int dirPin = 4;
const int stepPin = 3;
const int enPin = 2;
const int limit1 = 8;// left limit
const int limit2 = 12;// right limit
void setup() {
// Set up stepper motor
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
//Set up 2 limit switch
pinMode(limit1 , INPUT_PULLUP);
pinMode(limit2 , INPUT_PULLUP);
}
void loop() {
digitalWrite(dirPin, LOW); // setup motor turn left first
do {
digitalWrite(stepPin , HIGH);
delayMicroseconds(150);
digitalWrite(stepPin , LOW);
delayMicroseconds(150);
} while (digitalRead(limit1) == HIGH); // motor turn left until touch limit 1 handle
delay(500);
digitalWrite(dirPin, HIGH);// motor turn right after limit1 powered ( touch limit1 handle).
do {
digitalWrite(stepPin , HIGH);
delayMicroseconds(150);
digitalWrite(stepPin , LOW);
delayMicroseconds(150);
} while (digitalRead(limit2) == HIGH);// motor turn right until touch limit 2 handle
delay(500);
digitalWrite(dirPin, LOW);// motor turn left after touch limit 2 handle
}
Thanks in advance
Please read the topic "How to get the best out of this forum", especially how to post code.
What controller are You using?
Plase post lnk to the datasheet of the encoder.
Adding a switch.. Use INPUT_PULLUP. Connect the switch between GND and input pin.
EDIT:
Reorganice the code so it don't use delay if You want to check a switch.
My controller is Arduino Uno. motor nema17, driver tmc2208.
As an encoder I will use Reed switch and magnet attached to the pulley.
All switches are wired like on the picture:
// Declare Stepper Motor used pins
const int dirPin = 4;
const int stepPin = 3;
const int enPin = 2;
const int limit1 = 8;// left limit
const int limit2 = 12;// right limit
void setup() {
// Set up stepper motor
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
//Set up 2 limit switch
pinMode(limit1 , INPUT_PULLUP);
pinMode(limit2 , INPUT_PULLUP);
}
void loop() {
digitalWrite(dirPin, LOW); // setup motor turn left first
do {
digitalWrite(stepPin , HIGH);
delayMicroseconds(150);
digitalWrite(stepPin , LOW);
delayMicroseconds(150);
} while (digitalRead(limit1) == HIGH); // motor turn left until touch limit 1 handle
delay(500);
digitalWrite(dirPin, HIGH);// motor turn right after limit1 powered ( touch limit1 handle).
do {
digitalWrite(stepPin , HIGH);
delayMicroseconds(150);
digitalWrite(stepPin , LOW);
delayMicroseconds(150);
} while (digitalRead(limit2) == HIGH);// motor turn right until touch limit 2 handle
delay(500);
digitalWrite(dirPin, LOW);// motor turn left after touch limit 2 handle
}
The code is above.
I really don't know how to add switches to the code. Especially if there are Conditions like if switch1 is HIGH and switch 3is HIGH... etc. I really don't have background to write it...
R1 is not needed. You can use the built in pullup as told in #2.
Using delay like You do You "can't pick up the extra/new switch You tell about.
Study and test the following topics:
"Do several things at the same time".
"Blink without delay".