Pages: [1]   Go Down
Author Topic: RPM reading is delayed...  (Read 445 times)
0 Members and 1 Guest are viewing this topic.
Texas
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Lost in SPI FADE land
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
  }
}
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
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++;
}


Logged

Rob Tillaart

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

Texas
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Lost in SPI FADE land
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Texas
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Lost in SPI FADE land
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: October 13, 2011, 02:48:02 pm by ibourdon » Logged

Texas
Offline Offline
Jr. Member
**
Karma: 0
Posts: 69
Lost in SPI FADE land
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25898
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial prints in an ISR are rarely a good idea.
Logged

"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.

Pages: [1]   Go Up
Jump to: