hi.
how can i use 2 codes combined .
Line Tracking Robot and 8*16 LED Board
unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#define SCL_Pin A5 //Set clock pin to A5
#define SDA_Pin A4 //Set data pin to A4
void setup(){
//Set pin to output
pinMode(SCL_Pin,OUTPUT);
pinMode(SDA_Pin,OUTPUT);
//Clear the matrix display
matrix_display(clear);
}
void loop(){
matrix_display(start01); //Display start pattern
delay(2000);
matrix_display(front); ///Front pattern
delay(2000);
matrix_display(STOP01); //Stop pattern
delay(2000);
matrix_display(clear); //Clear the matrix display
delay(2000);
}
//this function is used for dot matrix display
void matrix_display(unsigned char matrix_value[ ])
{
IIC_start(); //the function to call the data transmission
IIC_send(0xc0); //Select address
for(int i = 0;i < 16;i++) //Pattern data has 16 bytes
{
IIC_send(matrix_value[i]); //data to convey patterns
}
IIC_end(); //end the transmission of patterns data
IIC_start();
IIC_send(0x8A); //display control, set pulse width to 4/16
IIC_end();
}
// the condition that data transmission starts
void IIC_start()
{
digitalWrite(SCL_Pin,HIGH);
delayMicroseconds(3);
digitalWrite(SDA_Pin,HIGH);
delayMicroseconds(3);
digitalWrite(SDA_Pin,LOW);
delayMicroseconds(3);
}
// transmit data
void IIC_send(unsigned char send_data)
{
for(char i = 0;i < 8;i++) //Every character has 8 bits
{
digitalWrite(SCL_Pin,LOW); //pull down the SCL_Pin to change the signal of SDA
delayMicroseconds(3);
if(send_data & 0x01) //1 or 0 of byte is used to set high and low level of SDA_Pin
{
digitalWrite(SDA_Pin,HIGH);
}
else
{
digitalWrite(SDA_Pin,LOW);
}
delayMicroseconds(3);
digitalWrite(SCL_Pin,HIGH); //Pull up SCL_Pin to stop data transmission
delayMicroseconds(3);
send_data = send_data >> 1; //Detect bit by bit, so move the data right by one bit
}
}
//the sign that data transmission ends
void IIC_end()
{
digitalWrite(SCL_Pin,LOW);
delayMicroseconds(3);
digitalWrite(SDA_Pin,LOW);
delayMicroseconds(3);
digitalWrite(SCL_Pin,HIGH);
delayMicroseconds(3);
digitalWrite(SDA_Pin,HIGH);
delayMicroseconds(3);
}
////////////////////
#define ML_Ctrl 4 //define direction control pin of B motor
#define ML_PWM 5 //define PWM control pin of B motor
#define MR_Ctrl 2 //define direction control pin of A motor
#define MR_PWM 9 //define PWM control pin of A motor
const int sensor_l = 6;//define the pin of left line tracking sensor
const int sensor_c = 7;//define the pin of middle line tracking sensor
const int sensor_r = 8;//define the pin of right line tracking sensor
int l_val,c_val,r_val;//define these variables
void setup() {
Serial.begin(9600);//start serial monitor and set baud rate to 9600
pinMode(ML_Ctrl, OUTPUT);//set direction control pin of B motor
pinMode(ML_PWM, OUTPUT);//set PWM control pin of B motor to OUTPUT
pinMode(MR_Ctrl, OUTPUT);//set direction control pin of A motor to OUTPUT
pinMode(MR_PWM, OUTPUT);//set PWM control pin of A motor to OUTPUT
pinMode(sensor_l,INPUT);//set the pins of left line tracking sensor to INPUT
pinMode(sensor_c,INPUT);//set the pins of middle line tracking sensor to INPUT
pinMode(sensor_r,INPUT);//set the pins of right line tracking sensor to INPUT
}
void loop()
{
tracking(); //run main program
}
void tracking()
{
l_val = digitalRead(sensor_l);//read the value of left line tracking sensor
c_val = digitalRead(sensor_c);//read the value of middle line tracking sensor
r_val = digitalRead(sensor_r);//read the value of right line tracking sensor
if(c_val == 1)//if the state of middle one is 1, which means detecting black line
{
front();//car goes forward
}
else
{
if((l_val == 1)&&(r_val == 0))//if only left line tracking sensor detects black trace
{
left();//car turns left
}
else if((l_val == 0)&&(r_val == 1))//if only right line tracking sensor detects black trace
{
right();//car turns right
}
else// if left and right line tracking sensors detect black trace or they don’t read
{
Stop();//car stops
}
}
}
void front()//define the status of going forward
{
digitalWrite(ML_Ctrl,HIGH);//set direction control pin of B motor to HIGH
analogWrite(ML_PWM,70);//set PWM control speed of B motor to 70
digitalWrite(MR_Ctrl,HIGH);//set direction control pin of A motor to HIGH
analogWrite(MR_PWM,70);//set PWM control speed of A motor to 70
}
void back()//define the state of going back
{
digitalWrite(ML_Ctrl,LOW);//set direction control pin of B motor to LOW
analogWrite(ML_PWM,200);//set PWM control speed of B motor to 200
digitalWrite(MR_Ctrl,LOW);//set direction control pin of A motor to LOW
analogWrite(MR_PWM,200);//set PWM control speed of A motor to 200
}
void left()//car turns left
{
digitalWrite(ML_Ctrl,LOW);//set direction control pin of B motor to LOW
analogWrite(ML_PWM,200);//set PWM control speed of B motor to 200
digitalWrite(MR_Ctrl,HIGH);//set direction control pin of A motor to HIGH level
analogWrite(MR_PWM,200);//set PWM control speed of A motor to 200
}
void right()//define the right-turning state
{
digitalWrite(ML_Ctrl,HIGH);//set direction control pin of B motor to HIGH level
analogWrite(ML_PWM,200);//set PWM control speed of B motor to 200
digitalWrite(MR_Ctrl,LOW);//set direction control pin of A motor to LOW
analogWrite(MR_PWM,200);//set PWM control speed of A motor to 200
}
void Stop()//define the state of stop
{
analogWrite(ML_PWM,0);//set PWM control speed of B motor to 0
analogWrite(MR_PWM,0);//set PWM control speed of A motor to 0
}