Me and my team are trying to use an arduino to measure the force on a load cell, as well as the rpm of a shaft. Both are needed for our final design and getting them to work in tandem is hard. We are using an hx711 amplifier with our load cell which limits the sample rate pretty low. The rpm is measured blisteringly fast though, so to sort of even them out we used the following code. We are using the hx711 library by bogdan necula. The issue with the code is that one, we cant use a tare() function to set the starting mass to 0, as putting it in the while loop resets it to zero every time it loops. The 2nd issue is that for some reason after about 30s the force stops reading and changing, it sets at whatever value it was at and won't update anymore. Ideally the tachometer reads really fast and the force sensor reads every .5s, just need insight as to why that's not happening.
#define lightsensor 5
#include "HX711.h"
unsigned long count;
unsigned long times;
const int LoadCellDout = 2;
const int LoadCellSCK = 3;
unsigned long dtime;
unsigned long timer;
double peenor;
HX711 scale;
void setup() {
// put your setup code here, to run once:
Serial.begin(250000);
count=0;
dtime=0;
times=0;
timer=0;
//Force Sensor Readings:
}
void loop() {
// put your main code here, to run repeatedly:
int reading = digitalRead(lightsensor);
Serial.print("\t");
Serial.print(dtime);
Serial.print("\n");
Serial.print("\t");
int millitime = (millis()-times);
/**
if ((millis()-times) == 500){
scale.begin(LoadCellDout, LoadCellSCK);
int calibration_factor = 856.4;
scale.set_scale(861.978);
scale.tare();
peeno = scale.get_units(1),5;
times = millis();
Serial.print('hello');
}
*/
int mtime = millis();
while ((mtime-times) == 500){
scale.begin(LoadCellDout, LoadCellSCK);
int calibration_factor = 856.4;
scale.set_scale(861.978);
scale.tare();
peeno = scale.get_units(1),5;
times = millis();
}
Serial.print(peenor);
Serial.print("\t");
static bool givesazerooronedepedningonthevalueofthelightsensoratacertainpoint = false;
if(reading-givesazerooronedepedningonthevalueofthelightsensoratacertainpoint==0&&reading==0) {
count++;
givesazerooronedepedningonthevalueofthelightsensoratacertainpoint=true;
dtime=micros()-times;
times=micros();
}
givesazerooronedepedningonthevalueofthelightsensoratacertainpoint=(reading==1?false:true);
Serial.print("\t");
Serial.print(count);
}