diff --git a/HX-Joystick.ino b/HX-Joystick.ino new file mode 100644 index 0000000..d75b9d9 --- /dev/null +++ b/HX-Joystick.ino @@ -0,0 +1,92 @@ +// +// FILE: HX-Joystick.ino +// AUTHOR: Didier Aeschimann +// PURPOSE: HandBrake Joystick +// URL: https://github.com/RobTillaart/HX711 + + +#include "HX711.h" +#include + +HX711 scale; + +Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 0, 0, false, false, false, false, false, false, false, false, false, true, false); + +String readString, servo1, servo2; + +// adjust pins if needed +uint8_t dataPin = 6; +uint8_t clockPin = 7; +long brake_; + +void setup() +{ + Joystick.begin(); + Serial.begin(115200); + scale.begin(dataPin, clockPin); + scale.set_raw_mode(); + scale.tare(); +} + +bool showConfig; +bool showValue; + +long MinVal=0; +long MaxVal=0; + +void loop() +{ + brake_ = scale.read(); + + //Min -140550 + if(brake_ < MinVal) + MinVal = brake_; + //Max 1253444 + if(brake_ > MaxVal) + MaxVal = brake_; + + if(brake_ <= 0) + Joystick.setBrake(0); + + if(brake_ > 0) + { + brake_ = map(brake_, 0, 1300000, 0, 1024); + Joystick.setBrake(brake_); + } + + while (Serial.available()) { + if (Serial.available() >0) { + char c = Serial.read(); //gets one byte from serial buffer + if(c == 'v') + showValue=!showValue; + elseif(c == 'c') + showConfig=true; + else + readString += c; //makes the string readString + } + } + + if (showConfig) { + + } + + 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(10); + +} +