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 Challenge - Unlimited CNC Robot Blog #6
  • 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: 12 Sep 2022 11:23 AM Date Created
  • Views 425 views
  • Likes 6 likes
  • Comments 0 comments
  • Line Following
  • dwinhold
  • Unlimited CNC
  • designchallenge
  • Turn and Move Design Challenge with TE Robotics
Related
Recommended

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

dwinhold
dwinhold
12 Sep 2022

Unlimited CNC Robot Blog #6

I wanted to post this blog before I post my final blog tonight. I found a big error in my code and wanted to update it now.

The error I made was I had my line sensors reversed to what they needed to be for the right reading. I had a "<" instead of ">" and reverse of that.

I added new code as well, thanks to DAB for the suggestion!! Excellent idea and it works great!!

Below is the corrected code as well as the newly added code:

/**Unlimited CNC Robot**/

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


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;
int speakernote = NOTE_F4; 
int speakernote2 = NOTE_G4; 
int speakernote3 = NOTE_A5; 
  

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

  pinMode(9, OUTPUT); /**Power to Laser & Fan**/
  pinMode(4, OUTPUT); //LED Green
  pinMode(5, OUTPUT); //LED Yellow
  pinMode(6, OUTPUT); //LED Red
  pinMode(8, OUTPUT); //LED Speakernote
  
}

void loop() {

  digitalWrite(5, LOW); //LED Yellow - Power off
  digitalWrite(6, LOW); //LED Green - Power off
  Serial.println("Waiting for IR Signal to start");
  digitalWrite(4, HIGH); //LED Green - Power On
  
  if(irrecv.decode()){  // IR receive results
    ircontrol = 1;
    delay(500);
    digitalWrite(5, HIGH); //LED Yellow - 1st warning
    delay(1000);
    tone(8, speakernote, 600); //Sound warning 1
    delay(1000);
    noTone(8);
    tone(8, speakernote, 600); //Sound warning 2
    delay(1000);
    noTone(8);
    tone(8, speakernote, 600); //Sound warning 3
    delay(1000);
    noTone(8);
    tone(8, speakernote, 600); //Sound warning 4
    delay(1000);
    noTone(8);
    tone(8, speakernote3, 600); //Sound warning 5
    delay(1000);
    noTone(8);
    digitalWrite(6, HIGH); //LED Red - Robot Running
    delay(2000);
 }
 
  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/Fan On");
      setMotorSpeed( M1, 0 );
      setMotorSpeed( M2, 0 );
      digitalWrite(9, HIGH); //Laser & Fan on
      tone(8, speakernote2, 600); //Laser On signal
      delay(250);
      tone(8, speakernote3, 600); //Laser On signal
      delay(250);
      noTone(8);

      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);
      tone(8, speakernote3, 600); //Laser Off signal
      delay(250);
      tone(8, speakernote2, 600); //Laser Off signal
      delay(250);
      noTone(8);

      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;
    tone(8, speakernote3, 600); //Laser Off signal
    delay(250);
    tone(8, speakernote2, 600); //Laser Off signal
    delay(2000);
    noTone(8);
  
  } 


  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(2000);
    tone(8, speakernote3, 600); //Laser Off signal
    delay(250);
    tone(8, speakernote2, 600); //Laser Off signal
    delay(250);
    tone(8, speakernote, 600); //Shutdown signal
    delay(200);
    tone(8, speakernote, 600); //Shutdown signal
    delay(200);
    tone(8, speakernote, 600); //Shutdown signal
    delay(200);
    noTone(8);
    
  }
  

}while(ircontrol != 0);

  }

  digitalWrite(5, HIGH); //LED Yellow - Off
  digitalWrite(6, HIGH); //LED Red - Off
  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
}

The final blog coming tonight!!

Dale Winhold

  • Sign in to reply
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