U8glib loop not working

Hi

I aim to count pulses and print the values on a i2c oled screen.

After using the loop i must have for the U8glib lib, the rest of the loop doesent seem to work.

What is wrong with my code?

#include "U8glib.h"


U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI --------------FUNKAR------------------

// read RPM

volatile int rpmcount1 = 0;//see http://arduino.cc/en/Reference/Volatile
volatile int rpmcount2 = 0;//see http://arduino.cc/en/Reference/Volatile
volatile int rpm1 = 0;
volatile int rpm2 = 0;
unsigned long lastmillis1 = 0;
unsigned long lastmillis2 = 0;


void setup(){
 Serial.begin(9600); 
 pinMode(2, INPUT_PULLUP);
 pinMode(3, INPUT_PULLUP);
 attachInterrupt(0, rpm_fan1, HIGH);//interrupt cero (0) is on pin two(2).
 attachInterrupt(1, rpm_fan2, HIGH);//interrupt cero (1) is on pin two(3).
}




void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  //u8g.drawStr( 0, 22, "Hello World! TEST");
  //u8g.drawStr( 0, 22, rpm1);
  u8g.setPrintPos(0,22);
  u8g.print(rpm1);
  u8g.setPrintPos(0,44);
  u8g.print(rpm2);
}



void loop(){

{
  // picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  
  // rebuild the picture after some delay
  delay(50);
}

 
 if (millis() - lastmillis1 == 1000){  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/

 detachInterrupt(0);    //Disable interrupt when calculating
 
 rpm1 = rpmcount1 * 60;  /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
 
 Serial.print("RPM =\t"); //print the word "RPM" and tab.
 Serial.print(rpm1); // print the rpm value.
 Serial.print("\t Hz=\t"); //print the word "Hz".
 Serial.println(rpmcount1); /*print revolutions per second or Hz. And print new line or enter.*/
 
 rpmcount1 = 0; // Restart the RPM counter
 lastmillis1 = millis(); // Uptade lasmillis
 attachInterrupt(0, rpm_fan1, FALLING); //enable interrupt

  }



 if (millis() - lastmillis2 == 1000){  //Uptade every one second, this will be equal to reading frecuency (Hz).
 
 detachInterrupt(1);    //Disable interrupt when calculating
 
 rpm2 = rpmcount2 * 60;  // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.
 
 Serial.print("RPM2 =\t"); //print the word "RPM" and tab.
 Serial.print(rpm2); // print the rpm value.
 Serial.print("\t Hz2=\t"); //print the word "Hz".
 Serial.println(rpmcount2); //print revolutions per second or Hz. And print new line or enter.
 
 rpmcount2 = 0; // Restart the RPM counter
 lastmillis2 = millis(); // Uptade lasmillis
 attachInterrupt(1, rpm_fan2, FALLING); //enable interrupt
  }
  }

void rpm_fan1(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount1++;
   
}

void rpm_fan2(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount2++;
}

/*
void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 22, "RPM Monitor void");
        u8g.setPrintPos(0,54);
    u8g.print(rpm1);
        u8g.print(rpmcount1);

}
*/

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we’re trying to read your code. Do not post double spaced code.

When your code requires a library that’s not included with the Arduino IDE please always post a link (using the chain link icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

When i use U8glib loop, its like the interupt function in the main loop doesent work?

If i remove the u8glib loop the interupt function works fine and i can read the rpm of the fan. But as soon as i use the u8glib loop the hole program just stand still. What im i doing wrong?

You could try the newer u8g2. Yet in principle, I would expect, that the interrupts are created as usual.

Oliver

u8g2库太占内存了我认为使用u8x8方式比较好。