Filiment puller with width sensor.

Hello,

I have been trying to debug this simple code however it seems to do what it wants.

/*
   Simple demo, should work with any driver board

   Connect STEP, DIR as indicated

   Copyright (C)2015 Laurentiu Badea

   This file may be redistributed under the terms of the MIT license.
   A copy of this license has been included with this distribution in the file LICENSE.
*/
#include <Arduino.h>
#include "BasicStepperDriver.h"

// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define MOTOR_STEPS 200

// All the wires needed for full functionality
#define DIR 55
#define STEP 54
//Uncomment line to use enable/disable functionality
#define ENBL 38

// Since ****microstepping is set externally****, make sure this matches the selected mode
// 1=full step, 2=half step etc.
#define MICROSTEPS 1

// 2-wire basic config, microstepping is hardwired on the driver
//BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP);

//Uncomment line to use enable/disable functionality
BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP, ENBL);

 float setpoint = 1.75;  //Desired filament width
 int b = 0.05;          //speed change increment


// motor 1 puller width sensor


int sensorPin = A3; // width sensor

float snelheid1 = 0;  // width variable for speed adjustment


void setup() {

   Serial.begin(250000);// initialize serial communication at 9600 bien

  pinMode(sensorPin, INPUT);
}
void loop() {   {
  
    stepper.setRPM(snelheid1 * 10);  // needed to multiply the output to get decent speeds
    
    // energize coils - the motor will hold position
    // stepper.enable();

    stepper.setMicrostep(MICROSTEPS);//Tell the driver the microstep level we selected.  If mismatched, the motor will move at a different RPM than chosen.


    stepper.rotate(360);// Moving motor one full revolution using the degree notation


    int sensorValue = analogRead (A3);             // read the input on analog pin:
    float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    snelheid1 = analogRead(sensorPin);         //


    if (voltage > setpoint) {snelheid1 = snelheid1 + (float).05;} // width to diameter to large speed up to stretch it out more
    snelheid1 = constrain(snelheid1, 10 , 10000);  //need a minimum to keep stepper from over heating when idle (probably 10000 is to much, normally spikes under 500 - 1000)
    
    if (voltage < setpoint) {snelheid1 = snelheid1 - (float).05;}// width to diameter to small slow down to stretch it less
    snelheid1 = constrain(snelheid1, 10 , 10000);  //need a minimum to keep stepper from over heating when idle (probably 10000 is to much, normally spikes under 500 - 1000)

    Serial.println(setpoint);
    Serial.println(voltage);
    Serial.println(snelheid1);
  }                          // end loop

(I Have removed all other codes to simplify your understanding of my problem)

What its supposed to do is read a variable voltage 0.00-5.00v from an optical width sensing board (programed to out put an exact voltage based on sensed width. I.E. 1.75mm = 1.75v).

However, it seems to only want to set a speed for about 1.30mm. Meaning that if the filament diameter gets higher than 1.30mm the motor accelerates to thin it out again, ans vise versa.

I need it to be trying to obtain 1.75mm as can bee seen by the setPoint in the code.

I have used the serial print to see what its reading and if i take a piece of 1.75mm filament and place it in the sensor it reads 1.75mm exactly and consistently. So the sensor is not the problem.

Unfortunately the only way to tell if this code is working properly is to have the extruder making fresh filament. However, I have been through several failed cycles of debugging and no mater what i set the setPoint at i get the same average width.

The attached schematic has a photocell only because it matches the voltage signal idea. (this is not a photocell they do not give a variable voltage)

Why are you reading analog input A3 [u]twice[/u], and treating the results differently?

   int sensorPin = A3; // width sensor
...
    int sensorValue = analogRead (A3);             // read the input on analog pin:
    float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    snelheid1 = analogRead(sensorPin);         //

You need to go through this code line by line and make sure that you understand what each line does.

Aside: the factor of 1023.0 is wrong, it should be 1024.0, and the voltage is almost certainly not 5.0 Volts. These are very common mistakes.

??? This part of the code is from another file source, the writer had it controlling a DC servo motor.

I found that the servo was too slow and added the stepper in.

However, it didn't really work in the origional file either.

... If i coment the second analog read i get nothing.

If i coment the second analog read i get nothing.

You will get nowhere with that approach toward programming.

I'll try serial printing all of the veriables an see what i am getting.

I think your right

snelheid1 = analogRead(sensorPin);

Doesn't make since

I haven’t tried this yet, however, after some thought, I pretty much rewrote the whole thing.

Can you tell me if my “else” statement is correct?

#include <Arduino.h>
#include "BasicStepperDriver.h"

// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define MOTOR_STEPS 200

// All the wires needed for full functionality
#define DIR 55
#define STEP 54
//Uncomment line to use enable/disable functionality
#define ENBL 38

// Since ****microstepping is set externally****, make sure this matches the selected mode
// 1=full step, 2=half step etc.
#define MICROSTEPS 1

// 2-wire basic config, microstepping is hardwired on the driver
//BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP);

//Uncomment line to use enable/disable functionality
BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP, ENBL);

 float setpoint = 1.85;  //Desired filament width
 int b = 0.05;          //speed change increment


