Monday, August 24, 2020

millis() overflow? BlinkWithoutDelay question?

 Normal if we want to run some code in fixed interval, we will use the following pattern:

void loop() {
	// check if the fixed interval reached
	unsigned long currentMillis = millis();
	if (currentMillis - previousMillis >= interval) {
		//do something in fixed interval
		//...
		
		
	}
}

It's a general practice as show in Arduino BlinkWithoutDelay example.

Where millis() returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

So, what will happen if it overflow? Will it be a bug in the statement (currentMillis - previousMillis >= interval)?
The answer is NO.

Lets try the following example, run on Uno.
void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }     // wait for serial port to connect.

  unsigned long i =0;
  unsigned long j = i-1;
  unsigned long k = j+1000;
  
  Serial.print("i = ");
  Serial.println(i);
  Serial.print("j = ");
  Serial.println(j);
  Serial.print("k = ");
  Serial.println(k);

  Serial.print("(k > j) : ");
  Serial.println((k > j) ? "true" : "false");

  Serial.print("k-j : ");
  Serial.println(k-j);
  
  Serial.print("((k-j) >= 1000) : ");
  Serial.println(((k-j) >= 1000) ? "true" : "false");
   
}

void loop() {
  // put your main code here, to run repeatedly:

}

Sunday, August 16, 2020

NodeMCU (ESP8266) + 1.44" 128x128 TFT with ST7735 SPI driver (KMR1441_SPI V2)





This video show how to driver 1.44" 128x128 TFT with ST7735 SPI driver (KMR1441_SPI V2) with NodeMCU (ESP8266) using ssd1306 library. Using Arduino IDE.

- In Arduino IDE, open library manager, search ST7735, and install ssd1306 library.

- Open Example > ssd1306 > demos > st7735_demo

- Connect ESP8266 to LCD
ESP8266 LCD
===================
3V3 VCC
GND GND
D1 A0 (D/C)
D2 CS (CS)
RX RESET (RES)
D7 SDA (DIN)
D5 SCK (CLK)
LED (Open in my test)