- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x3f, 16, 2);//如果12864不显示请更换地址0x3f
- int Left_motor=8; //左电机(IN3) 输出0 前进 输出1 后退
- int Left_motor_pwm=9; //左电机PWM调速
- int Right_motor_pwm=10; // 右电机PWM调速
- int Right_motor=11; // 右电机后退(IN1) 输出0 前进 输出1 后退
- char buffer[18]; //串口缓冲区的字符数组
- void setup() //设定串口和引脚模式
- {
- lcd.init();
- lcd.backlight();
- Serial.begin(115200);
- Serial.flush(); //清空串口缓存
- pinMode(Left_motor,OUTPUT); // PIN 8 8脚无PWM功能
- pinMode(Left_motor_pwm,OUTPUT); // PIN 9 (PWM)
- pinMode(Right_motor_pwm,OUTPUT);// PIN 10 (PWM)
- pinMode(Right_motor,OUTPUT);// PIN 11 (PWM)
- analogWrite(Left_motor_pwm,0);
- analogWrite(Right_motor_pwm,0);
- }
- void run() // 前进
- {
- digitalWrite(Right_motor,LOW); // 右电机前进
- digitalWrite(Right_motor_pwm,HIGH); // 右电机前进
- analogWrite(Right_motor_pwm,255);//PWM比例0~255调速,左右轮差异略增减
-
-
- digitalWrite(Left_motor,HIGH); // 左电机前进
- digitalWrite(Left_motor_pwm,HIGH); //左电机PWM
- analogWrite(Left_motor_pwm,255);//PWM比例0~255调速,左右轮差异略增减
- }
- void goslow() // 前进
- {
- digitalWrite(Right_motor,LOW); // 右电机前进
- digitalWrite(Right_motor_pwm,HIGH); // 右电机前进
- analogWrite(Right_motor_pwm,125);//PWM比例0~255调速,左右轮差异略增减
-
-
- digitalWrite(Left_motor,HIGH); // 左电机前进
- digitalWrite(Left_motor_pwm,HIGH); //左电机PWM
- analogWrite(Left_motor_pwm,155);//PWM比例0~255调速,左右轮差异略增减
- }
- void brake() //刹车,停车
- {
-
- digitalWrite(Right_motor_pwm,LOW); // 右电机PWM 调速输出0
- analogWrite(Right_motor_pwm,0);//PWM比例0~255调速,左右轮差异略增减
- digitalWrite(Left_motor_pwm,LOW); //左电机PWM 调速输出0
- analogWrite(Left_motor_pwm,0);//PWM比例0~255调速,左右轮差异略增减
- //delay(time * 100);//执行时间,可以调整
- }
- void left() //左转(左轮不动,右轮前进)
- {
- digitalWrite(Right_motor,LOW); // 右电机前进
- digitalWrite(Right_motor_pwm,HIGH); // 右电机前进
- analogWrite(Right_motor_pwm,255);//PWM比例0~255调速,左右轮差异略增减
-
- digitalWrite(Left_motor,LOW); // 左电机后退
- digitalWrite(Left_motor_pwm,HIGH); //左电机PWM
- analogWrite(Left_motor_pwm,255);//PWM比例0~255调速,左右轮差异略增减
- }
- /*void spin_left() //左转(左轮后退,右轮前进)
- {
- digitalWrite(Right_motor,LOW); // 右电机前进
- digitalWrite(Right_motor_pwm,HIGH); // 右电机前进
- analogWrite(Right_motor_pwm,150);//PWM比例0~255调速,左右轮差异略增减
-
- digitalWrite(Left_motor,HIGH); // 左电机后退
- digitalWrite(Left_motor_pwm,HIGH); //左电机PWM
- analogWrite(Left_motor_pwm,150);//PWM比例0~255调速,左右轮差异略增减
- //delay(time * 100); //执行时间,可以调整
- }
- */
- void right() //右转(右轮不动,左轮前进)
- {
- //小车右转
- digitalWrite(Right_motor,HIGH); // 右电机后退
- digitalWrite(Right_motor_pwm,HIGH); // 右电机PWM输出1
- analogWrite(Right_motor_pwm,255);//PWM比例0~255调速,左右轮差异略增减
-
-
- digitalWrite(Left_motor,HIGH); // 左电机前进
- digitalWrite(Left_motor_pwm,HIGH); //左电机PWM
- analogWrite(Left_motor_pwm,255);//PWM比例0~255调速,左右轮差异略增减
- }
- /*
- void spin_right() //右转(右轮后退,左轮前进)
- {
- digitalWrite(Right_motor,HIGH); // 右电机后退
- digitalWrite(Right_motor_pwm,HIGH); // 右电机PWM输出1
- analogWrite(Right_motor_pwm,150);//PWM比例0~255调速,左右轮差异略增减
-
-
- digitalWrite(Left_motor,LOW); // 左电机前进
- digitalWrite(Left_motor_pwm,HIGH); //左电机PWM
- analogWrite(Left_motor_pwm,150);//PWM比例0~255调速,左右轮差异略增减
- //delay(time * 100); //执行时间,可以调整
- }
- */
- void back() //后退
- {
- digitalWrite(Right_motor,HIGH); // 右电机后退
- digitalWrite(Right_motor_pwm,HIGH); // 右电机前进
- analogWrite(Right_motor_pwm,255);//PWM比例0~255调速,左右轮差异略增减
-
-
- digitalWrite(Left_motor,LOW); // 左电机后退
- digitalWrite(Left_motor_pwm,HIGH); //左电机PWM
- analogWrite(Left_motor_pwm,255);//PWM比例0~255调速,左右轮差异略增减
- }
- void loop()
- {
-
- // lcd.setCursor(0,0); //设置显示指针
- // lcd.print("123456789"); //输出字符到LCD1602上
-
- if(Serial.available() > 0) //Serial.available()返回串口收到的字节数
- {
- int index = 0;
- delay(100); //延时等待串口收完数据,否则刚收到1个字节时就会执行后续程序
- int numChar = Serial.available();
- if(numChar > 17) //确认数据不会溢出,应当小于缓冲大小
- {
- numChar = 17;
- }
- while(numChar--)
- {
- buffer[index++] = Serial.read(); //将串口数据一字一字的存入缓冲
- }
- splitString(buffer); //字符串分割
- }
- }
- void splitString(char *data)
- {
- lcd.init();
- // Serial.print("Data entered:");
- // Serial.println(data);
- char *parameter;
- parameter = strtok(data, " ,"); //string token,将data按照空格或者,进行分割并截取
- // Serial.print("***");
- // Serial.println(parameter);
- lcd.setCursor(0,0); //设置显示指针
- lcd.print(parameter); //输出字符到LCD1602上
- while(parameter != NULL)
- {
- setLED(parameter);
- parameter = strtok(NULL, " ,"); //string token,再次分割并截取,直至截取后的字符为空
- // Serial.print("---");
- // Serial.println(parameter);
- }
- for(int x = 0; x < 18; x++) //清空缓冲
- {
- buffer[x] = '\0';
- }
- Serial.flush();
- }
- void setLED(char *data)
- {
- if((data[0] == '\r') &&(data[1] == '\n') &&(data[2] == '+') && (data[3] == 'A')&& (data[4] == 'S')&&(data[5] == 'R') && (data[6] == 'E')&& (data[7] == 'V')&&(data[8] == 'E') && (data[9] == 'N')&& (data[10] == 'T')&&(data[11] == ':')
- && (data[12] == '1')&& (data[13] == '1'))
- {
- // Serial.println("go forward!");
- run();
- }
-
- if((data[0] == '\r') &&(data[1] == '\n') &&(data[2] == '+') && (data[3] == 'A')&& (data[4] == 'S')&&(data[5] == 'R') && (data[6] == 'E')&& (data[7] == 'V')&&(data[8] == 'E') && (data[9] == 'N')&& (data[10] == 'T')&&(data[11] == ':')
- && (data[12] == '1')&& (data[13] == '2'))
- {
- // Serial.println("go back!");
- back();
- }
- if((data[0] == '\r') &&(data[1] == '\n') &&(data[2] == '+') && (data[3] == 'A')&& (data[4] == 'S')&&(data[5] == 'R') && (data[6] == 'E')&& (data[7] == 'V')&&(data[8] == 'E') && (data[9] == 'N')&& (data[10] == 'T')&&(data[11] == ':')
- && (data[12] == '1')&& (data[13] == '3'))
- {
- // Serial.println("go left!");
- left();
- }
- if((data[0] == '\r') &&(data[1] == '\n') &&(data[2] == '+') && (data[3] == 'A')&& (data[4] == 'S')&&(data[5] == 'R') && (data[6] == 'E')&& (data[7] == 'V')&&(data[8] == 'E') && (data[9] == 'N')&& (data[10] == 'T')&&(data[11] == ':')
- && (data[12] == '1')&& (data[13] == '4'))
- {
- // Serial.println("go right!");
- right();
- }
- if((data[0] == '\r') &&(data[1] == '\n') &&(data[2] == '+') && (data[3] == 'A')&& (data[4] == 'S')&&(data[5] == 'R') && (data[6] == 'E')&& (data[7] == 'V')&&(data[8] == 'E') && (data[9] == 'N')&& (data[10] == 'T')&&(data[11] == ':')
- && (data[12] == '1')&& (data[13] == '5'))
- {
- // Serial.println("Stop!");
- brake();
- }
- if((data[0] == '\r') &&(data[1] == '\n') &&(data[2] == '+') && (data[3] == 'A')&& (data[4] == 'S')&&(data[5] == 'R') && (data[6] == 'E')&& (data[7] == 'V')&&(data[8] == 'E') && (data[9] == 'N')&& (data[10] == 'T')&&(data[11] == ':')
- && (data[12] == '1')&& (data[13] == '6'))
- {
- // Serial.println("goslow!");
- goslow();
- }
- /* else
- {
- Serial.println("Stop!");
- brake();
- }
- */
- }
复制代码 |