// motor 1 puller width sensor


int sensorPin = A3; // width sensor



float pullerspeed = 0;


void setup() {

   Serial.begin(250000);// initialize serial communication at 9600 bien

  pinMode(sensorPin, INPUT);
}
void loop() {   
  
    stepper.setRPM(pullerspeed );//* 10);  // needed to multiply the output to get decent speeds
    
    // energize coils - the motor will hold position
    // stepper.enable();

    stepper.setMicrostep(MICROSTEPS);//Tell the driver the microstep level we selected.  If mismatched, the motor will move at a different RPM than chosen.


    stepper.rotate(360);// Moving motor one full revolution using the degree notation


    int sensorValue = analogRead (A3);             // read the input on analog pin:
    float voltage = sensorValue * (5.0 / 1024.0); // Convert the analog reading (which goes from 0 - 1024) to a voltage (0 - 5V):

    if (voltage = 0) {pullerspeed = 10;}
else{
    if (voltage > setpoint) {pullerspeed = pullerspeed + (float).05;} // width to diameter to large speed up to stretch it out more
    pullerspeed = constrain(pullerspeed, 10 , 1000);  //need a minimum to keep stepper from over heating when idle (probobly 10000 is to much, normally spikes under 500 - 1000)
    
    if (voltage < setpoint) {pullerspeed = pullerspeed - (float).05;}// width to diameter to small slow down to stretch it less
    pullerspeed = constrain(pullerspeed, 10 , 1000);  //need a minimum to keep stepper from over heating when idle (probobly 10000 is to much, normally spikes under 500 - 1000)
}
    Serial.println(sensorPin);
    Serial.println(sensorValue);
    Serial.println(b);
    Serial.println(setpoint);
    Serial.println(voltage);
    Serial.println(pullerspeed);
    
    
  }                          // end loop

You don’t need either of the following lines, and the first one has incorrect syntax:

    if (voltage = 0) {pullerspeed = 10;}
else{

The case voltage == 0 (NOTE the double equals!) is taken care of by the constrain statements below the “else”.

“.05” is automatically a float.

Why is “b” printed out but never used?

The following comment

//need a minimum to keep stepper from over heating when idle

is not correct. You need to set the current limit correctly on the motor driver.

This is the code that ended up working, any other problems that stand out?

#include <Arduino.h>
#include "BasicStepperDriver.h"

// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define MOTOR_STEPS 200

// All the wires needed for full functionality
#define DIR 55
#define STEP 54
//Uncomment line to use enable/disable functionality
#define ENBL 38

// Since ****microstepping is set externally****, make sure this matches the selected mode
// 1=full step, 2=half step etc.
#define MICROSTEPS 1

// 2-wire basic config, microstepping is hardwired on the driver
//BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP);

//Uncomment line to use enable/disable functionality
BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP,ENBL);

 float setpoint = 1.75;  //Desired filament width
 float b = 2.00;          //speed change increment  -  remember acctual speed is a muliple of 10 so higher speeds jump quickly- set it so that it 
 float c = 0.05;           //  this is for setting a tolerence in the diameter, alowing for less freequent or unnessasary speed changes

// motor 1 puller width sensor


int sensorPin = A3; // width sensor



float pullerspeed = 0;


void setup() {

   //Serial.begin(250000);// initialize serial communication at 9600 bien

  pinMode(sensorPin, INPUT);
}
void loop() {   
  
    stepper.setRPM(pullerspeed * 10);  // needed to multiply the output to get decent speeds
    
    // energize coils - the motor will hold position
    // stepper.enable();

    stepper.setMicrostep(MICROSTEPS);//Tell the driver the microstep level we selected.  If mismatched, the motor will move at a different RPM than chosen.


    stepper.rotate(360);// Moving motor one full revolution using the degree notation


    int sensorValue = analogRead (A3);             // read the input on analog pin:
    float voltage = sensorValue * (5.0 / 1024.0); // Convert the analog reading (which goes from 0 - 1024) to a voltage (0 - 5V):



    if (voltage > setpoint + c) {pullerspeed = pullerspeed + b;} // width to diameter to large-> speed up to stretch it out more
    pullerspeed = constrain(pullerspeed, 10 , 1500);  //I have a minumum set to keep the puller sipnning a little it helps with loading.
    
    if (voltage < setpoint - c) {pullerspeed = pullerspeed - b;}// width to diameter to small-> slow down to stretch it less
    pullerspeed = constrain(pullerspeed, 10 , 1500);  //I have a minumum set to keep the puller sipnning a little it helps with loading.
    
   // Serial.println(sensorPin);
   // Serial.println(sensorValue);
   // Serial.println(b);
   // Serial.println(setpoint);
   // Serial.println(voltage);
  //  Serial.println(pullerspeed);
    
    
  }                          // end loop

Hey jremington or any one else that knows.

“.05” is automatically a float.

When I don’t float it serial prints a zero.