Есть данный код:
#include <Arduino.h>
#include <GyverOLED.h>
GyverOLED<SSD1306_128x64, OLED_BUFFER> oled;
#define echoPin 2
#define trigPin 3
unsigned long timerSleep = 0;
unsigned long timerMeasure = 0;
unsigned long timerDelay = 0;
boolean startMeasure = false;
int measure();
void setup() {
pinMode(trigPin,OUTPUT);
Serial.begin(9600);
oled.init();
oled.clear();
oled.setScale(4);
oled.update();
}
void loop() {
if(millis() - timerSleep >=150){
int measureValues = measure();
if(measureValues != -1){
String processedData;
if(measureValues >= 100){
float dividedValue = measureValues * 0.01;
processedData += dividedValue;
processedData += F(" m");
}
else{
processedData += measureValues;
processedData += F(" cm");
}
Serial.println(processedData);
oled.clear();
oled.setCursorXY(0,30);
oled.print(processedData);
oled.update();
startMeasure = false;
timerSleep = millis();
}
}
}
int measure(){
if(!startMeasure){
digitalWrite(trigPin,LOW);
startMeasure = true;
timerMeasure = micros();
return(-1);
}
if(micros() - timerMeasure >= 2 && micros() - timerMeasure <= 10 ){
digitalWrite(trigPin,HIGH);
return(-1);
}
if(micros() - timerMeasure >= 10){
digitalWrite(trigPin,LOW);
return(pulseIn(echoPin, HIGH)/58.3);
}
return(-1);
}
Приблизительно каждые 15-20 секунд плата перезагружается. Использую LGT8F328P