我的目的是做一辆遥控车,但不知道代码哪里有问题 arduino:- int c=0;
- int INA = 4; //电机 A 正反转控制端
- int PWMA = 5; //电机 A 调速端
- int INB = 7; //电机 B 正反转控制端
- int PWMB = 6; //电机 B 调速端
- void setup(){
- Serial.begin(9600);
- pinMode(INA,OUTPUT);
- pinMode(INB,OUTPUT); //配置电机输出 IO 口为输出
- }
- void loop(){
- if(Serial.available()){
- c=Serial.read();
- if(c==97) motosp(255,255);
- else if(c==98) motosp(-1,255);
- else if(c==99) motosp(255,-1);
- else if(c==100) motosp(-255,-255);
- }
- }
- void motosp(int sp1,int sp2)//声明电机速度控制函数。括号内定义的变量分别为左右电机速
- //度值,范围-255~+255,正值为正转,负值为反转。
- {
- if(sp1>0) {
- digitalWrite(INA, HIGH);
- }
- else
- {
- digitalWrite(INA, LOW);
- }
- if(sp2>0)
- {
- digitalWrite(INB, HIGH);
- }
- else
- {
- digitalWrite(INB, LOW);
- }
- analogWrite(PWMA,abs (sp1));
- analogWrite(PWMB,abs (sp2));
- }
复制代码 processing:
- import processing.serial.*;
- Serial port;
- PImage p;
- void setup(){
- port=new Serial(this,"COM5",9600);
- size(300,200);
- p=loadImage("steering_wheel.png");
- image(p,0,0);
- rect(100,0,100,100);
- }
- void mouseClicked(){
- if((mouseX>100)&(mouseX<200)&(mouseY<100)) {println("Ahead!"); port.write("a"); }
- else if((mouseX>0)&(mouseX<100)&(mouseY>100)) {println("turn left"); port.write("b"); }
- else if((mouseX>200)&(mouseX<300)&(mouseY>100)) {println("turn right"); port.write("c"); }
- else if((mouseX>100)&(mouseX<200)&(mouseY>100)) {println("Back!"); port.write("d"); }
- }
复制代码
|