Code not detecting RPM 2 on Hall Effect Sensor – Arduino IDE

  arduino, arduino-ide, c++

I am trying to write a code that measures the RPM of 2 shafts using hall effect sensors and an arduino board. The shafts will rotate at the same speed, until 1 is then slowed down forcefully.

I am trying to get the code to stop when RPM of shaft 1 does not equal the RPM of shaft 2.

The code I have, it is not detecting RPM1 at all , only RPM2 and I don’t know why

Note: All code was done in Arduino IDE (C++)

RPM CODE FOR SENSOR 1

int sensor = 2; // Hall sensor at pin 2
volatile byte counts;
unsigned int rpm; //unsigned gives only positive values
unsigned long previoustime;
void count_function()
{ /*The ISR function
Called on Interrupt
Update counts*/
counts++;
}
void setup() {
Serial.begin(9600);
//Intiates Serial communications
attachInterrupt(0, count_function, RISING); //Interrupts are called on Rise of Input
pinMode(sensor, INPUT); //Sets sensor as input
counts= 0;
rpm = 0;
previoustime = 0; //Initialise the values
}
void loop()
{
delay(1000);//Update RPM every second
detachInterrupt(0); //Interrupts are disabled
rpm = 60*1000/(millis() - previoustime)*counts;
previoustime = millis(); //Resets the clock
counts= 0; //Resets the counter
Serial.print("RPM=");
Serial.println(rpm); //Calculated values are displayed
attachInterrupt(0, count_function, RISING); //Counter restarted
}

RPM code for 2 sensors requiring cutoff

int sensor = 2; // Hall sensor at pin 2

volatile byte counts;
unsigned int rpm; //unsigned gives only positive values
unsigned long previoustime;
void count_function()
{
  counts++;
}


// RPM 2


int sensor2 = 3; // Hall sensor at pin 2

volatile byte counter;
unsigned int rpm1; //unsigned gives only positive values
unsigned long previoustime1;


void isr()
{
  counter++;
}

void setup() {
  Serial.begin(9600);
  //Intiates Serial communications
  attachInterrupt(0, count_function, RISING); //Interrupts are called on Rise of Input
  pinMode(sensor, INPUT); //Sets sensor as input
  counts = 0;
  rpm = 0;
  previoustime = 0; //Initialise the values


  attachInterrupt(0, isr, RISING); //Interrupts are called on Rise of Input
  pinMode(sensor2, INPUT);
  counter = 0;
  rpm1 = 0;
  previoustime1 = 0; //Initialise the values
}
void loop() {

  delay(500);//Update RPM every second
  detachInterrupt(0); //Interrupts are disabled
  rpm = 30 * 1000 / (millis() - previoustime) * counts;
  previoustime = millis(); //Resets the clock
  counts = 0; //Resets the counter
  Serial.print("RPM1=");
  Serial.println(rpm); //Calculated values are displayed
  attachInterrupt(0, count_function, RISING); //Counter restarted

  delay(500);
  detachInterrupt(0); //Interrupts are disabled
  rpm1 = 30 * 1000 / (millis() - previoustime1) * counter;
  previoustime1 = millis(); //Resets the clock
  counter = 0; //Resets the counter
  Serial.print("RPM2=");
  Serial.println(rpm1);
  attachInterrupt(0, isr, RISING);


  if (rpm != rpm1) // If the RPM1 does not equal RPM2, then initiate switch off, of hall effect sensors and print out "Machine cut off" - so as to stop measuring RPM
  {

    analogWrite(sensor, 0);
    analogWrite(sensor2, 0);
    Serial.print(" Machine cut-off");
    delay(1000);
    exit(0);
  }
}

Source: Windows Questions C++

LEAVE A COMMENT