User Tools

Site Tools


bdev:filamentextruder_recyclebot

This is an old revision of the document!


Filament extruder recyclebot

spool holder I need to design https://www.thingiverse.com/thing:3980786

PCB for potentiometer controller of stepper motor

Sketch for controlling potentiometer

#include <AccelStepper.h>


#include <Wire.h>
#include <SoftwareSerial.h>
#define dirPin 4
#define stepPin 3
#define motorInterfaceType 1

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);



//const float stepsperul = 230; //resolution of 1ml disposable syringes
//const float stepsperul = 131.53; //resolution of 1ml disposable syringes
//const float stepsperul = 272.12; //resolution of 250ul glass syringe


int turnon = 4;
int enablePin = 9;
int estepsPin = 3;
int directionPin = 2;
int direction = 0;
int ton = 0;

int elimitPin = A1; 
//int esteps = 1000;
//int esteprate = 500;
//int estepsincrement = 0;
int estepcount = 0;

int speedpot = A5;

int buttona = A4;
int buttonb = A3;


int buttonastate = 0;
int buttonbstate = 0;


//float stepsperul = 23; //resolution of 1ml disposable syringes
const float stepsperul = 131.43;

int acc = 30;
String command;
long int currpos;
long int pos;

void setup() {
  // put your setup code here, to run once:
  stepper.setMaxSpeed(1000*16);
  Serial.begin(115200);

  pinMode(turnon, OUTPUT);
  digitalWrite(turnon, HIGH); 

  pinMode(enablePin, OUTPUT);
  pinMode(directionPin, OUTPUT);
  pinMode(estepsPin, OUTPUT);


  pinMode(speedpot, INPUT);
  pinMode(buttona, INPUT);
  pinMode(buttonb, INPUT);


  digitalWrite(enablePin,LOW);
  currpos = 0;
  stepper.setAcceleration(acc);
}

void loop() {
  // put your main code here, to run repeatedly:

 buttonastate = analogRead(buttona);
 buttonbstate = analogRead(buttonb);
 /*
 Serial.print("buttona: ");
 Serial.println(analogRead(buttona));
 */ 
 if (buttonbstate < 1020){
  Serial.println("buttonbstate is low");
 } else { Serial.println("buttonbstate is high"); }

 if (buttonastate < 1020){
  Serial.println("buttonastate is low");
 } else { Serial.println("buttonastate is high"); }


 Serial.println(buttonbstate);
 Serial.print("speedpot: ");
 Serial.println(analogRead(speedpot));
 delay(100);
 /*
 if (digitalRead(buttona) == HIGH){
  Serial.println("power set");
  if (ton==0){ton = 1;}else{ton=0;}
 }
 if (digitalRead(buttonb) == HIGH){
  Serial.println("direction set");
  if (direction==0){direction = 1;}else{direction=0;}
 }
 */
 if(Serial.available())
 {
    char c = Serial.read();
    if (c== '\n')
    {
      currpos = parseCommand(command, currpos);
      command = "";
    }
    else 
    {
      command +=c;
    }
 }
 delay(30);
}

long int parseCommand(String com, long int currpos)
{
  //Serial.print("Your command: ");
  //Serial.println(com);

  //"G1E10F200";
  String part1 = com.substring(0,com.indexOf("e"));
  if (part1.equalsIgnoreCase("g1")){
    String part2 = com.substring(com.indexOf("e")+1,com.indexOf("s"));
    String part3 = com.substring(com.indexOf("s")+1,com.indexOf("a"));
    String part4 = com.substring(com.indexOf("a")+1);
    int vpos = part2.toInt();
    pos = vpos * stepsperul; 
    int vfeed = part3.toInt();
    int feed = vfeed * stepsperul;
    //int acc = part4.toInt();
    //int feed = vfeed;
    int acc = feed/3;
    if ((pos-currpos) < 0) {
      feed = feed * -1;
    }
    moveacc((pos-currpos), feed, acc);
    currpos = pos;
  }
  else if(com.equalsIgnoreCase("readpot")){
   readpotentionmetersanity(speedpot);
  }
  else if(com.equalsIgnoreCase("m114")){
   Serial.print("Step Position: ");
   Serial.print(pos);
   Serial.print(" volume Position: ");
   Serial.println((pos/stepsperul),3);
  } 
  else if(com.equalsIgnoreCase("g28e0")){
   currpos = homing(estepsPin,directionPin,elimitPin);
   currpos = 0;
   pos = 0;
  } 
  else if(com.equalsIgnoreCase("readinput")){
   Serial.print("Input ");
   Serial.println(digitalRead(elimitPin));
  } 
  else if(com.equalsIgnoreCase("d")){
   if (direction == 0){
    direction = 1;
   }
   Serial.print("direction is ");
   Serial.println(direction);
  } 
  else if(com.equalsIgnoreCase("info")){
    Serial.println("multistepper");
  }
  else {
   Serial.print("Did not recognize ");
   Serial.println(com);
  }
   return currpos;
}


