Panel Cookies
yt_link
insta_link
fb_link
twitter_link

Manual CNC


Manual CNC - CODE Arduino A
Help me by sharing this post



This is code for Arduino A. Download the .zip file below. Open the Arduino (.ino) code on your Arduino IDE and upload it to the Arduino NANO (Arduino A, see schematic). Make sure you have connections in the schematic below. You could download the code from the link below or also copy and paste the code from below.


Downlaod ArduinoA code (last update 07/11/2018)



manual CNC arduino schematic with LCD





/*
Code by: ELECTRONOOBS, 07/11/2018
Tutorial webpage: https://www.electronoobs.com/eng_arduino_tut48.php
Schematic: https://www.electronoobs.com/eng_arduino_tut48_sch1.php
Youtube channel: https://www.youtube.com/c/ELECTRONOOBS
*/

//Code for Arduino A (the Arduino with the motors)

//Inputs and outputs
int en_mot = 12;
int step_x = 2;
int step_y = 3;
int dir_x = 5;
int dir_y = 6;
int x_up = 8;
int x_down = 9;
int y_up = 10;
int y_down = 11;
int speed_in = A0;

////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////EDITABLE VARIABELS//////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
float turns_per_cm = 3.5;             //This is the amount of turns your lead screw needs for oen turn.
float steps_per_revolution = 200;   //This is the amount of steps the motor needs for one full rotation
float max_speed = 17;               //Speed in mm per second
float min_speed = 0.6;              //Speed in mm per second
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////



//Other variables
float mm_per_step = 0.007142;     //Initial value inc ase of 7 turns per cm and 200 steps per full rotatio
float pos_x = 0;
float pos_y = 0;
float DELAY = 0;
float RealSpeed = 0;
unsigned long previousMillis = 0; 
bool step_x_status = false;
bool step_y_status = false;
float max_delay = 0;
float min_delay = 0;



void setup() {  
  //Define the pins as inputs or outputs
  pinMode(en_mot,INPUT);
  pinMode(x_up,INPUT);
  pinMode(x_down,INPUT);
  pinMode(y_up,INPUT);
  pinMode(y_down,INPUT);
  pinMode(speed_in,INPUT);  
  pinMode(step_x,OUTPUT);
  pinMode(step_y,OUTPUT);
  pinMode(dir_x,OUTPUT);
  pinMode(dir_y,OUTPUT);  
  //Calculate the delay for the speed control
  mm_per_step = (10/turns_per_cm)/steps_per_revolution;
  max_delay = min_speed*20000;
  min_delay = max_speed*23;
}

void loop() {
  //Read the analog input of A0 and create the speed delay
  int analog_in = analogRead(speed_in);
  DELAY = map(analog_in,0,1024,max_delay, min_delay);    
  RealSpeed = (1000000/DELAY)*mm_per_step;

  //Move X up
  if(digitalRead(x_up) && !digitalRead(x_down) && !digitalRead(y_up) && !digitalRead(y_down) && !digitalRead(en_mot))
  {
    step_x_status = !step_x_status;
    digitalWrite(dir_x,HIGH);   //CW
    digitalWrite(step_x,step_x_status);
    delayMicroseconds(DELAY); 
    pos_x = pos_x + mm_per_step;  
  }

  //Move X down
   if(!digitalRead(x_up) && digitalRead(x_down) && !digitalRead(y_up) && !digitalRead(y_down) && !digitalRead(en_mot))
  {
    step_x_status = !step_x_status;
    digitalWrite(dir_x,LOW);   //CCW
    digitalWrite(step_x,step_x_status);
    delayMicroseconds(DELAY);    
    pos_x = pos_x - mm_per_step;
    
  }

  //Move Y up
  if(!digitalRead(x_up) && !digitalRead(x_down) && digitalRead(y_up) && !digitalRead(y_down) && !digitalRead(en_mot))
  {
    step_y_status = !step_y_status;
    digitalWrite(dir_y,HIGH);   //CW
    digitalWrite(step_y,step_y_status);
    delayMicroseconds(DELAY);   
    pos_y = pos_y + mm_per_step;
    
  }

  //Move Y down
  if(!digitalRead(x_up) && !digitalRead(x_down) && !digitalRead(y_up) && digitalRead(y_down) && !digitalRead(en_mot))
  {
    step_y_status = !step_y_status;
    digitalWrite(dir_y,LOW);   //CCW
    digitalWrite(step_y,step_y_status);
    delayMicroseconds(DELAY);  
    pos_y = pos_y - mm_per_step;
    
  }

  

  //Move X up and Y up
  if(digitalRead(x_up) && !digitalRead(x_down) && digitalRead(y_up) && !digitalRead(y_down) && !digitalRead(en_mot))
  {
    step_x_status = !step_x_status;
    step_y_status = !step_y_status;
    digitalWrite(dir_x,HIGH);   //CW
    digitalWrite(dir_y,HIGH);   //CW
    digitalWrite(step_x,step_x_status);
    digitalWrite(step_y,step_y_status);
    delayMicroseconds(DELAY);    
    pos_x = pos_x + mm_per_step;    
    pos_y = pos_y + mm_per_step;  
  }

  //Move X up and Y down
  if(digitalRead(x_up) && !digitalRead(x_down) && !digitalRead(y_up) && digitalRead(y_down) && !digitalRead(en_mot))
  {
    step_x_status = !step_x_status;
    step_y_status = !step_y_status;
    digitalWrite(dir_x,HIGH);   //CW
    digitalWrite(dir_y,LOW);   //CCW
    digitalWrite(step_x,step_x_status);
    digitalWrite(step_y,step_x_status);
    delayMicroseconds(DELAY);
    pos_x = pos_x + mm_per_step;    
    pos_y = pos_y - mm_per_step;  
  }

  //Move X down and Y down
  if(!digitalRead(x_up) && digitalRead(x_down) && !digitalRead(y_up) && digitalRead(y_down) && !digitalRead(en_mot))
  {
    step_x_status = !step_x_status;
    step_y_status = !step_y_status;
    digitalWrite(dir_x,LOW);   //CCW
    digitalWrite(dir_y,LOW);   //CCW
    digitalWrite(step_x,step_x_status);
    digitalWrite(step_y,step_y_status);
    delayMicroseconds(DELAY);
    pos_x = pos_x - mm_per_step;    
    pos_y = pos_y - mm_per_step;  
  }

  //Move X down and Y up
   if(!digitalRead(x_up) && digitalRead(x_down) && digitalRead(y_up) && !digitalRead(y_down) && !digitalRead(en_mot))
  {
    step_x_status = !step_x_status;
    step_y_status = !step_y_status;
    digitalWrite(dir_x,LOW);    //CCW
    digitalWrite(dir_y,HIGH);   //CW
    digitalWrite(step_x,step_x_status);
    digitalWrite(step_y,step_y_status);    
    delayMicroseconds(DELAY);
    pos_x = pos_x - mm_per_step;    
    pos_y = pos_y - mm_per_step;  
  }
}//end of void loop








ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel bajo