0
\$\begingroup\$

I have a code where I have 2 functions:

1 - read_loadcell - which reads 5 HX711 (5 load cells)

2 - rpm_read - which is supposed to read rpm's using an inductive sensor. Basically I have a metallic wheel which has 4 symmetric holes in it. Each time when a whole is under the sensor I get pin in high state.

Now my issue:

  1. If I run in loop() only "read_loadcell" is working fine.
  2. If I run in loop() only "rpm_read" also working fine.
  3. The issue is that if i run both of them, the rpm don t work anymore

So the question is how can i have them both working?

#include "HX711.h"

HX711 scale_GS1(A1, A0); 
HX711 scale_GS2(A3, A2);
HX711 scale_GS3(13, 12);
HX711 scale_GS4(11, 10);
HX711 scale_FS(9, 8);

float gs1 = 0, gs2 = 0, gs3 = 0,gs4 = 0, fs = 0;

// read RPM

volatile int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;

void setup() {
  attachInterrupt(0, rpm_fan, RISING);//interrupt cero (0) is on pin two(2).

  Serial.begin(9600);   //start serial communication

  Serial.println();  Serial.println();  Serial.println();
  Serial.println("/////     WELCOME     ///////");
  Serial.println();

  //set scales
  scale_GS1.set_scale(1324);
  scale_GS1.tare();

  scale_GS2.set_scale(1324);
  scale_GS2.tare();

  scale_GS3.set_scale(1324);
  scale_GS3.tare();

  scale_GS4.set_scale(1324);
  scale_GS4.tare();

  scale_FS.set_scale(1324);
  scale_FS.tare();

  Serial.println("Readings:");
}

void loop() {

rpm_read();

read_loadcell();

}

void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets high.*/
  rpmcount++;
}

void rpm_read(){
  if (millis() - lastmillis == 1000){  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/

 detachInterrupt(0);    //Disable interrupt when calculating


 rpm = rpmcount * 15;  /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/

 Serial.print("RPM =\t"); //print the word "RPM" and tab.
 Serial.println(rpm); // print the rpm value.


 rpmcount = 0; // Restart the RPM counter
 lastmillis = millis(); // Uptade lasmillis
 attachInterrupt(0, rpm_fan, RISING); //enable interrupt
  }
}

void read_loadcell(){

  scale_GS1.power_up();
  gs1 = scale_GS1.get_units();
  scale_GS1.power_down(); 

  scale_GS2.power_up();
  gs2 = scale_GS2.get_units();
  scale_GS2.power_down(); 

  scale_GS3.power_up();
  gs3 = scale_GS3.get_units();
  scale_GS3.power_down(); 

  scale_GS4.power_up();
  gs4 = scale_GS4.get_units();
  scale_GS4.power_down(); 

  scale_FS.power_up();
  fs = scale_FS.get_units();
  scale_FS.power_down(); 

  Serial.print("GS1 :\t");      
  Serial.print(gs1);
  Serial.print("\t GS2 :\t");      
  Serial.print(gs2);
  Serial.print("\t GS3 :\t");      
  Serial.print(gs3);
  Serial.print("\t GS4 :\t");      
  Serial.print(gs4);
  Serial.print("\t FS :\t");      
  Serial.println(fs);
}
| improve this question | | | | |
\$\endgroup\$
-1
\$\begingroup\$
#include "HX711.h"

HX711 scale_GS1(A1, A0); 
HX711 scale_GS2(A3, A2);
HX711 scale_GS3(13, 12);
HX711 scale_GS4(11, 10);
HX711 scale_FS(9, 8);

float gs1 = 0, gs2 = 0, gs3 = 0,gs4 = 0, fs = 0;

// read RPM

volatile int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;

void setup() {
  attachInterrupt(0, rpm_fan, RISING);//interrupt cero (0) is on pin two(2).

  Serial.begin(9600);   //start serial communication

  Serial.println();  Serial.println();  Serial.println();
  Serial.println("/////     WELCOME     ///////");
  Serial.println();

  //set scales
  scale_GS1.set_scale(1324);
  scale_GS1.tare();

  scale_GS2.set_scale(1324);
  scale_GS2.tare();

  scale_GS3.set_scale(1324);
  scale_GS3.tare();

  scale_GS4.set_scale(1324);
  scale_GS4.tare();

  scale_FS.set_scale(1324);
  scale_FS.tare();

  Serial.println("Readings:");
}

void loop() {

rpm_read();

read_loadcell();

}

void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets high.*/
  rpmcount++;
}

void rpm_read(){


  unsigned long elapsedTime, localRpm, interruptCount;

  elapsedTime = millis() - lastmillis;
  if (elapsedTime >= 1000) {
    noInterrupts();
    interruptCount = rpmcount;
    rpmcount = 0;
    lastmillis = millis();
    interrupts();   
    localRpm = 15 * 1000 * interruptCount / elapsedTime ;
    Serial.print("RPM =\t"); //print the word "RPM" and tab.
    Serial.println(localRpm); // print the rpm value.
  }


}

void read_loadcell(){

  scale_GS1.power_up();
  gs1 = scale_GS1.get_units();
  scale_GS1.power_down(); 

  scale_GS2.power_up();
  gs2 = scale_GS2.get_units();
  scale_GS2.power_down(); 

  scale_GS3.power_up();
  gs3 = scale_GS3.get_units();
  scale_GS3.power_down(); 

  scale_GS4.power_up();
  gs4 = scale_GS4.get_units();
  scale_GS4.power_down(); 

  scale_FS.power_up();
  fs = scale_FS.get_units();
  scale_FS.power_down(); 

  Serial.print("GS1 :\t");      
  Serial.print(gs1);
  Serial.print("\t GS2 :\t");      
  Serial.print(gs2);
  Serial.print("\t GS3 :\t");      
  Serial.print(gs3);
  Serial.print("\t GS4 :\t");      
  Serial.print(gs4);
  Serial.print("\t FS :\t");      
  Serial.println(fs);
}
| improve this answer | | | | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.