void moveacc(long int inc, int spd, int acc){
 //speed is in steps per second
 stepper.setCurrentPosition(0);
 stepper.setAcceleration(acc);
 while(stepper.currentPosition() != inc){
    stepper.setSpeed(spd);
    stepper.runSpeed();
 }
}

int readpotentionmetersanity(int speedPin){
 Serial.print("speed pin: ");
 Serial.println(analogRead(speedPin));
 int val = analogRead(A2);
 int stepdelay = map(val, 0, 1023, 1, 1000); 
 Serial.print("stepdelay: ");
 Serial.println(stepdelay); 
}


int movespeedadjust(int stepsPin, int direction, int directionPin, int limitPin, int speedPin){
  if (direction == 0){
   digitalWrite(directionPin,LOW); // Set Dir high
  } else {
   digitalWrite(directionPin,HIGH); // Set Dir high
  }
 stepper.setCurrentPosition(1);
 int val = analogRead(A2);
 int stepdelay = map(val, 0, 1023, 1, 1000); 
 while(stepper.currentPosition() > 0){
    int spd = analogRead(A2) * stepsperul;
    int acc = spd/3;
    stepper.setAcceleration(acc);
    stepper.setSpeed(spd);
    stepper.runSpeed();
    int checker = digitalRead(limitPin);
     if (checker == HIGH){
      break;
     } 
 }
}


int homing(int stepsPin, int directionPin, int limitPin){
  digitalWrite(directionPin,LOW); // Set Dir high
  int checker = 1;
  int cnter = 0;
  int stpper = 1;
   while(stpper > 0){
    checker = digitalRead(limitPin);
    if (checker == HIGH){
      cnter = cnter + 1;
    }
    else {
      cnter = 0;
    }
    if (cnter > 4){
      stpper = 0;
    }
    digitalWrite(stepsPin,HIGH); // Output high
    delayMicroseconds(200); // Wait 1/2 a ms
    digitalWrite(stepsPin,LOW); // Output low
    delayMicroseconds(200); // Wait 1/2 a ms
   }
   digitalWrite(directionPin,HIGH); // Set Dir high
   for(int x = 0; x < 18; x++){ // Loop 200 times
    digitalWrite(stepsPin,HIGH); // Output high
    delayMicroseconds(1000); // Wait 1/2 a ms
    digitalWrite(stepsPin,LOW); // Output low
    delayMicroseconds(1000); // Wait 1/2 a ms
   }
    int stepcount = 0;
    return stepcount;
}

Sketch for controlling temperature

#include <Servo.h> 
#include <PID_v1.h> 
//#include <FastPID.h> 

Servo myservo;  // create servo object to control a servo #include <Wire.h> #include <SoftwareSerial.h>

#define THERMISTOR_PINA A1
#define THERMISTOR_PINB A2
#define THERMISTOR_PINC A3

#define TEMPCONTROLA 3
#define TEMPCONTROLB 5
#define TEMPCONTROLC 6


//Define the aggressive and conservative Tuning Parameters

double aggKpA=150, aggKiA=0.5, aggKdA=0;
double aggKpB=150, aggKiB=0.5, aggKdB=0;
double aggKpC=150, aggKiC=0.5, aggKdC=0;


int thermoelectricval = 255;
String command;
float currpos;
int celsius;
int levelstreamon;
double settemp = 0;
double Setpoint, Input, Output, OutputA, OutputB, OutputC;
int tme = 250;

