Hi everyone,
I'm new to arduino and im trying to control an RC car from my Macbook using WASD.
My UNO R4 Wifi digital pins are wired into a 4 relay board which controls the RC transmitter by bridging some connections on the RC PCB.
I'm using "Screen" on my osx terminal so I don't have to hit enter after every keypress to serial.
screen /dev/tty.usbmodemDC5475CBEFC02 9600
So far everything is working well. My only issue is that when I release one of the keys (W/A/S/D), it stays in that state. Eg; W is to accerlerate forwards, when I release that key, it stays accelerating.
Same thing with the other pins/relays.
I'm using a switch statement to do this. Right now, I have a case where if you press the spacebar, it sets the pins low. This works fine but I'd much rather have the pins go low when you release the corresponding key.
I've tried adding pinsLow() under each case but that just sets them low indefinitely.
I've also set the default state to be low but I believe it's stuck in the last "case".
I've had a look at other options, like PyGame, Processing, Johnny-five and they all seem like overkill. Surely there is an easy way that I can achieve this just within the Arduino IDE?
My coding knowledge isn't very great and I've been mostly reading other threads to figure things out so please forgive any spaghetti/wrong/redundent code.
Edit: I've also just realised I won't be able to turn and accelerate at the same time, but that might be a topic for another thread if I can't figure it out lol.
//#include <Keyboard.h>
//char ctrlKey = KEY_LEFT_GUI;
#define FORWARDS 2
#define BACKWARDS 3
#define LEFT 4
#define RIGHT 5
int pins[4] = { FORWARDS, BACKWARDS, LEFT, RIGHT };
int incomingByte = 0;
void setup() {
//Keyboard.begin();
// initialise pin outputs
init(pins);
// loop through pins and set output to low
pinsLow();
};
void loop() {
if (Serial.available() > 0) {
// read the incoming byte
incomingByte = Serial.read();
Serial.println(incomingByte);
// if incomingByte === case, then set pin output
pinsLow();
switch (incomingByte) {
case 'w':
Serial.println("Forward");
pinOff(pins[1]); // Turn off reverse relay
pinOn(pins[0], "forwards");
break;
case 's':
Serial.println("Backwards");
pinOff(pins[0]); // Turn off forwards relay
pinOn(pins[1], "backwards");
break;
case 'a':
Serial.println("Left");
pinOff(pins[3]); // Turn off turning right
pinOn(pins[2], "left");
break;
case 'd':
Serial.println("Right");
pinOff(pins[2]); // Turn off turning left
pinOn(pins[3], "right");
break;
case 32:
Serial.println("Turn off all motors");
pinsLow();
break;
default:
Serial.println("Standby");
pinsLow();
};
};
};
// turn pin on
void pinOn(int pin, String direction) {
digitalWrite(pin, HIGH);
//Serial.println(String("Pin: ") + pin + String(" Direction: ") + direction + String(" Active: true"));
};
// turn pin off
void pinOff(int pin) {
digitalWrite(pin, LOW);
//Serial.println(String("Pin: ") + pin + String(" Active: false"));
}
// turn all off
void pinsLow() {
for (int pin = 0; pin < 4; pin++) {
pinMode(pins[pin], OUTPUT);
digitalWrite(pins[pin], LOW);
};
};
// init pins
void init(int pins[]) {
Serial.begin(9600);
Serial.println("Arduino initialised:");
for (int pin = 0; pin < 4; pin++) {
printPin(digitalRead(pins[pin]), pins[pin]);
};
};
// print pin
void printPin(bool output, int pin) {
//Serial.println(String("Pin: ") + pin + String(" Active: ") + boolean(output));
}
Thanks in advance and sorry if this is in the wrong section.