# Arduino and my motor bike

Good morning to everyone. I am a new user of Arduino and my English is very poor: sorry and thank you for your patience. I am very happy to discover Arduino, it can be used in a lot of situation, solving the problems of weliding a lot of wire with the op amp sketch. My bike use the fuel injection and I want to build a sistem that show me the fuel consumption of the bike. The injector signal is a square wave with variable period ( the rpm of the engine ) and variable duty cycle ( the amount of fuel injected each turn of the engine ) This signal contains both the informations about the speed of the bike ( lineary connected with the rpm ) and the amount of the fuel ( the Vrms of the injector, an RC in few words) 1) first problem: I understand that the minimum time in Arduino is micros, 4us of resolution. This is a poor resolution for the time I want to measure but maybe enought. For this reason I need a very fast program. It is not a problem if the system works in "hide" situation and give me the result every one second. 2) My programming experience is only a little bit more than zero, I know only the basic of ms dos.... Yes, this is the real problem. I am not ( yet!) confident with Arduino sintax and for this I ask for your help.

## I write the program I have in my mind in mix betwen basic and Arduino sistem to show you what I mean.

10 digitalRead (pin12) 20 if pin(12)=hight goto 10 - I am cheking if the injector start to open - if the signal is low the injector works so I - can go on with the program, othewise I - return up waiting for the change of the - state of the pin(12). No time is waste.

30 T_start= micros - I have the first time for my calculation 40 digitalRead (pin12) 50 if pin(12)= low goto 40 - same situation written before but in this - situation I am waiting fot the end of the - signal. 60 T_end=micros - The injector was opened fot T_start-T_end - us

70 digitalRead (pin12) 80 if pin(12)=hight goto 70 - I am waiting for the next cycle 90 T_cycle= micros At this point I have all the informations I need. T_start - T_cycle is the time of one turn of the engine that is 1/speed of the bike T_start - T_end is the time in which the injector was open so it contains the information about the amount of gasoline. My question is: how can I force the program to stay stopped in one point waiting for an event? Does exist in Arduino an istruction like goto that make the program read up and not dow? Tahnk you very much again Pietro PS: I already made this with two external IC that convert in a tension the informations and it works fine. But I want to learn how make it in a completly digital way.

My question is: how can I force the program to stay stopped in one point waiting for an event? Does exist in Arduino an istruction like goto that make the program read up and not dow?

Well, first off you can, but you probably don't want to; there is a watchdog timer that (I think) will reset the micor if it is held in an endless loop for too long.

Basically, though, you are looking for the statements/constructs of "while":

http://arduino.cc/en/Reference/While

and "do...while":

http://arduino.cc/en/Reference/DoWhile

...rather than using goto's as you are used to in BASIC, to do what you are trying to do. That, combined with the code idea you have should work.

But this isn't the best way to approach it.

The best way would be to tie you PWM input pin (pin12 or whatever) to an ISR (interrupt service routine), so that every time the pin state transitions from HIGH to LOW or LOW to HIGH, that routine is automatically called by the system, and then you can check the pin state and have the system change/calc the variables as needed. Then, in the loop() function you can write those variables (or other information using those variables) out to an LCD or something for the real-time information display.

Using an interrupt though has its pitfalls - if the pulse transitions happen faster than what the Arduino can process, your code will fail pretty badly. The only way around this would probably be to speed the Arduino up (ie, switching to 20 MHz - which has its own pitfalls and would mess with the timer as well), or to use a faster processor supported by the Arduino system (something other than the 168/328 running at 16 MHz).

With that said, I don't see how the ECM computer firing the injectors is going to need to be faster than the Arduino can handle (then again, I know little to nothing about ECM computers - maybe injector firing happens waaay quicker than I am imagining?).

Good luck, and I hope this helps!

:)

cr0sh thank you very much. the while function works PERFECTLY! I made a little program that read a square wave and write it througt two while functions. Works perfectly and more I saw that the error between both is less of 4us, 3.5 I measured on my "Star Trek" oscilloscope (!?!?) The complete program show me: input frequency 150 Hz ( quartz oscillator ) frequency given by Arduino: 150.26 0.17 % of error. As always a man with two clocks is a man that do not know which time is! I have another ( silly!!!) question: because micros is an integer every mathematical operation give me an integer. And this is not suitable for me. I saw that float val=3/2 give me 1 but if I make float 3.001/2 give 1.5 which is the right sistem to make this operation? Pietro PS: Here in Italy we have a lot of beer ready for you!

I saw that float val=3/2 give me 1 but if I make float 3.001/2 give 1.5 which is the right sistem to make this operation?

float val = 3.0/2.0; will also result in 1.5.

This will do all floating point arithmetic, too:

``````float pulses = 3.0;
float time = (float)millis();
float pulsesPerUnitTime = pulses/time;
``````