putting it into just one method

Hey, I would like to get your adwise :) first question: Is it possible to put everything of this code into just one method, so that I can put it into an other sketch?

second question: is there a way to share variables inside a method with other methods not by using return(); or by putting it infront of void setup(){} ?

Thank you!!

 volatile byte rpmcount;
 unsigned int rpm;
 unsigned long timeold;

 void setup()
 {
   Serial.begin(9600);
   attachInterrupt(0, rpm_fun, RISING);

   rpmcount = 0;
   rpm = 0;
   timeold = 0;
 }

 void loop()
 {
   if (rpmcount >= 10) { 
     //Update RPM every 20 counts, increase this for better RPM resolution,
     //decrease for faster update
     rpm = 30*1000/(millis() - timeold)*rpmcount;
     timeold = millis();
     rpmcount = 0;
     Serial.print(rpm,DEC);
     Serial.println("  rpm"); //Prints " rpm" and a new line
   }
 }

 void rpm_fun()
 {
   rpmcount++;
   //Each rotation, this interrupt function is run twice
 }

No, in your case this isn't possible, because setup() and loop() are invoked independently and rpm_fun() is used as an interrupt handler, which is invoked in a different way again.

In case you want to join two sketches, you should be able to merge the setup() functions of both and the loop() functions too - as long as they don't contain any endless loops. The interrupt function and the global variables shouldn't interfere with other script, if the do, rename them.

As to passing variables to functions, there are ways like parameters passing by reference or building a class. But from the way you ask the questions, I guess you're trying to solve a problem but you're looking on the wrong end for the solutions. Could you please write a little more why you want to know this, perhaps we could provide you with the information you really need.

Korman

I am building an electric controlling system for an combustion "car" ( for the Shell ECO Marathon). I need to sense the rpm of a wheel and the motor. It will change gears depending on the speed and display everything on a LCD display. I just thought that there is a nice and pretty way to read out the rpm's. I am aware how to merge that little sketch into my code but thought that there might be a more compact and "clean" solution.

Best regards, Florian.

How about a structure like this:

volatile unsigned int rpm;
const int ticks = 10; // Number of pulses to use for rpm
const unsigned long lcdrefresh = 50; // Update LCD 20 times per second

void setup() {
      Serial.begin(9600);
      attachInterrupt(0, rpm_fun, RISING);
}

void loop() {
      static unsigned long timeold;
      unsigned long curtime = millis();
      
      if (curtime - timeold > lcdrefresh) {
            // Get current rpm time. Disable interrupts to prevent
            // modifiactions by the ISR while all bytes of long are copied.
            uint8_t oldSREG = SREG;
            cli();
            unsigned int rv = rpm;
            rpm =  0; // Reset for the case when motor is turned off
            SREG = oldSREG;

            if (rv) {
                  Serial.print(rv);
                  Serial.println("  rpm"); //Prints " rpm" and a new line
            }
            else {
                  Serial.println("Get the motor running, dummy!");
            }
      }
 }

void rpm_fun() {
      static int ticksleft = 0;
      static unsigned long rpmstart = 0;

    if (ticksleft) {
            // We're still measuring
            ticksleft--;
      }
      else {
            // Calculate time for rpm and reset counters
            unsigned long curtime = micros();
            rpm  = (1000000L  * 30 * ticks) / (curtime - rpmstart);             
            rpmstart = curtime;
            ticksleft = ticks - 1;
      }
}

With that structure, you just have one communication variable and your display function doesn't have to worry about whether data is ready. It just takes it whenever it needs it. I also used microseconds to measure the rpm so that you have the option of reducing the tick count. Just don't increase the count over 140 or you will overflow on the rpm calculation.

Korman

I see code like this a lot.

      static unsigned long timeold;

How does a static variable provide any advantage over a global variable?

How does a static variable provide any advantage over a global variable?

It has a reduced scope and doesn't interfere with other people using the same variable. I prefer them because they localise the use and make it easier to me bundle things that go together. When throwing away code less cruft is left over.

Korman

Thank you for your input