element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Twist, Turn and Move Design Challenge with TE Robotics
  • Challenges & Projects
  • Design Challenges
  • Twist, Turn and Move Design Challenge with TE Robotics
  • More
  • Cancel
Twist, Turn and Move Design Challenge with TE Robotics
Blog Twist, Turn and Move Design Challenge - Unlimited CNC Robot Blog #5
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Leaderboard
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: dwinhold
  • Date Created: 8 Sep 2022 11:00 AM Date Created
  • Views 476 views
  • Likes 8 likes
  • Comments 2 comments
  • Line Following
  • te connectivity
  • dwinhold
  • Twist Turn and Move Robotics Design Challenge
  • Unlimited CNC
Related
Recommended

Twist, Turn and Move Design Challenge - Unlimited CNC Robot Blog #5

dwinhold
dwinhold
8 Sep 2022

Unlimited CNC Robot Blog #5 - Sponsored by TE robotics

This update is the code I wrote for the CNC Robot. The code includes all my recent updates, like IR Remote start and emergency stop. The programming of the IR Remote was new to me and was a bit challenging. It like to challenge myself with learning new things, it's rewarding when you get it working!!

Here is the code:

/**Unlimited CNC Robot**/

#include <IRremote.h>  //including infrared remote header file - This is used to start the robot and emergency stop

int RECV_PIN = 7; // the pin where you connect the output pin of IR sensor     
IRrecv irrecv(RECV_PIN);     
decode_results results; 

/**set control port - DFRobot L298P**/
const int E1Pin = 10;
const int M1Pin = 12;
const int E2Pin = 11;
const int M2Pin = 13;

/**inner definition**/
typedef struct {
  byte enPin;
  byte directionPin;
} MotorContrl;
  
const int M1 = 0;
const int M2 = 1;
const int MotorNum = 2;

const MotorContrl MotorPin[] = { {E1Pin, M1Pin}, {E2Pin, M2Pin} } ;

const int Forward = LOW;
const int Backward = HIGH;

//Sensor Connection
const int left_sensor_pin =A0;
const int right_sensor_pin =A1;
const int laser_on_sensor_pin =A2; /**Right**/
const int laser_off_sensor_pin =A3; /**Left**/

int left_sensor_state;
int right_sensor_state;
int laser_on_sensor_state;
int laser_off_sensor_state;
int turn_delay = 10;
int laser_status = 0; /**0 = Laser status is off, 1 = Laser status is On**/
int ircontrol = 0;
  

/**program**/
void setup() {
  
  Serial.begin(9600);     
  irrecv.enableIRIn();
  ircontrol = 0;
  initMotor();

  pinMode(9, OUTPUT); /**Power to Laser & Fan**/
  
}

void loop() {
  
  Serial.println("Waiting for IR Signal to start");

  if(irrecv.decode()){  // IR receive results
  ircontrol = 1;
  delay(1500);
  }
  if(ircontrol != 0){
  do{
    
  Serial.println("Robot in Running Mode");
  int value;
  
  irrecv.resume();
  left_sensor_state = analogRead(left_sensor_pin);
  right_sensor_state = analogRead(right_sensor_pin);
  laser_on_sensor_state = analogRead(left_sensor_pin);
  laser_off_sensor_state = analogRead(right_sensor_pin); 


  if(laser_status = 0)
  {
    if(laser_on_sensor_state  < 500)
    {
    Serial.println("Laser On");
    setMotorSpeed( M1, 0 );
    setMotorSpeed( M2, 0 );
    digitalWrite(9, HIGH); //Laser & Fan on

    delay(250);

    laser_status = 1; /**Laser status is now on**/

     }
  }


  if(laser_status = 1)
  {
    if(laser_off_sensor_state  < 500)
    {
    Serial.println("Laser Off/Fan Off");
    setMotorSpeed( M1, 0 );
    setMotorSpeed( M2, 0 );
    
    digitalWrite(9, LOW); //Laser & Fan off
    delay(200);

    laser_status = 0; //Laser status is now off
  
	}
  }
  
  
  if(right_sensor_state > 500 && left_sensor_state < 500)
  {
  Serial.println("turning right");

  setMotorDirection( M2, Forward );
  setMotorSpeed( M2, 100 );
  setMotorSpeed( M1, 0 );
  delay(turn_delay);
  
  }

  
if(right_sensor_state < 500 && left_sensor_state > 500)
{
  Serial.println("turning left");
  
  setMotorDirection( M1, Forward );
  setMotorSpeed( M1, 100 );
  setMotorSpeed( M2, 0 );
  delay(turn_delay);
  }


if(right_sensor_state < 500 && left_sensor_state < 500)
{
  Serial.println("going forward");

  setMotorDirection( M1, Forward );
  setMotorSpeed( M1, 100 );
  setMotorDirection( M2, Forward );
  setMotorSpeed( M2, 100 );

  delay(turn_delay);
  
  }


 if(right_sensor_state > 500 && left_sensor_state > 500)
{ 
  Serial.println("stop");
  
  setMotorSpeed( M1, 0 );
  setMotorSpeed( M2, 0 );
  digitalWrite(9, LOW); //Laser & Fan off

  laser_status = 0; //Laser status is off
  ircontrol = 0;
  
  } 


  if(irrecv.decode()){  // IR receive results
    ircontrol = 0;
    Serial.println("Emergency Stop Pressed");
    setMotorSpeed( M1, 0 );
    setMotorSpeed( M2, 0 );
    digitalWrite(9, LOW); //Laser & Fan off
    laser_status = 0; //Laser status is off
    delay(5000);
    
  }
  
}while(ircontrol != 0);

  }

  ircontrol = 0;
  irrecv.resume();
}


/**functions**/
void initMotor() {
  int i;
  for ( i = 0; i < MotorNum; i++ ) {
    digitalWrite(MotorPin[i].enPin, LOW);

    pinMode(MotorPin[i].enPin, OUTPUT);
    pinMode(MotorPin[i].directionPin, OUTPUT);
  }
}

/**  motorNumber: M1, M2
direction:          Forward, Backward **/
void setMotorDirection( int motorNumber, int direction ) {
  digitalWrite( MotorPin[motorNumber].directionPin, direction);
}

/** speed:  0-100   * */
inline void setMotorSpeed( int motorNumber, int speed ) {
  analogWrite(MotorPin[motorNumber].enPin, 255.0 * (speed / 100.0) ); //PWM
}

I still need to configure the motor speed to allow the laser to burn a continuous line. There may be other parts of the code that require adjusting, that will be known when I test run the robot.

Thank you for checking out my latest blog,

Dale Winhold

  • Sign in to reply
  • dwinhold
    dwinhold over 2 years ago in reply to DAB

    That is a great idea, I will definitely add that on!!

    Thank you DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 2 years ago

    You might want to add a flashing LED and audio to warn those around this robot that the laser is active.

    You have to keep those eyeballs safe.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube