문제

I decided to build a telepresence robot that would be controlled through a keyboard(using WASD) through serial. I've been waiting for my parts to arrive so I started doing some tests with processing and using the WASD keys to light up LED's.

import processing.serial.*;
import cc.arduino.*;
Serial myPort;
Arduino arduino;

void setup () {
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list() [1], 57600);
  arduino.pinMode(12, Arduino.OUTPUT);
  arduino.pinMode(11, Arduino.OUTPUT);
  arduino.pinMode(9, Arduino.OUTPUT);
  arduino.pinMode(10, Arduino.OUTPUT);
}

void draw() {
  if (keyPressed == true) {
    if (key == 'w' || key == 'W') {
      arduino.digitalWrite (12, Arduino.HIGH);
    }
    if (key == 's' || key == 'S') {
      arduino.digitalWrite (11, Arduino.HIGH);
    }
    if (key == 'a' || key == 'A') {
      arduino.digitalWrite (9, Arduino.HIGH);
    }
    if (key == 'd' || key == 'D') {
      arduino.digitalWrite (10, Arduino.HIGH);
    }
    else {
      arduino.digitalWrite (12, Arduino.LOW);
      arduino.digitalWrite (11, Arduino.LOW);
      arduino.digitalWrite (9, Arduino.LOW);
      arduino.digitalWrite (10, Arduino.LOW);
    }
  }
}

Everything works fine except when I press the 'D' key. Whenever I press it (or any other letter used in the last if) the LED lights up but doesn't turn off when I release the key. I've tried changing the LED pin, and the order in which the "if" statements are but the last one always has the same problem. The Arduino is running the StandardFirmata sketch. I'm using an Arduino Uno, Win 7 x64 and Processing 1.5.1 since 2.0.2 was giving me an error. Any help would be great.

Thanks

도움이 되었습니까?

해결책

It looks like your 'else' statement is under the wrong 'if' block. Based on your goal, you should have it so that if a key is not pressed, you are writing Arduino.LOW to pins 9-12. Basically, just move a bracket:

void draw() {
  if (keyPressed == true) {
    if (key == 'w' || key == 'W') {
      arduino.digitalWrite (12, Arduino.HIGH);
    }
    if (key == 's' || key == 'S') {
      arduino.digitalWrite (11, Arduino.HIGH);
    }
    if (key == 'a' || key == 'A') {
      arduino.digitalWrite (9, Arduino.HIGH);
    }
    if (key == 'd' || key == 'D') {
      arduino.digitalWrite (10, Arduino.HIGH);
    }
  }
  else {
      arduino.digitalWrite (12, Arduino.LOW);
      arduino.digitalWrite (11, Arduino.LOW);
      arduino.digitalWrite (9, Arduino.LOW);
      arduino.digitalWrite (10, Arduino.LOW);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top