Files
handbrake_joystick/HX-Joystick.ino
2026-01-14 13:51:22 -05:00

148 lines
3.2 KiB
C++

//
// FILE: HX-Joystick.ino
// AUTHOR: Didier Aeschimann
// PURPOSE: HandBrake Joystick
// URL: https://git.home.daprogs.net/DAProgs/handbrake_joystick/
#define BrakeMode
#include "HX711.h"
#include <Joystick.h>
HX711 scale;
#ifdef BrakeMode
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 0, 0, false, false, false, false, false, false, false, false, false, true, false);
#else
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 0, 0, true, false, false, false, false, false, false, false, false, false, false);
#endif
String readString, servo1, servo2;
// adjust pins if needed
uint8_t dataPin = 6;
uint8_t clockPin = 7;
long brake_;
long conf_minValue=0;
long conf_maxValue=13000;
void setup()
{
Joystick.begin();
Serial.begin(115200);
scale.begin(dataPin, clockPin);
scale.set_raw_mode();
scale.tare();
}
bool showConfig;
bool showValue;
bool showDebug;
long loadCell;
long MinVal=0;
long MaxVal=0;
void loop()
{
brake_ = scale.read();
loadCell = brake_ / 100;
//Min -140550
if(loadCell < MinVal)
MinVal = loadCell;
//Max 1253444
if(loadCell > MaxVal)
MaxVal = loadCell;
if(loadCell > conf_maxValue)
conf_maxValue = loadCell;
if(loadCell <= conf_minValue)
brake_ = 0;
if(loadCell > conf_minValue)
brake_ = map(loadCell, conf_minValue, (conf_maxValue+1), 0, 1024);
// long test;
// test = map(21320,0,21321,0,1023);
// Serial.println(test);
#ifdef BrakeMode
Joystick.setBrake(brake_);
#else
Joystick.setXAxis(brake_);
#endif
while (Serial.available()) {
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
if(c == 'v')
showValue=!showValue;
else if(c == 'c')
showConfig=true;
else if(c == 'd')
showDebug=!showDebug;
else
readString += c; //makes the string readString
}
}
if (showValue) {
Serial.println(brake_);
}
if (showDebug) {
Serial.print(brake_);
Serial.print(", ");
Serial.print(loadCell);
Serial.print(", ");
Serial.print(MinVal);
Serial.print(", ");
Serial.print(MaxVal);
Serial.println();
}
if (showConfig) {
showConfig = false;
Serial.println("DAProgs Handbrake Joystick. v1.0");
Serial.println("Configs:");
Serial.print(" minVal=");
Serial.println(MinVal);
Serial.print(" maxVal=");
Serial.println(MaxVal);
Serial.print(" zero=");
Serial.println(conf_minValue);
Serial.print(" maximum=");
Serial.println(conf_maxValue);
Serial.print(" Current=");
Serial.println(brake_);
#ifdef BrakeMode
Serial.println(" mode=brake");
#else
Serial.println(" mode=axis");
#endif
Serial.println("");
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 5); //get the first four characters
servo2 = readString.substring(5, 10); //get the next four characters
Serial.println(servo1); //print ot serial monitor to see results
Serial.println(servo2);
int n1; //declare as number
int n2;
readString="";
}
delay(2);
}