Go Down

Topic: RPM reading is delayed... (Read 548 times) previous topic - next topic

ibourdon

I have a setup with 2 hall effect sensors to read 2 shafts and different rpms. When my code was just setup for one everything worked fine, now that I have two for some reason its not working properly anymore.. It takes anywhere from 5-10 revolutions before it starts counting up. Before that it will just display 1, 1, 1, 1 etc then finally goto 2,3,etc... Also once its counting properly if I hit my button to reset the count it has the same issue. Horrible code issue? I am using 2.2k resistors across my power & signal pin on each sensor.

Code: [Select]

const int SENSOR = 12;
const int SENSOR1 = 10;
int rpm = 0;
int rpm1 = 0;
int lasthall = 0;
int lasthall1 = 0;
int hallstate = 0;
int hallstate1 = 0;
const int button = 8;
const int button1 = 9;
int buttonstate = 0;
int buttonstate1 = 0;
void setup()
{
pinMode(SENSOR, INPUT);
        pinMode(SENSOR1, INPUT);
        pinMode(button, INPUT);
        pinMode(button1, INPUT);
digitalWrite(SENSOR, HIGH);
        digitalWrite(SENSOR1, HIGH);
        Serial.begin(9600);
}

void loop()
{   
  hallstate = digitalRead(SENSOR);
  hallstate1 = digitalRead(SENSOR1);

if (hallstate != lasthall)
{
  if (hallstate == LOW)
  {
    rpm++;
    Serial.print(rpm, DEC);
    Serial.println(" RPMs S1");
  }
}
  lasthall = hallstate;
  buttonstate = digitalRead(button);
  if (buttonstate == HIGH)
  {
    rpm = 0;
  }
 
if (hallstate1 != lasthall1)
{
  if (hallstate1 == LOW)
  {
    rpm1++;
    Serial.print(rpm1, DEC);
    Serial.println(" RPMs S2");
  }
}
  lasthall1 = hallstate1;
   
  buttonstate1 = digitalRead(button1);
  if (buttonstate1 == HIGH)
  {
    rpm1 = 0;
  }
}

dc42

I can see that you are enabling the internal pullup resistor on the sensor pins (despite having a 2k2 external pullup resistor by my reading of your description), but what about the button pins?
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

robtillaart


use interrupts for this kind of counting - http://www.arduino.cc/en/Reference/AttachInterrupt -

Code: [Select]

volatile unsigned long counter1 = 0;
volatile unsigned long counter2 = 0;

void setup()
{
  Serial.begin(115200);
  attachInterrupt(0, irq_1, FALLING);  // 0 ==> pin 2
  attachInterrupt(1, irq_2, FALLING);  // 1 ==> pin 3
}

void loop()
{
  Serial.println(counter1);
  Serial.println(counter2);
  Serial.println();
  delay(1000);
}

void irq_1()
{
  counter1++;
}

void irq_2()
{
  counter2++;
}



Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

ibourdon

I will try the interrupts way, as for the buttons I am using 10k from the "ground" pin from my button to the digital pin on my Arduino. So +5v -> button -> 10k ->Arduino

ibourdon

#4
Oct 13, 2011, 09:45 pm Last Edit: Oct 13, 2011, 09:48 pm by ibourdon Reason: 1
Here is my new code using interrupts. It seemed to work rather well before I added the reset buttons. Now I am having the same problem I was originally having.

Code: [Select]

volatile unsigned int rpm = 0;
volatile unsigned int rpm1 = 0;
const int b0 = 10;
const int b1 = 12;
int bs0 = 0;
int bs1 = 0;
void setup()
{
       attachInterrupt(0, irq_1, FALLING);
       attachInterrupt(1, irq_2, FALLING);
       Serial.begin(9600);
}

void loop(){
bs0 = digitalRead(b0);
bs1 = digitalRead(b1);

if (bs0 == HIGH)
{
 rpm = 0;
}
if (bs1 == HIGH)
{
 rpm1 = 0;
}}

void irq_1(){
 rpm++;
 Serial.print(rpm, DEC);
 Serial.println(" S1 RPMS");
}
void irq_2(){
 rpm1++;
 Serial.print(rpm1, DEC);
 Serial.println(" S2 RPMS");
}

Maybe I need some debouncing code? It works fine with 1 button without debounce code so I don't see why I would need it now.

ibourdon

Solved the problem. I just changed the button input pins to high and made the sketch look for a low and it works. Thanks for the interrupt suggestion. I need to start using them more often, very useful.

AWOL

Serial prints in an ISR are rarely a good idea.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up