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);
}