Advertisement
pleasedontcode

Real-time Force rev_01

Jan 4th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Real-time Force
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-05 00:54:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* My project involves connecting one end of a sample */
  21.     /* to a load cell and the other end to the shaft, */
  22.     /* moving the motor backward to make it break. I want */
  23.     /* to see the force generated in the load cell in */
  24.     /* real time. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29. #include <EasyButton.h>
  30. #include <HX711.h>
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void measureForce(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t PUSH_BUTTON_PIN = 2;
  39. const uint8_t LOAD_CELL_DATA_PIN = A0;
  40. const uint8_t LOAD_CELL_CLOCK_PIN = 3;
  41.  
  42. /****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
  43. EasyButton pushButton(PUSH_BUTTON_PIN);
  44. HX711 loadCell;
  45.  
  46. /****** SYSTEM REQUIREMENTS *****/
  47. // SYSTEM REQUIREMENT 1: Real-time force measurement
  48. const float LOAD_CELL_CALIBRATION_FACTOR = 420.0983; // Calibration factor for load cell
  49.  
  50. void setup(void)
  51. {
  52.   // Initialize Serial for debugging purposes
  53.   Serial.begin(115200);
  54.  
  55.   // Initialize the push button
  56.   pushButton.begin();
  57.  
  58.   // Set up the button callbacks
  59.   pushButton.onPressed([]() {
  60.     Serial.println("Button pressed");
  61.   });
  62.  
  63.   pushButton.onSequence(2, 1500, []() {
  64.     Serial.println("Double click");
  65.   });
  66.  
  67.   // Enable interrupt if supported
  68.   if (pushButton.supportsInterrupt()) {
  69.     pushButton.enableInterrupt([]() {
  70.       pushButton.read();
  71.     });
  72.     Serial.println("Button will be used through interrupts");
  73.   }
  74.  
  75.   // Set up the load cell
  76.   loadCell.begin(LOAD_CELL_DATA_PIN, LOAD_CELL_CLOCK_PIN);
  77.   loadCell.set_scale(LOAD_CELL_CALIBRATION_FACTOR);
  78.   loadCell.tare();
  79. }
  80.  
  81. void loop(void)
  82. {
  83.   // Measure and print force in real-time
  84.   measureForce();
  85.  
  86.   // Add delay if needed for real-time measurement
  87.   // delay(100); // Adjust the delay time according to the desired update rate
  88. }
  89.  
  90. void measureForce(void)
  91. {
  92.   float force = loadCell.get_units();
  93.   Serial.print("Force: ");
  94.   Serial.print(force);
  95.   Serial.println(" units");
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement