Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Arduino Uno R3 Step...
 
Share:
Notifications
Clear all

Arduino Uno R3 Stepper Servo Lcd

1 Posts
1 Users
0 Likes
455 Views
 Anonymous
Joined: 53 years ago
Posts: 0
Topic starter  
Hello some time ago I made a wire cutting machine with an Arduino UNO R3 a display and a step by step

I am not a programmer but I found some codes on the net, after some modification to the code the machine works well however I need to add a second stepper with a variable speed different from the first stepper, using a potentiometer on the Analog A5.

The I12 output is available for the pul + of the new Stepper.

I have copied the Codes below in order to have your comment.

Thank you

Mike

-----------------------------------------


#include <Servo.h>
#include <Stepper.h>
#include <LiquidCrystal.h>


//////////////////////////////////////
// LCD buttons
//////////////////////////////////////
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int pinSwitch = 20;
int pinServo = 13;

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
const int stepsPerFoot = 485; // steps per stepsPerFoot of cable

int calSteps;           // steps counted during calibration
unsigned long startTime;
unsigned long endTime;
unsigned long calTime;
int switchLastState;    // switch change state

int stepCount = 0; 
int cutPos = 0;         // servo cutter closed position
int openPos = 140;        // servo cutter open position
int servoDelay = 500;   // servo movement time
int baseDelay = 6100;   // time to measure one stepsPerFoot of cable
int speed1 =1500;        // stepper speed in RPMs
int cutDelay;           // how long it takes to measure entire length
int setLen;             // requested length
int setQty;             // requested quantity
int currQty = 0;        // current quantity
int totalSteps;         // how many steps we need to measure the requested length
int measureLen = 0;     // measured length (float for decimals!)
int measureInch;
int measureFeet;
int cutLen;             // Measured length during cutting operation


unsigned long lastDelayTime = 0;
unsigned long delayTime = 100;
unsigned long lastInteraction = 0;
unsigned long lastUpdate = 0;
//int buttonState = 0;

int cursorPos = 0;
int menuState = 0;        // Menu position, starts at Menu 0

Stepper myStepper(stepsPerRevolution,03,11);
Servo myservo ;

// Set up lcd global variable
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

/////////////////////////////////////////////
// New way to read buttons
////////////////////////////////////////////
int read_LCD_buttons()
{
  adc_key_in = analogRead(0); // read the value from the buttons
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50) return btnRIGHT;
  if (adc_key_in < 250) return btnUP;
  if (adc_key_in < 450) return btnDOWN;
  if (adc_key_in < 650) return btnLEFT;
  if (adc_key_in < 850) return btnSELECT;

    return btnNONE; // when all others fail, return this...
}

void setup() {
  Serial.begin(9600);
  Serial.println("======== Automated Cable Cutter Online =========");
  
  pinMode(10, OUTPUT);    // LCD backlight control
  digitalWrite(10,  255);
  pinMode(pinSwitch, INPUT_PULLUP);

  lcd.begin(16, 2);     // Begin 16 x 2 LCD
  myservo.attach(pinServo);
  myStepper.setSpeed(speed1);

  myservo.write(openPos); // make sure we start with cutter open

  showMenus();
}

void loop(){
  lcd_key = read_LCD_buttons(); // read the buttons
  processButtons();             // do the thing
  if (digitalRead(pinSwitch) != switchLastState) {
    showMenus();
    switchLastState = digitalRead(pinSwitch);   // checks to see if the wire sensor switch has changed
   } 

 }

