/*************************************************************************** Line tracing example using the L298N.
This software is free provided that this notice is not removed and proper attribution
is accorded to Layad Circuits and its Author(s). Layad Circuits invests resources in producing free software. By purchasing Layad Circuits'
products or utilizing its services, you support the continuing development of free software for all.
Author(s): CDM for Layad Circuits Electronics Engineering
Revision: 1.4.0 - Layad Circuits Electronics Engineering Supplies and Services
B314 Lopez Bldg., Session Rd. cor. Assumption Rd., Baguio City, Philippines www.layadcircuits.com
general: [email protected] sales: [email protected]
+63-916-442-8565 ***************************************************************************/
// change below to define if line // is black on white background or
// white on black background.
// These define the sensor value when// under black and white surface
// You may also use the DO and DO'// pins of the Saleng Tracker to
// change line configuration
/*Yellow IR
#define BLK LOW // line#define WHT HIGH // background
*/
#define BLK HIGH // line#define WHT LOW // background
// speed setting based on movement. Edit the speeds you need. These settings are up to 255 or full speed and down to 0 for completely no power
#define SPEED_ON_TURNS 90 //48
#define SPEED_FORWARD 90//64#define SPEED_BACKWARD 90//32
#define AIN1 2//IN1
#define BIN1 7//IN3#define AIN2 4//IN2
#define BIN2 5//IN4#define PWMA 3//
#define PWMB 6#define SENSOR_L 10
#define SENSOR_C 11#define SENSOR_R A0
byte sensorL, sensorC, sensorR;
void speedSetting(byte val){
analogWrite(PWMA,val); analogWrite(PWMB,val);
}
void forward()
{ speedSetting(SPEED_FORWARD);
digitalWrite(AIN1,LOW); digitalWrite(AIN2,HIGH);
digitalWrite(BIN1,LOW); digitalWrite(BIN2,HIGH);
}
void backward()
{ speedSetting(SPEED_BACKWARD);
digitalWrite(AIN1,HIGH); digitalWrite(AIN2,LOW);
digitalWrite(BIN1,HIGH); digitalWrite(BIN2,LOW);
}
void turnleft(){
speedSetting(SPEED_ON_TURNS); digitalWrite(AIN1,LOW);
digitalWrite(AIN2,HIGH); digitalWrite(BIN1,HIGH);
digitalWrite(BIN2,LOW);}
void turnright()
{ speedSetting(SPEED_ON_TURNS);
digitalWrite(AIN1,HIGH); digitalWrite(AIN2,LOW);
digitalWrite(BIN1,LOW); digitalWrite(BIN2,HIGH);
}
void motorstop(){
digitalWrite(AIN1,LOW); digitalWrite(AIN2,LOW);
digitalWrite(BIN1,LOW); digitalWrite(BIN2,LOW);
}
void shortbreak(){
digitalWrite(AIN1,HIGH); digitalWrite(AIN2,HIGH);
digitalWrite(BIN1,HIGH); digitalWrite(BIN2,HIGH);
}
void setup() {
pinMode(SENSOR_L,INPUT); pinMode(SENSOR_C,INPUT);
pinMode(SENSOR_R,INPUT); pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT); pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT); //we use a slow speed to avoid
// overshooting lines // and conserve battery
// full speed = 255 speedSetting(90); // change this speed as needed
delay(1000); // add 3s delay Serial.begin(115200);
}
void loop() { // read and store all sensors
sensorL = digitalRead(SENSOR_L);
sensorC = digitalRead(SENSOR_C); sensorR = digitalRead(SENSOR_R);
//forward();
/*if(sensorL == WHT && sensorC == BLK && sensorR == WHT){
forward(); }*/
if(sensorL == BLK && sensorC == BLK && sensorR == BLK) {
motorstop(); //do nothing OR stop by calling // or move forward with forward()
} else if(sensorL == WHT && sensorC == BLK && sensorR == BLK)
{ turnright();
} else if(sensorL == WHT && sensorC == BLK && sensorR == WHT)
{ // confused! Just move forward, or stop
forward(); }
else if(sensorL == BLK && sensorC == BLK && sensorR == WHT) {
turnleft(); }
else if(sensorL == BLK && sensorC == WHT && sensorR == BLK) {
forward(); }
else if(sensorL == WHT && sensorC == WHT && sensorR == BLK)
{ turnright();
} else if(sensorL == WHT && sensorC == WHT && sensorR == WHT)
{ forward();
} else if(sensorL == BLK && sensorC == WHT && sensorR == WHT)
{ turnleft();
}
}