Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理arduino连接困难_Arduino_Processing - Fatal编程技术网

处理arduino连接困难

处理arduino连接困难,arduino,processing,Arduino,Processing,我正试图将字符从处理发送到arduino,但arduino只识别其中的两个 问题在于“2”字符 当我按下“s”键时,处理代码正在发送“2”字符,因为我可以看到arduino rx led灯亮起,但电机什么也不做 对于“1”或“0”字符,我没有问题, 我将arduino代码中的“2”切换为向前行驶,然后再切换为向后行驶,但分配了“2”字符的代码在这两种情况下都不起作用, 正如我所说,“1”和“0”字符的发送和接收都很好 我猜这是arduino代码中的一些东西,但我不知道是什么 arduino代码:

我正试图将字符从处理发送到arduino,但arduino只识别其中的两个

问题在于“2”字符

当我按下“s”键时,处理代码正在发送“2”字符,因为我可以看到arduino rx led灯亮起,但电机什么也不做

对于“1”或“0”字符,我没有问题, 我将arduino代码中的“2”切换为向前行驶,然后再切换为向后行驶,但分配了“2”字符的代码在这两种情况下都不起作用, 正如我所说,“1”和“0”字符的发送和接收都很好

我猜这是arduino代码中的一些东西,但我不知道是什么

arduino代码:

//skipped some code.
void loop() {
    if (Serial.available()>0) {
        // If data is available to 
        read, val = Serial.read(); 
        // read it and store it in val 
    }

    if (val == 2) {
        drive_forward();
    }

    if (val == '1') {
        drive_reverse();
    }

    if (val == '0') {
        motor_stop();
    }
}
int-motor1=4;
int motor2=5;
char-val;
//---------------------------------------------------------------设置
无效设置(){
Serial.begin(9600);
//设置电机
引脚模式(电机1,输出);
引脚模式(电机2,输出);
}
//----------------------------------------------------------------循环
void loop(){
如果(Serial.available()>0)
{//如果数据可读取,
val=Serial.read();//读取并存储在val中
}
如果(val='2'){
向前行驶();
}
如果(val='1'){
倒车档();
}
如果(val='0'){
电机停止();
}
}
//-------------------------------------------------------------驾驶
无效电机停止(){
数字写入(电机1,低电平);
数字写入(电机2,低电平);
}
无效向前行驶(){
数字写入(电机1,高);
数字写入(电机2,低电平);
延误(15);
数字写入(电机1,低电平);
数字写入(电机2,低电平);
延误(15);
}
无效驾驶_倒车(){
数字写入(电机2,高);
数字写入(电机1,低电平);
延误(15);
数字写入(电机2,低电平);
数字写入(电机1,低电平);
延误(15);
}
处理代码:

//skipped some code
void keyPressed() {
    if (key == 'w' || key == 'W') {
        myPort.write(1); 
    }

    if (key == 's' || key == 'S') {
        myPort.write(2);
        println("2");
    }
} 

void keyReleased() {
    myPort.write(0);
    println("0");
}
import processing.serial.*;
串行端口;
无效设置()
{
规模(200200);
myPort=new Serial(这是Serial.list()[2],9600);
}
作废提款(){
} 
按下void键(){
如果(键=='w'| |键=='w')
{ 
myPort.write('1');
println(“1”);}
如果(键=='s'| |键=='s')
{ 
myPort.write('2');
println(“2”);}
}
void keyReleased(){
myPort.write('0');
println(“0”);

}

正如@tailedmouse所说,以整数形式发送数据

处理代码:

//skipped some code
void keyPressed() {
    if (key == 'w' || key == 'W') {
        myPort.write(1); 
    }

    if (key == 's' || key == 'S') {
        myPort.write(2);
        println("2");
    }
} 

void keyReleased() {
    myPort.write(0);
    println("0");
}
Arduino代码:

//skipped some code.
void loop() {
    if (Serial.available()>0) {
        // If data is available to 
        read, val = Serial.read(); 
        // read it and store it in val 
    }

    if (val == 2) {
        drive_forward();
    }

    if (val == '1') {
        drive_reverse();
    }

    if (val == '0') {
        motor_stop();
    }
}

正如@tailedmouse所说,以整数形式发送数据

处理代码:

//skipped some code
void keyPressed() {
    if (key == 'w' || key == 'W') {
        myPort.write(1); 
    }

    if (key == 's' || key == 'S') {
        myPort.write(2);
        println("2");
    }
} 

void keyReleased() {
    myPort.write(0);
    println("0");
}
Arduino代码:

//skipped some code.
void loop() {
    if (Serial.available()>0) {
        // If data is available to 
        read, val = Serial.read(); 
        // read it and store it in val 
    }

    if (val == 2) {
        drive_forward();
    }

    if (val == '1') {
        drive_reverse();
    }

    if (val == '0') {
        motor_stop();
    }
}

不太可能是问题,但您应该将val初始化为“0”。您是否尝试使用Arduino串行监视器?只有一个程序可以使用com端口,因此如果我使用串行监视器,我将无法将处理程序连接到Arduino并查看它到底接收到什么,因为当我尝试使用readChar()而不是read()从processingTry发送'2'字符时,我看到RX led亮起。还可以尝试使用print(),而不是write(),但我不确定这是否正确。尝试将这些字符串作为整数发送,对我来说似乎效果更好。这不太可能是问题所在,但您应该将val初始化为“0”。您是否尝试使用Arduino串行监视器?只有一个程序可以使用com端口,因此,如果我使用串行监视器,我将无法将处理程序连接到arduino并查看它到底接收到了什么,因为当我尝试使用readChar()而不是read()从processingTry发送“2”字符时,RX led会亮起。还可以尝试使用print()而不是write(),但我不确定这是否正确。尝试将这些字符串作为整数发送,这似乎对我更有效