1

I am making a program which will use stepper motors and a line tracking sensor. The idea is that when the linetracker detects a black surface both steppers must move forward, and when the linetrackers is not detecting black surface it should scan the surface for a black surface.

I made some code which makes the robot move forward if the linetracker has a set value. The problem is that when I implement the code for the line-tracker. My steppers are going to run very laggy probably because the arduino is constantly scanning if the value of the linetracker is below 500.

My question is, is there a way to program the line tracker without having it interfere on my stepper motors? (The stepper motors run smooth without the linetracker)

#include <RedBot.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Code voor de LCD
#define I2C_ADDR       0x3F // Define I2C Address (use i2c scanner if necessary)
#define BACKLIGHT_PIN  3
#define En_pin         2
#define Rw_pin         1
#define Rs_pin         0
#define D4_pin         4
#define D5_pin         5
#define D6_pin         6
#define D7_pin         7
#define LED_OFF        0

#define LED_ON         1
//Hier worden de eigen functies gemaakt door HEX code
uint8_t battery_1[8]=   {0xe,0x1b,0x11,0x11,0x11,0x11,0x11,0x1f};
uint8_t battery_2[8]=   {0xe,0x1b,0x11,0x11,0x11,0x11,0x1f,0x1f};
uint8_t battery_3[8]=   {0xe,0x1b,0x11,0x11,0x11,0x1f,0x1f,0x1f};
uint8_t battery_4[8]=   {0xe,0x1b,0x11,0x11,0x1f,0x1f,0x1f,0x1f};
uint8_t battery_5[8]=   {0xe,0x1b,0x11,0x1f,0x1f,0x1f,0x1f,0x1f};
uint8_t battery_6[8]=   {0xe,0x1b,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f};
uint8_t battery_7[8]=   {0xe,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f};
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

RedBotSensor left_sen    =    RedBotSensor(A3);   
RedBotSensor right_sen   =    RedBotSensor(A2);
int lineStandard         =    800;

void setup()
{ 
  scherm();
  //stepper 1
  pinMode(13,OUTPUT);   // Step
  pinMode(12,OUTPUT);   // Dir
  //stepper 2
  pinMode(11,OUTPUT);   //Step 2
  pinMode(10,OUTPUT);   //Dir 2 
  Serial.begin(9600);
  Serial.println("IR Sensor Readings:: ");
  //delay(500);
}

void loop()
{
  Serial.print(left_sen.read());
  Serial.print("\t");  
  Serial.print(right_sen.read());
  Serial.println();

  if(right_sen.read() > lineStandard)
    {
      left();
    }

  else if(left_sen.read() > lineStandard)
    {
      right();
    }

  if((left_sen.read() > lineStandard) && (right_sen.read() > lineStandard) )
    {
      Stop ();
    }

  else
    {
      forward();
    }
}

void right() 
{
  //stepper 1
  digitalWrite(13,HIGH);  
  delayMicroseconds(1500); 
  digitalWrite(13,LOW);   
  delayMicroseconds(1500); 
  //stepper 2
  digitalWrite(11,LOW);   
  delayMicroseconds(1500); 
  //Oranje lampje, rechter kant
  //digitalWrite(3,HIGH);
}
void Stop () 
{
  digitalWrite(13, LOW);   
  delay(1500);
  digitalWrite(11, LOW);
  delay(1500);
  //Rood lampje aansturen
  //digitalWrite(2,HIGH);
}
void left() 
{
  //stepper 1
  digitalWrite(11,HIGH);  
  delayMicroseconds(1500); 
  digitalWrite(11,LOW); 
  delayMicroseconds(1500); 
  //stepper 2
  digitalWrite(13,LOW); 
  delayMicroseconds(1500);
  //Oranje lampje, linker kant
  //digitalWrite(4,HIGH);
}
void forward() 
{
  //stepper 1
  digitalWrite(13,HIGH);    
  delayMicroseconds(1500); 
  digitalWrite(13,LOW); 
  delayMicroseconds(1500); 
  //stepper 2
  digitalWrite(11,HIGH);   
  delayMicroseconds(1500); 
  digitalWrite(11,LOW);   
  delayMicroseconds(1500);  
  //Groen lampje sturen
  //digitalWrite(1,HIGH);
}

void scherm()
{
  lcd.begin(16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(LED_ON);
  //Hier worden de eigen symbolen benoemd
  lcd.createChar(1, battery_1);
  lcd.createChar(2, battery_2);
  lcd.createChar(3, battery_3);
  lcd.createChar(4, battery_4);
  lcd.createChar(5, battery_5);
  lcd.createChar(6, battery_6);
  lcd.createChar(7, battery_7);
  lcd.home();
  lcd.print("Batterij Status");
  lcd.setCursor(0, 1);
  //Schrijf de batterij icoontjes naar het LCD
  lcd.write(1);
  lcd.write(2);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
  lcd.write(6);
  lcd.write(7);
}
  • Try removing your serial print statement. It's detrimental to performance. And then read up on how to use interrupts, which you could use in combination with a competitor. – Sanchises Dec 19 '16 at 21:25
1

You could use a driver for stepper motors, so you can control the motors using PWM. You call the PWM from Arduino using analogWrite(). After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (Read this link from the Arduino page). This will help you in your project, and maybe find the solution to your problems when reading the sensor at the same time. An advantage of using PWM is that you can also made smoother movement for your robot when it is crossing or finding its way.

I also recommend using two sensors (I suppose you're using IR), so if you want to track the black surface and there is a turn, one of the sensors will be detecting the line, and the other one wont. That way you can assume which way should the robot turn, making it more efficient.

  • Thanks for the comment, I am currently using 2 line tracking sensors which are working fine. My code is also finished but the problem with the laggy steppers persists. I am going to try using two arduinos. – Jordakoes Dec 20 '16 at 22:19
  • Well, using two arduinos would be a waste if you plan to do a simple function as a line tracker with two microcontrollers like the ATmega328P. Since the driver use MOSFETs to control your robot, you will be saving a redundancy in your circuit. Just a recommendation. If you don't have any problem with it, just go ahead. – Alejandro Harris Bonet Dec 21 '16 at 14:07
  • Also, buying a driver should be cheaper than other Arduino link – Alejandro Harris Bonet Dec 21 '16 at 14:10
  • I am using a drivers, It is the polulu drv8825 – Jordakoes Dec 22 '16 at 20:44
  • But the problem still persists, Am I doing something wrong in the coding? – Jordakoes Dec 22 '16 at 20:44
0

is there a way to program the line tracker without having it interfere on my stepper motors?

you are simply generating a pwm signal to drive your steppers. So make that generation in the background, through a timer isr for example.

Thus your main loop() will just be doing the low priority stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.