Hello there! I am working on a rover project that i finished a while ago, but i would like to add control capability through processing depending on keyboard input.
I have attempted to do so with little proccesing knowledge. However in my code when i press the correct key the motor turns on and works abut stays on even if i let go of the key. if anyone could help me i want to make it so the motor only turns when im actually pressing down the key!
here is my code:
Arduino:
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
char c;
void setup()
{
Serial.begin(9600);
AFMS.begin();
}
void loop()
{
char c;
if (Serial.available()){
c = Serial.read();
if (c == 'w'){
myMotor->setSpeed(150);
myMotor->run(FORWARD);
}
if (c == 'p'){
myMotor->setSpeed(150);
myMotor->run(BACKWARD);
}
if(c == 'o'){
myMotor->setSpeed(0);
}
}
}
Processing:
import processing.serial.*;
Serial myPort;
void setup()
{
myPort = new Serial(this, "COM5", 9600);
println(Serial.list());
}
void draw()
{
if (keyPressed) {
if (key == 'w') {
myPort.write('w');
} else if (key == 'p') {
myPort.write('p');
} else {
myPort.write('o');
}
}
}
Thank you!!