Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Joystick.h" //https://github.com/MHeironimus/ArduinoJoystickLibrary
- #include "HX711.h" //https://github.com/bogde/HX711
- #define DOUT 3
- #define CLK 2
- HX711 scale;
- float calibration_factor = -300; //this controls how sensitive the pedal is
- Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
- JOYSTICK_TYPE_JOYSTICK, 0, 0,
- false, false, false, false, false, false,
- false, false, false, true, false);
- int brake = 0;
- int lastBrakeValue = 0;
- void setup() {
- //Serial.begin(9600);
- scale.begin(DOUT, CLK);
- scale.set_scale();
- scale.tare(); //Reset the scale to 0
- long zero_factor = scale.read_average(); //Get a baseline reading
- scale.set_scale(calibration_factor);
- Joystick.setBrakeRange(0, 1023);
- Joystick.begin();
- }
- void loop() {
- int brake = scale.get_units();
- //Serial.print(String(brake) + " >> ");
- if(brake > 1023){
- brake = 1023;
- }else if(brake < 10){
- brake = 0;
- }
- //Serial.println(brake);
- if (lastBrakeValue != brake) {
- Joystick.setBrake(brake);
- lastBrakeValue = brake;
- }
- delay(1);
- }
Advertisement
Comments
-
- how do i make it work :( my load cells read with the test but when using this code it does not show any responses. i am new to arduino stuff. if you have any tips that would be great.
-
- #include "Joystick.h"
- #include "HX711.h"
- // HX711 circuit wiring
- const int LOADCELL_DOUT_PIN = 2;
- const int LOADCELL_SCK_PIN = 3;
- HX711 scale;
- // Adjust these values based on your load cell's actual minimum and maximum readings
- float calibration_factor = 40; // Adjust based on your setup
- long zeroValue = 0; // The zero value after taring
- long maxValue = 5000; // Adjusted maximum value based on your input
- // Initialize the Joystick library
- Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
- JOYSTICK_TYPE_JOYSTICK, 1, 0, // 1 button, no hat switch
- false, false, false, false, false, false,
- false, false, false, true, false); // Only the brake axis is enabled
- void setup() {
- Serial.begin(57600);
- scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
- scale.set_scale(calibration_factor); // Apply the calibration factor
- scale.tare(); // Reset the scale to zero
- Joystick.begin(false); // Initialize joystick without automatic send
- Joystick.setBrakeRange(0, 1023);
- }
- void loop() {
- if (scale.is_ready()) {
- long reading = scale.get_units();
- // Map the reading from the load cell range to the joystick brake range
- int brakeValue = map(reading, zeroValue, maxValue, 0, 1023);
- brakeValue = constrain(brakeValue, 0, 1023); // Ensure the value is within the expected range
- Joystick.setBrake(brakeValue);
- Joystick.sendState(); // Explicitly send the state to the joystick
- Serial.print("Mapped Brake value: ");
- Serial.println(brakeValue);
- } else {
- Serial.println("HX711 not found.");
- }
- delay(10); // Short delay for stability
- }
Add Comment
Please, Sign In to add comment