//Specify the links and initial tuning parameters
//PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
PID myPIDA(&Input, &OutputA, &Setpoint, aggKpA, aggKiA, aggKdA, DIRECT);
PID myPIDB(&Input, &OutputB, &Setpoint, aggKpB, aggKiB, aggKdB, DIRECT);
PID myPIDC(&Input, &OutputC, &Setpoint, aggKpC, aggKiC, aggKdC, DIRECT);

#define NUMTEMPS 20
short temptable[NUMTEMPS][2] = {
   {1, 841}, {54, 255},
   {107, 209},
   {160, 184},
   {213, 166},
   {266, 153},
   {319, 142},
   {372, 132},
   {425, 124},
   {478, 116},
   {531, 108},
   {584, 101},
   {637, 93},
   {690, 86},
   {743, 78},
   {796, 70},
   {849, 61},
   {902, 50},
   {955, 34},
   {1008, 3}
};


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(TEMPCONTROLA, OUTPUT);
  pinMode(TEMPCONTROLB, OUTPUT);
  pinMode(TEMPCONTROLC, OUTPUT);
  analogWrite(TEMPCONTROLA, 255); 
  analogWrite(TEMPCONTROLB, 255); 
  analogWrite(TEMPCONTROLC, 255); 

  Setpoint = settemp;
  //turn the PID on
  myPIDA.SetMode(AUTOMATIC);
  myPIDB.SetMode(AUTOMATIC);
  myPIDC.SetMode(AUTOMATIC);
}

void loop() {
   Input = read_temp("A");
   double gap = abs(Setpoint-Input); //distance away from setpoint
   myPIDA.SetTunings(aggKpA, aggKiA, aggKdA);
   myPIDA.Compute();
   analogWrite(TEMPCONTROLA,OutputA);

   Input = read_temp("B");
   gap = abs(Setpoint-Input); //distance away from setpoint
   myPIDB.SetTunings(aggKpB, aggKiB, aggKdB);
   myPIDB.Compute();
   analogWrite(TEMPCONTROLB,OutputB);

   Input = read_temp("C");
   gap = abs(Setpoint-Input); //distance away from setpoint
   myPIDC.SetTunings(aggKpC, aggKiC, aggKdC);
   myPIDC.Compute();
   analogWrite(TEMPCONTROLC,OutputC);

   
   int rawvalue = analogRead(THERMISTOR_PINA);
   int rawvalueA = rawvalue;
   float celsiusA = read_temp("A");
   rawvalue = analogRead(THERMISTOR_PINB);
   float celsiusB = read_temp("B");
   rawvalue = analogRead(THERMISTOR_PINC);
   float celsiusC = read_temp("C");
   //int fahrenheit = (((celsius * 9) / 5) + 32);
  if (levelstreamon == 1) {
   Serial.print("A: ");
   Serial.print(rawvalueA);
   Serial.print(" ");
   Serial.println(celsiusA);
   Serial.print("B: ");
   Serial.println(celsiusB);
   Serial.print("C: ");
   Serial.println(celsiusC);
   Serial.print("OutputA: ");
   Serial.println(OutputA);
   Serial.print("Settemp: ");
   Serial.println(settemp);   
   delay(500);
  }

 if(Serial.available())
 {
    char c = Serial.read();
    if (c== '\n')
    {
      parseCommand(command);
      command = "";
    }
    else 
    {
      command +=c;
    }
 }
 delay(1);
}

