// Change the values below to suit your robot's motors, weight, wheel type, etc. #define KP .2 #define KD 5 #define M1_DEFAULT_SPEED 50 #define M2_DEFAULT_SPEED 50 #define M1_MAX_SPEED 70 #define M2_MAX_SPEED 70 #define MIDDLE_SENSOR 3 #define NUM_SENSORS 5 // number of sensors used #define TIMEOUT 2500 // waits for 2500 us for sensor outputs to go low #define EMITTER_PIN 2 // emitter is controlled by digital pin 2 #define DEBUG 0 // set to 1 if serial debug output needed
int leftMotorSpeed = M1_DEFAULT_SPEED + motorSpeed;
int rightMotorSpeed = M2_DEFAULT_SPEED - motorSpeed;
// set motor speeds using the two motor speed variables above
set_motors(leftMotorSpeed, rightMotorSpeed);
}
void set_motors(int motor1speed, int motor2speed)
{
if (motor1speed > M1_MAX_SPEED ) motor1speed = M1_MAX_SPEED; // limit top speed
if (motor2speed > M2_MAX_SPEED ) motor2speed = M2_MAX_SPEED; // limit top speed
if (motor1speed < 0) motor1speed = 0; // keep motor above 0
if (motor2speed < 0) motor2speed = 0; // keep motor speed above 0
motor1.setSpeed(motor1speed); // set motor speed
motor2.setSpeed(motor2speed); // set motor speed
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void manual_calibration() {
int i;
for (i = 0; i < 250; i++) // the calibration will take a few seconds
{
qtrrc.calibrate(QTR_EMITTERS_ON);
delay(20);
}
if (DEBUG) { // if true, generate sensor dats via serial output
Serial.begin(9600);
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMinimumOn*);*
Serial.print(' ');*
}*
Serial.println();*
for (int i = 0; i < NUM_SENSORS; i++)*
{*
_ Serial.print(qtrrc.calibratedMaximumOn*);_
_ Serial.print(' ');_
_ }_
_ Serial.println();_
_ Serial.println();_
_ }_
_}[/quote]*_ But this tutor use pololu sensor and I can't find it in my country. I just can find KY-033 sensor. Anyone help to show code like this tutor by changing pololu sensor with KY-033 sensor? many thanks to you
#include <PololuQTRSensors.h>
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ ); // PIN 11 - create motor #1 pwm
AF_DCMotor motor2(2, MOTOR12_8KHZ ); // PIN 3 - create motor #2 pwm
// Change the values below to suit your robot's motors, weight, wheel type, etc.
#define KP .2
#define KD 5
#define M1_DEFAULT_SPEED 50
#define M2_DEFAULT_SPEED 50
#define M1_MAX_SPEED 70
#define M2_MAX_SPEED 70
#define MIDDLE_SENSOR 3
#define NUM_SENSORS 5 // number of sensors used
#define TIMEOUT 2500 // waits for 2500 us for sensor outputs to go low
#define EMITTER_PIN 2 // emitter is controlled by digital pin 2
#define DEBUG 0 // set to 1 if serial debug output needed
PololuQTRSensorsRC qtrrc((unsigned char[]) { 18,17,16,15,14} ,NUM_SENSORS, TIMEOUT, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
void setup()
{
delay(1000);
manual_calibration();
set_motors(0,0);
}
int lastError = 0;
int last_proportional = 0;
int integral = 0;
void loop()
{
unsigned int sensors[5];
int position = qtrrc.readLine(sensors);
int error = position - 2000;
int motorSpeed = KP * error + KD * (error - lastError);
lastError = error;
int leftMotorSpeed = M1_DEFAULT_SPEED + motorSpeed;
int rightMotorSpeed = M2_DEFAULT_SPEED - motorSpeed;
// set motor speeds using the two motor speed variables above
set_motors(leftMotorSpeed, rightMotorSpeed);
}
void set_motors(int motor1speed, int motor2speed)
{
if (motor1speed > M1_MAX_SPEED ) motor1speed = M1_MAX_SPEED; // limit top speed
if (motor2speed > M2_MAX_SPEED ) motor2speed = M2_MAX_SPEED; // limit top speed
if (motor1speed < 0) motor1speed = 0; // keep motor above 0
if (motor2speed < 0) motor2speed = 0; // keep motor speed above 0
motor1.setSpeed(motor1speed); // set motor speed
motor2.setSpeed(motor2speed); // set motor speed
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void manual_calibration() {
int i;
for (i = 0; i < 250; i++) // the calibration will take a few seconds
{
qtrrc.calibrate(QTR_EMITTERS_ON);
delay(20);
}
if (DEBUG) { // if true, generate sensor dats via serial output
Serial.begin(9600);
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMinimumOn);
Serial.print(' ');
}
Serial.println();
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMaximumOn);
Serial.print(' ');
}
Serial.println();
Serial.println();
}
}
Have you tried writing a short program to show you what effect you get when using the sensor - just reading the value of the signal and printing it to the Serial Monito and then manually holding the sensor over and beside the line.
Robin2:
Have you tried writing a short program to show you what effect you get when using the sensor - just reading the value of the signal and printing it to the Serial Monito and then manually holding the sensor over and beside the line.
...R
I dunno what do you mean. I wonder if someone help me with the complete script..thanks
// declaring Shield
int dataPin = 8;
int latchPin = 12;
int clockPin = 4;
int en = 7;
// declaring Sensor Pins.
int LeftSensor = A1;
int RightSensor = A0;
int L_sensor_val = 0; // To store value from sensors.
int R_sensor_val = 0;
int threshold = 300; // Threshold value to distinguish black and white.
void setup()
{ // setting up shield.
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(en, OUTPUT);
digitalWrite(en, LOW);
}
void loop()
{
L_sensor_val = analogRead(LeftSensor); // Reading Left sensor data
R_sensor_val = analogRead(RightSensor); // Reading Right sensor data
if(L_sensor_val<threshold && R_sensor_val>threshold) { // testing for left turn
while (L_sensor_val<threshold && R_sensor_val>threshold){
turn_right();
L_sensor_val = analogRead(LeftSensor);
R_sensor_val = analogRead(RightSensor);
}
}
else if(L_sensor_val>threshold && R_sensor_val<threshold){ // tesing for right turn
while (L_sensor_val>threshold && R_sensor_val<threshold){
turn_left();
L_sensor_val = analogRead(LeftSensor);
R_sensor_val = analogRead(RightSensor);
}
}
forward(); // Default movement is forward.
}
void forward(void){ // function for forward movement.
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 3);
digitalWrite(latchPin, HIGH);
}
void backward(void){ // function for forward movement.
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 164);
digitalWrite(latchPin, HIGH);
}
void turn_left(void){ // function for left turn.
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 161);
digitalWrite(latchPin, HIGH);
}
void turn_right(void){ // function for Right turn.
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 38);
digitalWrite(latchPin, HIGH);
}
void halt(void){ // function for stopping robot.
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 32);
digitalWrite(latchPin, HIGH);
}
this script follow white line, how I change for following black line? just change int threshold value?
"int LeftSensor = A1;
int RightSensor = A0;"
Can I use pin 13 and pin 2?
How I use
"Adafruit_DCMotor *myMotor1 = AFMS.getMotor(3); //left motor
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(4); //right motor
int Speed = 80;
myMotor1->setSpeed(Speed);
myMotor2->setSpeed(Speed);" for this script?
Can I?
Sorry for many question.....
@irruzzz, I think you should put your line-follower to one side for a few days and learn the basics of Arduino programming. Study several of the examples that come with the Arduino IDE and some of the C or C++ tutorials on the internet.
The code you posted in Reply #12 creates some variables and gives them values. The values are almost certainly the numbers of the Arduino pins that things are connected to.
What you are trying to achieve is not rocket-science, but neither is it as simple as making a cup of instant coffee. It require thinking, and the thinking must be informed by knowledge of the basics.