Go Down

Topic: Reading a pulse signal (Read 854 times) previous topic - next topic

CdRskuLL

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: [Select]
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

bens

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

kBit

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.



neuron_upheaval

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.

Grumpy_Mike

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

Go Up