Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TMCStepper.h>
- #include <SoftwareSerial.h>
- // Define pins
- int upPin = A0;
- int downPin = A1;
- int upSpeed;
- int downSpeed;
- int upInput;
- int downInput;
- int enableInput = 4;
- int enableOutput = 5;
- const int TX_PIN = 7;
- const int RX_PIN = 8;
- #define dirPin 2
- #define stepPin 3
- // Define motor parameters
- const float R_SENSE = 0.11f;
- const uint8_t DRIVER_ADDRESS = 0b00;
- const int MICROSTEPS = 32;
- const int STEPS_PER_REVOLUTION = 200;
- // UART communication
- SoftwareSerial softSerial(RX_PIN, TX_PIN);
- TMC2209Stepper driver(&softSerial, R_SENSE, DRIVER_ADDRESS);
- void setup() {
- Serial.begin(9600); // Initialize serial communication for debugging
- pinMode(stepPin, OUTPUT);
- pinMode(dirPin, OUTPUT);
- pinMode(enableInput,INPUT);
- pinMode(enableOutput, OUTPUT);
- digitalWrite(enableOutput, HIGH);
- softSerial.begin(19200); // Initialize software serial for TMC2209
- driver.begin();
- driver.toff(5);
- driver.rms_current(600);
- driver.microsteps(MICROSTEPS);
- driver.en_spreadCycle(false); // Disable SpreadCycle
- driver.pwm_autoscale(true); // Enable automatic current scaling
- driver.pwm_freq(2); // Set PWM frequency
- Serial.println("Driver initialized successfully"); // Debug statement
- }
- void loop() {
- upInput = analogRead(upPin);
- upSpeed = map(upInput, 0, 1023, 45, 3);
- downInput = analogRead(downPin);
- downSpeed = map(downInput, 0, 1023, 45, 3);
- //Serial.print("UP ");
- //Serial.print(upInput);
- //Serial.print(" DOWN ");
- //Serial.println(downInput);
- //Serial.println(digitalRead(enableInput));
- if(digitalRead(enableInput)==1){
- digitalWrite(enableOutput, LOW);
- }else{
- digitalWrite(enableOutput, HIGH);
- }
- if(upInput> 40){
- digitalWrite(dirPin, LOW);
- digitalWrite(stepPin, HIGH);
- delay(upSpeed);
- digitalWrite(stepPin, LOW);
- delay(upSpeed);
- }
- if(downInput> 40){
- digitalWrite(dirPin, HIGH);
- digitalWrite(stepPin, HIGH);
- delay(downSpeed);
- digitalWrite(stepPin, LOW);
- delay(downSpeed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement