ArduinoMega CNCshieldV3 — Arduino Mega simulation

An interactive Arduino Mega circuit simulation you can run free in your browser on Velxio, by leemcf.

Components

Sketch code

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <AccelStepperWithDistance.h>

const byte rows = 4;
const byte cols = 4;

char keyNumber[rows][cols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};

byte rowsPin[rows] = {39,41,43,45};
byte colsPin[cols] = {47,49,51,53};

Keypad keypad = Keypad(makeKeymap(keyNumber), rowsPin, colsPin, rows, cols);
LiquidCrystal_I2C lcd(0x27,20,4);

AccelStepperWithDistance rotstep(AccelStepperWithDistance::DRIVER, 2, 5);
AccelStepperWithDistance gantrystep(AccelStepperWithDistance::DRIVER, 3, 6);
AccelStepperWithDistance vertstep(AccelStepperWithDistance::DRIVER, 4, 7);
AccelStepperWithDistance probestep(AccelStepperWithDistance::DRIVER, 12, 13);


void setup(){
  pinMode(9, INPUT_PULLUP);//ENDSTOPS GANTRY internal pullup resistor (debouncing)
  pinMode(10, INPUT_PULLUP);//ENDSTOPS VERT internal pullup resistor (debouncing)
  pinMode(11, INPUT_PULLUP);//ENDSTOPS PROBE internal pullup resistor (debouncing)
  pinMode(0, INPUT_PULLUP);//ESTOP internal pullup resistor (debouncing)
  pinMode(1, INPUT_PULLUP);//ABORT internal pullup resistor (debouncing)
  pinMode(16, INPUT_PULLUP);//SWITCH MODES internal pullup resistor (debouncing)

  attachInterrupt(digitalPinToInterrupt(1), aBort, CHANGE);
  attachInterrupt(digitalPinToInterrupt(0), Estop, CHANGE);

  lcd.init();
  lcd.backlight();
  lcd.clear();

  stepSetup();//setup 4-axis of stepper motors

  caliBration();//calibrate gantry,vert, & probe
    
  float barDia=0;
  float barLth=0;
  float probeDown=0;
  float barCirc=0;
  float probeFromCL=0;
  barSize(barDia, barLth, probeDown, barCirc, probeFromCL);//get bar-stock size from keypad and lcd

  bool scanorGo = digitalRead(16);
  if(scanorGo==HIGH){
    setProbe();//set probe spacing and height
    barScan();//scan barstock
  }
  else{
    goTo;
  }
  
}


void loop() {}


float getFloatInput() {
  float intPart = 0;
  float fracPart = 0;
  int divisor = 1;
  boolean isDec = false;
  
  while (true) {
    char key = keypad.getKey();
    if (key != NO_KEY) {
      if (key >= '0' && key <= '9') {
        if (!isDec) {
          intPart = (intPart * 10) + (key - '0');
        } else {
          fracPart = (fracPart * 10) + (key - '0');
          divisor *= 10;
        }
        Serial.print(key);
      }
      else if (key == '*') { // Press '*' for decimal point
        isDec = true;
        Serial.print('.');
      }
      else if (key == '#') { // Press '#' to finish input
        Serial.println();
        break;
      }
    }
  }
  return intPart + (float)fracPart / divisor;
}


void stepSetup(){
  rotstep.setMaxSpeed(200); //SPEED = Steps / second
  rotstep.setAcceleration(100); //ACCELERATION = Steps /(second)^2
  rotstep.setStepsPerRotation(200);  // For a 1.8° stepper motor
  rotstep.setDistancePerRotation(1.5); // inches per rotation

  gantrystep.setMaxSpeed(200); //SPEED = Steps / second
  gantrystep.setAcceleration(100); //ACCELE

More projects by leemcf