float parseCommand(String com)
{

  if(com.equalsIgnoreCase("info")){
    Serial.println("tempcontrol");
  }
  else if(com.equalsIgnoreCase("levelstreamon")){
   levelstreamon = 1;
  }  
  else if(com.equalsIgnoreCase("defaultpid")){
    double aggKpA=19.56, aggKiA=0.71, aggKdA=134.26;
    double aggKpB=19.56, aggKiB=0.71, aggKdB=134.26;
    double aggKpC=19.56, aggKiC=0.71, aggKdC=134.26;
  }
  else if (com.substring(0,7) == "settemp") {
    settemp = com.substring(8).toDouble();
    Setpoint = settemp;
    Serial.print("settemp: ");
    Serial.println(settemp);
  }
  else if (com.substring(0,4) == "setpA") {
    aggKpA = com.substring(5).toDouble();
  }
  else if (com.substring(0,4) == "setiA") {
    aggKiA = com.substring(5).toDouble();
  }
  else if (com.substring(0,4) == "setdA") {
    aggKdA = com.substring(5).toDouble();
  }

  else if (com.substring(0,4) == "setpB") {
    aggKpB = com.substring(5).toDouble();
  }
  else if (com.substring(0,4) == "setiB") {
    aggKiB = com.substring(5).toDouble();
  }
  else if (com.substring(0,4) == "setdB") {
    aggKdB = com.substring(5).toDouble();
  }

  else if (com.substring(0,4) == "setpC") {
    aggKpC = com.substring(5).toDouble();
  }
  else if (com.substring(0,4) == "setiB") {
    aggKiC = com.substring(5).toDouble();
  }
  else if (com.substring(0,4) == "setdC") {
    aggKdC = com.substring(5).toDouble();
  }
  else if(com.equalsIgnoreCase("levelstreamoff")){
   levelstreamon = 0;
  } 
  else if(com.equalsIgnoreCase("readlevelA")){
     int rawvalue = analogRead(THERMISTOR_PINA);
     float celsius = read_temp("A");
     Serial.println(celsius);
  }
  else if(com.equalsIgnoreCase("readlevelB")){
     int rawvalue = analogRead(THERMISTOR_PINB);
     float celsius = read_temp("B");
     Serial.println(celsius);
  }
  else if(com.equalsIgnoreCase("readlevelC")){
     int rawvalue = analogRead(THERMISTOR_PINC);
     float celsius = read_temp("C");
     Serial.println(celsius);
  }
  else if(com.equalsIgnoreCase("turnonA")){
     analogWrite(TEMPCONTROLA, 255);
     Serial.println("pin A is on");
  }
  else if(com.equalsIgnoreCase("turnoffA")){
     analogWrite(TEMPCONTROLA, 0);
     Serial.println("pin A is off");
  }
  else if(com.equalsIgnoreCase("turnonB")){
     analogWrite(TEMPCONTROLB, 255);
     Serial.println("pin B is on");
  }
  else if(com.equalsIgnoreCase("turnoffB")){
     analogWrite(TEMPCONTROLB, 0);
     Serial.println("pin B is off");
  }
  else if(com.equalsIgnoreCase("turnonC")){
     analogWrite(TEMPCONTROLC, 255);
     Serial.println("pin C is on");
  }
  else if(com.equalsIgnoreCase("turnoffC")){
     analogWrite(TEMPCONTROLC, 0);
     Serial.println("pin C is off");
  }


  else if(com.equalsIgnoreCase("readlevel")){
     int rawvalue = analogRead(THERMISTOR_PINA);
     float celsius = read_temp("A");
     Serial.print("A: ");
     Serial.println(celsius);
     rawvalue = analogRead(THERMISTOR_PINB);
     celsius = read_temp("B");
     Serial.print("B: ");
     Serial.println(celsius);
     rawvalue = analogRead(THERMISTOR_PINC);
     celsius = read_temp("C");
     Serial.print("C: ");
     Serial.println(celsius);
  }
}

float read_temp(String sensor_type)
{
   int THERMISTOR_PIN = 0;
   if (sensor_type == "A"){
    THERMISTOR_PIN = THERMISTOR_PINA;
   }
   if (sensor_type == "B"){
    THERMISTOR_PIN = THERMISTOR_PINB;
   }
   if (sensor_type == "C"){
    THERMISTOR_PIN = THERMISTOR_PINC;
   }

   int rawtemp = analogRead(THERMISTOR_PIN);
   float current_celsius = 0;

   byte i;
   for (i=1; i<NUMTEMPS; i++)
   {
      if (temptable[i][0] > rawtemp)
      {
         float realtemp  = temptable[i-1][1] + (rawtemp - temptable[i-1][0]) * (temptable[i][1] - temptable[i-1][1]) / (temptable[i][0] - temptable[i-1][0]);

         if (realtemp > 255)
           realtemp = 255; 
           current_celsius = realtemp;
           break;
      }
   }

   // Overflow: We just clamp to 0 degrees celsius
   if (i == NUMTEMPS)
   current_celsius = 0;

   return current_celsius;
}
bdev/filamentextruder_recyclebot.1666816900.txt.gz · Last modified: 2022/10/26 20:41 by richard