void processButtons () { // Reads the buttons and exectues the proper actions based on the menu currently open

  if ((millis() - lastDelayTime) > delayTime) {                                 // Only read buttons every 0.3 second

    switch (menuState) {
      case 0:
        ///////////////////////////////////////////////////
        // Menu 0: Cut mode
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT: // Select
            if (digitalRead(pinSwitch) == HIGH) {  // if we haven't loaded a cable
              releaseStepper();
            }
            menuState = 1;
            cursorPos = 1;
            showMenus();
            break;
          case btnRIGHT: // Right
            if (digitalRead(pinSwitch) == HIGH) {  // if we haven't loaded a cable
              releaseStepper();
            }
            menuState = 1;
            cursorPos = 1;
            showMenus();
            break;
          case btnUP: //Up
            menuState = 7;
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 2;
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;
            showMenus();
            break;
        }
        break;
      case 1:
        ///////////////////////////////////////////////////
        // Menu 0 Sub 1: Set cuts
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:   // Select
            if (setQty > 0) { // only if we've set Qty and if there's a cable loaded
              cutCables();
            }
            break;
          case btnRIGHT: // Right
            if (cursorPos == 1) {
              cursorPos = 2;
            } else if (cursorPos == 2) {
              cursorPos = 1;
            }
            showMenus();
            break;
          case btnUP: //Up
            if (cursorPos == 1) {
              setLen ++;
            } else if (cursorPos == 2) {
              setQty ++;
            }
            showMenus();
            break;
          case btnDOWN: // Down
            if (cursorPos == 1 && setLen > 0) {
              setLen --;
            } else if (cursorPos == 2 && setQty > 0) {
              setQty --;
            }
            showMenus();
            break;
          case btnLEFT: // Left
            menuState = 0;
            showMenus();
            break;
        }
        break;
      case 2:
        ///////////////////////////////////////////////////
        // Menu 1: Measure Mode
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 5;    // measure menu
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 5;    // measure menu
            showMenus();
            break;
          case btnUP: //Up
            menuState = 0;    // Go back to Top menu
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 7;    // Go to calibrate menu
            showMenus();
            break;
          case btnLEFT: // ESC

            break;
        }
        break;
      case 4:
        ///////////////////////////////////////////////////
        // Cut summary
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 1;    // Go back to Cut menu
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 1;    // Go back to Cut menu
            showMenus();
            break;
          case btnUP: //Up
            menuState = 1;    // Go back to Cut menu
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 1;    // Go back to Cut menu
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;    // Go back to Top menu
            showMenus();
            break;
        }
        break;

      case 5:
        ///////////////////////////////////////////////////
        // Measurement mode
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            measureCable();
            break;
          case btnRIGHT: // Right
            // do nothing
            break;
          case btnUP: //Up
            // do nothing
            break;
          case btnDOWN: // Down
            // do nothing
            break;
          case btnLEFT: // ESC
            menuState = 2;    // Go back to Measure top menu
            showMenus();
            break;
        }
        break;
      case 6:
        ///////////////////////////////////////////////////
        // Cut summary
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
          case btnUP: //Up
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
        }
        break;
      case 7:
        ///////////////////////////////////////////////////
        // Calibrate
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 8;    // calibrate menu
            showMenus();
            releaseStepper();
            break;
          case btnRIGHT: // Right
            menuState = 0;    //
            showMenus();
            break;
          case btnUP: //Up
            menuState = 2;    //
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 0;    //
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;    //
            showMenus();
            break;
        }
        break;
      case 8:
        ///////////////////////////////////////////////////
        // Calibrate
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            //menuState = 8;    //
            calibrate();
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 0;    //
            showMenus();
            break;
          case btnUP: //Up
            menuState = 2;    //
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 0;    //
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;    //
            showMenus();
            break;
        }
        break;

      default:
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 0;    //
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 0;    //
            showMenus();
            break;
          case btnUP: //Up
            menuState = 0;    //
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 0;    //
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;    //
            showMenus();
            break;
        }
        break;
    }
    lastDelayTime = millis();
  }
}



void showMenus() {
  // This function holds all the menu designs. Each case is a different menu/submenu. Cases are set by the Enter button incrementing the menuState variable.
  switch (menuState) {
    case 0:
      ///////////////////////////////////////////////////
      // Menu 0: Cut Mode
      ///////////////////////////////////////////////////

      lcd.noCursor(); // Don't show the cursor
      lcd.setCursor(0, 0);
      lcd.write(" COUPE BROCHES    ");
      lcd.setCursor(0, 1);
      lcd.write(" APPUYER SELECT  ");
      break;

    case 1:
      ///////////////////////////////////////////////////
      // Menu 0, Submenu 1: Cut Setup
      ///////////////////////////////////////////////////
      lcd.clear();
      lcd.cursor(); // Show the cursor
      lcd.setCursor(0, 0);
      lcd.write("LONG.(POUCES) ");
      lcd.setCursor(13, 0);
      lcd.print(setLen);
      lcd.setCursor(0, 1);
      lcd.write("QTY: ");
      lcd.print(setQty);
      if (digitalRead(pinSwitch) == HIGH) { // if we've not loaded a cable
        lcd.setCursor(10, 1);
        lcd.print("CHARGE");
      } else {
        lcd.setCursor(10, 1);
        lcd.print("PRET");
      }

      if (cursorPos == 1) {
        lcd.setCursor(13, 0);
      } else if (cursorPos == 2) {
        lcd.setCursor(5, 1);
      }
      break;

    case 2:
      ///////////////////////////////////////////////////
      // Menu 1: Measure Mode
      ///////////////////////////////////////////////////
      lcd.noCursor(); // Don't show the cursor
      lcd.setCursor(0, 0);
      lcd.write("  Measure Mode  ");
      lcd.setCursor(0, 1);
      lcd.write("  Press Select  ");

      break;

    case 3:
      //////////////////////////////////////////////////
      // Cutting in Progress
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 1);
      lcd.print("QTY.:");
      lcd.setCursor(8, 1);
      lcd.print(currQty);
      break;

    case 4:
      //////////////////////////////////////////////////
      // Cut summary
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print(" JOB FINI ");
      lcd.setCursor(0, 1);
      lcd.print("QTY. ");
      lcd.setCursor(4, 1);
      lcd.print(currQty);
      lcd.print(" @ ");
      lcd.print(setLen);
      lcd.print(" POUCES ");
      break;
    case 5:
      //////////////////////////////////////////////////
      // Measure mode
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      if (digitalRead(pinSwitch) == HIGH) {
        lcd.print(" CHARGER FILS ");
      } else {
        lcd.print("      PRET     ");
      }

      lcd.setCursor(0, 1);
      lcd.print("  APPUYER SELECT  ");
      break;
    case 6:
      //////////////////////////////////////////////////
      // Measure summary
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print("MESURE COMPLETER");
      lcd.setCursor(0, 1);
      lcd.print("Len: ");
      lcd.print(measureFeet);
      lcd.print("' ");
      lcd.print(measureInch);
      lcd.print("\"");
      break;

    case 7:
      //////////////////////////////////////////////////
      // Calibrate
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print("MODE CALIBRATION");
      lcd.setCursor(0, 1);
      lcd.print(" APPUYER SELECT  ");
      break;

    case 8:
      //////////////////////////////////////////////////
      // Calibrate
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print("CHARGER EXACT 1'");
      lcd.setCursor(0, 1);
      lcd.print("DE FILS.");
      break;

    case 9:
      //////////////////////////////////////////////////
      // Calibrate
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print("CAL. COMPLETER!");
      lcd.setCursor(0, 1);
      lcd.print(calSteps);
      lcd.print(" STEPS/POUCE");
      break;
  }
}

