Pages: [1]   Go Down
Author Topic: Reading a pulse signal  (Read 790 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi peeps, just starting playing with my Arduino and having lots of fun. .lol

I am after reading my speedo pulse on my car and am hoping someone could confirm if I'm doing it right.

I will be getting the pulse by using this direct to pin2



The code I have done so far is...

Code:
int inPin = 2;   // choose the input pin (for pulse count)
int val = 0;     // variable for reading the pulse count
int pulseval = 0; // variable for counting pulse
long currentmils = 0; // variable to hold current mills value
int currentspeed = 0; // holds current speed value (needs to be calibrated)
int ledPin = 13;                 // LED connected to digital pin 13
int ledval = 0;
void setup() {
  pinMode(inPin, INPUT);    // declare pushbutton as input
  currentmils =  millis();  // set current millis
  Serial.begin(9600); // using serial output so I can monitor whats going on.
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop(){
  
  if ( val == digitalRead(inPin) ) {
  } else {
        val = digitalRead(inPin);  
      pulseval = pulseval + 1;
  }

  if ((currentmils + 100) < millis()) { // runs every second, will shortern time once calibrated
        currentmils = millis();
        currentspeed = pulseval / 2; // current speed needs calibrating via GPS, but its a start
      Serial.print(currentspeed);
        if (ledval == 0 ) {
        digitalWrite(ledPin, HIGH);
        ledval = 1;
        } else {
        digitalWrite(ledPin, LOW);
        ledval = 0;
}
        pulseval = 0;
  }
}

I was going to use pulsein but this pauses the code and I need to add few other sensors as well.

For some reason <> doesnt seem to be accepted on == which is why I'm using

  if ( val == digitalRead(inPin) ) {
  } else {

any advice would help

cheers

Steve
Logged

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
For some reason <> doesnt seem to be accepted on == which is why I'm using

  if ( val == digitalRead(inPin) ) {
  } else {

any advice would help

Try using != instead of <>:

if (val != digitalRead(inPin))  // if value is not equal to digitalRead(inPin)
{

}

Other than this I haven't really looked at your code.  I'll take a closer look if you get stuck or try it out and observe problems you can describe.

- Ben
Logged


Central Florida, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I hope you have not wired it up as your schematic shows. Your Arduino will see 7.2V on its input pins at a 12V nominal input. Since an automobile electrical system can be 13.6V (or more) when engine is running, you will be putting ~ 8.25 into the inputs.

Reverse the resistors. You need to put 30K (or more) on the 12V side, and 20K (or less) to ground.


Logged

central nervous system
Offline Offline
Full Member
***
Karma: 0
Posts: 135
Causing havoc inside your brain since 1980
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote


You may find the following link helpful: http://www.arduino.cc/playground/uploads/Learning/Level_shifting_4_arduino.pdf

It refers to a circuit diagram for making Arduino sense a 12+-volt signal.
Logged

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

Just to be on the safe side put a diode with the anode on the digital pin and the cathode on the Arduino's 5V line. This will clamp the signal to a safe level in case the 12V gets bigger (like it does when the battery is charging).
Logged

Pages: [1]   Go Up
Jump to: