Pages: [1] 2 3 ... 5   Go Down
Author Topic: what input pin is it using?  (Read 3441 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 287
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi there just looking into using a hall sensor on my arduino and came across this code below, i dont know if im being blind but where does it state which input pin its using?

Code:


const int ledPin = 13;
volatile int rpmcount;
int sensorState = 0;
unsigned int rpm;
unsigned long timeold;

void rpm_fun()
 {
      rpmcount++;
      digitalWrite(ledPin, HIGH);
      delay(50);
      digitalWrite(ledPin, LOW);
 }
 
void setup() {
  pinMode(ledPin, OUTPUT);     
  Serial.begin(9600); 
  attachInterrupt(0, rpm_fun, FALLING); 
}

void loop(){
 
   if(rpmcount>=5) {
      rpm=(60000*rpmcount)/(8*(millis()-timeold)); //the 8 changes to 4 if 4 magnets are used.
      timeold = millis();
      //Serial.println(rpmcount,DEC);
      rpmcount = 0;
      Serial.println(rpm,DEC);
   }
}
Logged

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

Code:
attachInterrupt(0, rpm_fun, FALLING); 
Right there, pin 2.
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.

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/AttachInterrupt
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There's a delay(50) in the ISR. Not a good idea.

 
Logged

Examples can be found in your IDE.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 287
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

o ok i see thanks for that link one problem i can see is i am using an arduino uno and i will eventually need 3 hall inputs so how will i overcome that?

Quote
There's a delay(50) in the ISR. Not a good idea.
yes there is where it is blinking the led i dont actually need that led how can i get rid of that function as ive noticed if i remove void rpm_fun() i get a error
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes there is where it is blinking the led i dont actually need that led how can i get rid of that function as ive noticed if i remove void rpm_fun() i get a error

You could remove the blinking by taking out the two lines of code that write HIGH and LOW to the ledPin, and the delay between them.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 287
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok so basically leave it there but take the internals out of it.

would this work or am i barking up the wrong tree?

Code:


volatile int rpmcount;
int sensorState = 0;
unsigned int rpm;
unsigned long timeold;
int rpm1 =1;


void setup() {
  Serial.begin(9600); 
 pinMode(INPUT, rpm1);
}
void loop(){
 
   if(rpm1>=5) {
      rpm=(60000*rpmcount)/(8*(millis()-timeold)); //the 8 changes to 4 if 4 magnets are used.
      timeold = millis();
      //Serial.println(rpmcount,DEC);
      rpmcount = 0;
      Serial.println(rpm,DEC);
   }
}
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 446
Posts: 23872
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"i will eventually need 3 hall inputs so how will i overcome that?"

You can have an interrupt on any pin, they are called PCINT.
See NickGammon's writeup on them here
http://www.gammon.com.au/forum/?id=11488
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4036
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can keep the blinking led.
Move the blink part outside of the ISR. Set up a global byte variable and make it volatile to use as a blink flag, maybe call it bFlag. When the ISR runs, set that to 1 in the ISR. Then in loop() set up an if ( bFlag ) { } that makes the led blink and sets bFlag = 0.

So your ISR increments rpm count and flags the blink, it runs in next to nothing flat and is ready to service the next interrupt that fast.

IIRC you can set up any I/O pin to interrupt on Level Change. But look into things, maybe you don't need to use interrupts at all. Most things don't at 16 MHz.

You'd do better to learn how to use timers to run multiple tasks without using blocking code.
This is Nick Gammon's site, he's a master at explanations.
http://www.gammon.com.au/forum/?id=11411




Logged

Examples can be found in your IDE.

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

would this work or am i barking up the wrong tree?

Wrong tree entirely. You have completely removed the interrupt handler now.

In your original sketch, this interrupt handler was called when you got a falling edge on pin 2:

Code:
void rpm_fun()
 {
      rpmcount++;
      digitalWrite(ledPin, HIGH);
      delay(50);
      digitalWrite(ledPin, LOW);
 }

These are the lines of code that turn ledPin HIGH, wait 50 milliseconds and then turn it LOW again:

Code:
      digitalWrite(ledPin, HIGH);
      delay(50);
      digitalWrite(ledPin, LOW);

If you take those three lines out it will no longer flash the LED and will no longer have that unwise delay() in the interrupt handler.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 287
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi thanks for your replies on that link http://www.gammon.com.au/forum/?id=11488 there is a lot of stuff on that website to get your head wrapped around lol i have come up with this compiles fine but somethings telling me im missing something here?

Code:


volatile int rpmcount;
int sensorState = 0;
unsigned int rpm;
unsigned long timeold;


ISR (PCINT2_vect)
 {
 // handle pin change interrupt for D0 to D7 here
 }  // end of PCINT2_vect


void setup ()
  {
  Serial.begin(9600); 
  // pin change interrupt (example for D0)
  PCMSK2 |= _BV (PCINT16);  // want pin 0
  PCIFR  |= _BV (PCIF2);   // clear any outstanding interrupts
  PCICR  |= _BV (PCIE2);   // enable pin change interrupts for D0 to D7
  }

void loop(){
 
   if(rpmcount>=5) {
      rpm=(60000*rpmcount)/(8*(millis()-timeold)); //the 8 changes to 4 if 4 magnets are used.
      timeold = millis();
      //Serial.println(rpmcount,DEC);
      rpmcount = 0;
      Serial.println(rpm,DEC);
   }
}


p.s i dont wont the led output
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 90
Posts: 3528
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

somethings telling me im missing something here?
How about something to update rpmcount ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 287
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:


volatile int rpmcount;
int sensorState = 0;
unsigned int rpm;
unsigned long timeold;


ISR (PCINT2_vect)
 {
   rpmcount;
 // handle pin change interrupt for D0 to D7 here
 }  // end of PCINT2_vect


void setup ()
  {
  Serial.begin(9600); 
  // pin change interrupt (example for D0)
  PCMSK2 |= _BV (PCINT16);  // want pin 0
  PCIFR  |= _BV (PCIF2);   // clear any outstanding interrupts
  PCICR  |= _BV (PCIE2);   // enable pin change interrupts for D0 to D7
  }

void loop(){
 
   if(rpmcount>=5) {
      rpm=(60000*rpmcount)/(8*(millis()-timeold)); //the 8 changes to 4 if 4 magnets are used.
      timeold = millis();
      //Serial.println(rpmcount,DEC);
      rpmcount = 0;
      Serial.println(rpm,DEC);
   }
}


better?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 516
Posts: 31579
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
better?
No try:-

Code:
rpmcount++;
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You started with a sketch that did more or less what you wanted, in a fairly simple way - no messing with hardware registers and so on.

Why have you thrown that away and started again with a more complex approach?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1] 2 3 ... 5   Go Up
Jump to: