Line Following Not Working When Activated By IR
this code:
what want line tracking activated ir , continue looping until stopped ir. when pressed 4 on remote activates line following case turns left endlessly(until press stop).
any ideas on how make work?
code: [select]
#include <irremote.h>
//line following
int sensor_1 = 0, sensor_2 = 0, sensor_3 = 0; //sensors
int flag = 0, current_flag = 0, counter = 0; //used count how many times car reverses.
string patch = ""; //used counter int string error later on.
string line_value = ""; //string value combines(not adds) sensor values give cases. patch comes in.
//pin assigment.
int receiverpin = 12; //the pin ir
unsigned long red; //used store incoming commands.
//
#define 16736925 //down ----->
#define b 16754775
#define x 16712445
#define c 16720605
#define d 16761405
#define e 5316027
#define f 2747854299
#define y 3622325019
#define g 1386468383
#define h 553536955 // make car go forward, back, left, right , stop.
#define 16738455 // code # related number 1 on remote
#define j 16750695 // code # related number 2 on remote
#define k 16756815 //code # related number 3 on remote
#define l 16724175 // code # related number 4 on remote
// abc...'s cases red value.
irrecv irrecv(receiverpin); //recive
decode_results results; //decode
//the pin identication put setup, because made code neater.
void setup()
{
serial.begin(9600); // set serial communication
pinmode(receiverpin, input); // ir stuff
irrecv.enableirin();
pinmode(10, input); // line follower s1
pinmode(4, input); // line follower s2
pinmode(2, input); // line follower s3
pinmode(6, output); // wheel 1
pinmode(7, output); // wheel 2
pinmode(8, output); // wheel 3
pinmode(9, output); // wheel 4
pinmode(5, output); // enable a
pinmode(11, output); // enable b
analogwrite(5, 120); //change speed of motors.(0~255)
analogwrite(11, 120);
}
void loop()
{
if (irrecv.decode(&results))
{
int red = results.value;
serial.println(red);
irrecv.resume();
delay(150);
switch (red) {
case a:
dforward();
break;
case b:
dback();
break;
case c:
dleft();
break;
case d:
dright();
break;
case e:
dforward();
break;
case f:
dback();
break;
case g:
dleft();
break;
case h:
dright();
break;
case x:
dstop();
break;
case y:
dstop();
break;
case i:
_shake();
dstop();
break;
case j:
_rod();
dstop();
break;
case k:
_stayng(); //wrong song name car dances verses "slide left, slide right, criss cross".
dstop();
break;
case l: // new code excuted when 4 on remote pressed.
//forward_move();
sensors_testing();
black_line_check();
}
}
}
void forward_move()
{
digitalwrite(6, 1); // forward right
digitalwrite(7, 0); // reverse right
digitalwrite(8, 0); // reverse left
digitalwrite(9, 1); // forward left
// if did reverse before forward_move ..... increment 1
if (current_flag == 1)
{
counter++;
current_flag = 0;
}
if (counter == 15)
{ serial.println("180 degrees");
rotate_right();
delay(5000);
counter = 0;
}
}
//these movements line following.
void reverse_move()
{
digitalwrite(6, 0); // forward right
digitalwrite(7, 1); // reverse right
digitalwrite(8, 1); // reverse left
digitalwrite(9, 0); // forward left
}
void rotate_right()
{
digitalwrite(6, 0); // forward right
digitalwrite(7, 1); // reverse right
digitalwrite(8, 0); // reverse left
digitalwrite(9, 1); // forward left
}
void rotate_left()
{
digitalwrite(6, 1); // forward right
digitalwrite(7, 0); // reverse right
digitalwrite(8, 1); // reverse left
digitalwrite(9, 0); // forward left
}
void turn_right()
{
digitalwrite(6, 0); // forward right
digitalwrite(7, 0); // reverse right
digitalwrite(8, 0); // reverse left
digitalwrite(9, 1); // forward left
}
void turn_left()
{
digitalwrite(6, 1); // forward right
digitalwrite(7, 0); // reverse right
digitalwrite(8, 0); // reverse left
digitalwrite(9, 0); // forward left
}
void stop()
{ if (flag == 0)
{
digitalwrite(6, 0); // forward right
digitalwrite(7, 0); // reverse right
digitalwrite(8, 0); // reverse left
digitalwrite(9, 0); // forward left
}
if (flag == 1)
{
reverse_move();
current_flag = 1;
serial.println(patch + counter);
}
}
void sensors_testing()
{
sensor_1 = digitalread(10); // left sensor
sensor_2 = digitalread(4); // center sensor
sensor_3 = digitalread(2); // right sensor
line_value = patch + sensor_1 + sensor_2 + sensor_3;
serial.println(line_value);
//delay(800);
}
void black_line_check()
{
if (line_value == "111")
{
serial.println("forward_move()");
forward_move();
flag = 1;
}
if (line_value == "010")
{
serial.println("forward_move()");
forward_move();
flag = 1;
}
if (line_value == "100")
{
serial.println("rotate_left()");
rotate_left();
flag = 1;
}
if (line_value == "001")
{
serial.println("rotate_right()");
rotate_right();
flag = 1;
}
if (line_value == "110")
{
serial.println("turn_left()");
turn_left();
flag = 1;
}
if (line_value == "000")
{
serial.println("stop()");
stop();
}
if (line_value == "011")
{
serial.println("turn_right()");
turn_right();
flag = 1;
}
//genaral actions , functions.
}
void dforward()
{
digitalwrite(5, high);
digitalwrite(11, high);
digitalwrite(6, high); //digital output
digitalwrite(7, low);
digitalwrite(8, low);
digitalwrite(9, high);
serial.println("forward");
}
void dback()
{
digitalwrite(5, high);
digitalwrite(11, high);
digitalwrite(6, low);
digitalwrite(7, high);
digitalwrite(8, high);
digitalwrite(9, low);
serial.println("back");
}
void dleft()
{
digitalwrite(5, high);
digitalwrite(11, high);
digitalwrite(6, high);
digitalwrite(7, low);
digitalwrite(8, high);
digitalwrite(9, low);
serial.println("left");
}
void dright()
{
digitalwrite(5, high);
digitalwrite(11, high);
digitalwrite(6, low);
digitalwrite(7, high);
digitalwrite(8, low);
digitalwrite(9, high);
serial.println("right");
}
void dstop()
{
digitalwrite(5, low);
digitalwrite(11, low);
serial.println("stop!");
}
void _snaker()
{
dright();
delay(50);
dleft();
delay(50);
dforward();
delay(50);
dright();
delay(50);
dleft();
delay(50);
dright();
delay(50);
dleft();
delay(50);
}
void _snake()
{
(int var2 = 0; var2 <= 20; var2++) {
_snaker();
if (var2 == 20) {
break;
serial.println("rod 1 done");
}
}
}
void _rodr()
{
dforward();
delay(100);
dback();
delay(100);
}
void _rod()
{
(int var1 = 0; var1 <= 5; var1++) {
_rodr();
}
}
void _shaker()
{
dright();
delay(40);
dleft();
delay(40);
}
void _shake()
{
(int var3 = 0; var3 <= 30; var3++) {
_shaker();
}
}
void _stayng() {
dleft();
delay(200);
dstop();
delay(100);
dforward();
delay(200);
dstop();
delay(700);
dright();
delay(420);
dstop();
delay(100);
dforward();
delay(300);
dstop();
delay(300);
dleft();
delay(220);
dstop();
delay(1000);
dright();
delay(200);
dleft();
delay(300);
dstop();
delay(1000);
}
what want line tracking activated ir , continue looping until stopped ir. when pressed 4 on remote activates line following case turns left endlessly(until press stop).
any ideas on how make work?
Arduino Forum > Using Arduino > Programming Questions > Line Following Not Working When Activated By IR
arduino
Comments
Post a Comment