void calibrate() {
  calTime = 0;
  calSteps = 0;           // reset
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("CALIBRATION...");


  while (digitalRead(pinSwitch) == LOW) {
    myStepper.step(1);
    calSteps ++;
    lcd.setCursor(0, 1);
    lcd.print(calSteps);
  }

  menuState = 9;        // Calibrate summary menu
  showMenus();
  releaseStepper();
}

void cutCables() {
  cutLen = 0;                     // reset measured length
  currQty = 0;                    // reset current quantity

  if (digitalRead(pinSwitch) == HIGH) {    // switch reads HIGH if we haven't loaded a wire
    lcd.setCursor(0, 0);
    lcd.print("CHARGER FILS!");
  } else {
    Serial.println("-_-_-_ Cutting subroutine activated _-_-_-");
    Serial.print("~ Cutting ");
    Serial.print(setQty);
    Serial.print(" pieces with length ");
    Serial.print(setLen);
    Serial.println(" feet.");
    Serial.print("~ Current quantity cut: ");
    Serial.println(currQty);

    //totalSteps = (setLen * stepsPerFoot);     // total steps per cut
    menuState = 3;                  // Cutting in progress menu
    showMenus();

    while (currQty < setQty) {
      myStepper.step(-20);
      delay(100);
      myStepper.step(20);             // unstick the wire from the cutter
      cutLen = 0;                     // reset measured length
      Serial.println("~~~ MESURAGES... ~~~");
      lcd.setCursor(0, 0);
      lcd.print("MESURAGES...    ");
      while (cutLen < setLen) {
        myStepper.step(stepsPerFoot);
        cutLen ++;
        lcd.setCursor(13, 0);
        lcd.print(cutLen);
      }
      Serial.println("~~~ CUTTER OUVERT ~~~");
      lcd.setCursor(0, 0);
      lcd.print(" CUTTER FERMER ");
      myservo.write(cutPos);
      delay(servoDelay);
      Serial.println("~~~ CUTTER FERMER ~~~");
      lcd.setCursor(0, 0);
      lcd.print(" CUTTER OUVERT ");
      myservo.write(openPos);
      delay(servoDelay);
      Serial.println("~~~ COUPE FINI ~~~");
      currQty ++;
      showMenus();
    }

    menuState = 4;     // Cut summary
    showMenus();
  }
}

void measureCable() {
  int stepsPerInch = (stepsPerFoot / 12);
  measureLen = 0;

  lcd.clear();
  lcd.noCursor();
  lcd.setCursor(0, 0);
  lcd.print("  MESURAGES...  ");
  lcd.setCursor(0, 1);
  lcd.print("Len: ");
  lcd.print(measureLen);
  lcd.print(" in.");
  while (digitalRead(pinSwitch) == LOW) {
    myStepper.step(stepsPerInch);
    measureLen = measureLen + 1;
    lcd.setCursor(4, 1);
    lcd.print(measureLen);
  }

  //some math

  measureInch = measureLen % 12;
  measureFeet = measureLen / 12;

  menuState = 6;  // Summary menu
  showMenus();
  
}
void releaseStepper() {
  
  digitalWrite(03, LOW);
  digitalWrite(11, LOW); 
}

ReplyQuote
Share: