Skip to content

Instantly share code, notes, and snippets.

@SteveCooling
Created January 16, 2018 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteveCooling/161e5da467b1df0d3d36233836ba276f to your computer and use it in GitHub Desktop.
Save SteveCooling/161e5da467b1df0d3d36233836ba276f to your computer and use it in GitHub Desktop.
Arduino SBUS receiver (RC Joystick and Mouse)
#include <Joystick.h>
#include <Mouse.h>
#include <FUTABA_SBUS.h>
// Uses FUTABA_SBUS library from:
// https://github.com/mikeshub/FUTABA_SBUS/
Joystick_ Joystick;
FUTABA_SBUS sBus;
const int RANGE_MIN = 0;
const int RANGE_MAX = 2000;
bool mouse_active = false;
int mouse_activate_chan = 7;
int mouse_x_chan = 1;
int mouse_y_chan = 2;
int mouse_a_chan = 0;
int mouse_x_offset = 0;
int mouse_y_offset = 0;
int mouse_active_thresh = 1500;
int mouse_button_thresh = 1500;
const int MOUSE_MAX_SPEED = 80;
void setup(){
sBus.begin();
Joystick.begin(false);
Mouse.begin();
Serial.begin(9600);
Joystick.setXAxisRange(RANGE_MIN, RANGE_MAX);
Joystick.setYAxisRange(RANGE_MIN, RANGE_MAX);
Joystick.setZAxisRange(RANGE_MIN, RANGE_MAX);
Joystick.setRxAxisRange(RANGE_MIN, RANGE_MAX);
Joystick.setRyAxisRange(RANGE_MIN, RANGE_MAX);
Joystick.setRzAxisRange(RANGE_MIN, RANGE_MAX);
}
void loop(){
sBus.FeedLine();
if (sBus.toChannels == 1){
sBus.UpdateServos();
sBus.UpdateChannels();
sBus.toChannels = 0;
if(sBus.channels[7] < mouse_active_thresh) {
if(mouse_active) {
Serial.println("Joystick mode");
mouse_active = false;
}
// Normal joystick operation
update_joystick();
} else {
// Use RC signal for controlling mouse.
if(!mouse_active) {
Serial.println("Mouse mode");
// Set axis zeros.
mouse_x_offset = map(sBus.channels[mouse_x_chan], RANGE_MIN, RANGE_MAX, -MOUSE_MAX_SPEED, MOUSE_MAX_SPEED);
mouse_y_offset = map(sBus.channels[mouse_y_chan], RANGE_MIN, RANGE_MAX, -MOUSE_MAX_SPEED, MOUSE_MAX_SPEED);
mouse_active = true;
}
update_mouse();
}
}
}
void update_mouse() {
Mouse.move(
map(sBus.channels[mouse_x_chan], RANGE_MIN, RANGE_MAX, -MOUSE_MAX_SPEED, MOUSE_MAX_SPEED) - mouse_x_offset,
map(sBus.channels[mouse_y_chan], RANGE_MIN, RANGE_MAX, MOUSE_MAX_SPEED, -MOUSE_MAX_SPEED) + mouse_y_offset, // This axis is inverted
0
);
if(sBus.channels[mouse_a_chan] > mouse_button_thresh) {
if(!Mouse.isPressed()) Mouse.press();
} else {
if(Mouse.isPressed()) Mouse.release();
}
}
void update_joystick() {
Joystick.setXAxis(sBus.channels[0]);
Joystick.setYAxis(sBus.channels[1]);
Joystick.setZAxis(sBus.channels[2]);
Joystick.setRxAxis(sBus.channels[3]);
Joystick.setRyAxis(sBus.channels[4]);
Joystick.setRzAxis(sBus.channels[5]);
Joystick.setRudder(sBus.channels[6]);
Joystick.setThrottle(sBus.channels[7]);
Joystick.sendState();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment