ESP8266 и дисплей OLED-SSD1306 программирование в Arduino IDE
- Войдите на сайт для отправки комментариев
Здравствуйте Уважаемые форумчане
Я совсем начинающий работу с Ардуино ИДЕ. Поэтому прошу сильно не ругать. Столкнулся с проблемой поведения дисплея при работе программы "Счетчик Гейгера".
В чем суть вопроса? Раньше эта программа (Счетчик Гейгера) нормально работала у меня на платах Arduino Pro Mini, и не было никаких вопросов. Теперь Я решил перейти на плату ESP826612E. И вот тет столкнулся с проблемой. Конечно в Arduino IDE я ввел соответствующие платы в менеджер плат из: http://arduino.esp8266.com/stable/package_esp8266com_index.json
В программе конечно изменил некоторые ноги. Программа компилируется и заливается нормально без ошибок. Но вот при подаче питания на плату дисплей OLED-SSD1306 128х32 ведет себя очень странно. Вначале на дисплее просто куча точек, потом ползет полоса примерно 8 пикселей и открывается начальный экран. Полоса проходит полностью одну строку, потом начинается вторая строка, изображение частично появляется, и потом где-то на середине пробег строки останавливается и ВСЕ. Начинается странное периодическое обновление экрана с периодом 2-3 секунды. И так продолается все время пока не выключишь питание.
Вот помещаю текст программы:
/*
===========================
File........... Geiger sensor counter
Purpose........ Geiger sensor
Author......... Igor Tylman
E-mail......... igortylman@gmail.com
Started........ 8/30/2020
Finished....... -/--/----
Updated........ -/--/----
===================================================
Notes
===================================================
Updates
*/
// Libraries
//=================================================
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneButton.h>
#include <elapsedMillis.h>
elapsedMillis timeElapsed;
#define OLED_RESET 12
Adafruit_SSD1306 display(OLED_RESET);
//=========================================
// Constants
//=======================================
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16 // do not change this. Error in video
#define LOGO16_GLCD_WIDTH 16 // do not change this. Error in video
//================================================
// Variables
//================================================
//Counter variables:
int pic = 0;
volatile long int counter = 0;
int flag = 0;
int flag2 = 0;
int flag3 = 0;
int timeflag = 0;
int startflag = 0;
int mikroR = 0;
int mikroS = 1;
//Time related:
unsigned long interval = 30;
//Strings:
String result;
String impulses;
//===================================
// Pin Declarations
//===================================
//OLED related:
//SDA --> 12 /k displeju, nawszaja A4
//SCL --> 14 /k displeju, nawszaja A5
//InputSignal related:
#define InputSignal 13 /schetnyj signal (3)
//Button related:
#define Button 2 /knopka w originale (2)
OneButton btn = OneButton(
Button, // Input pin for the button
true, // Button is active LOW
true // Enable internal pull-up resistor
);
//================================
// Functions
//================================
void counting (){
if (timeflag == 0 && startflag == 1){
counter ++;
}
}
void Text(String text, int x, int y,int size, boolean d) {
display.setTextSize(size);
display.setTextColor(WHITE);
display.setCursor(x,y);
display.println(text);
if(d){
display.display();
}
}
void oneclick(){
if (pic >= 10 && pic <= 13){
pic ++;
}
if (pic >= 20 && pic <= 21){
pic ++;
}
if (pic == 0){
timeElapsed = 0;
counter = 0;
timeflag = 0;
startflag = 1;
}
if (pic > 13 && pic < 20){
pic = 10;
}
if (pic > 21){
pic = 20;
}
}
void doubleclick(){
}
void longPressStop(){
}
void longPressStart(){
if (pic == 0 && flag2 == 0){
pic = pic + 10;
}
if (flag2 == 1){
pic = 20;
flag2 = 0;
}
if (flag3 == 1){
pic = 0;
flag3 = 0;
}
if (pic == 0){
timeElapsed = 0;
counter = 0;
timeflag = 0;
startflag = 1;
}
}
//================================
// Initialization
//=================================
void setup() {
//Input signal related:
pinMode(InputSignal, INPUT_PULLUP);
//Button related:
pinMode(Button, INPUT_PULLUP);
btn.attachClick(oneclick);
btn.attachDoubleClick(doubleclick);
btn.attachLongPressStop(longPressStop);
btn.attachLongPressStart(longPressStart);
//Others:
Serial.begin(9600);
//OLED:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
display.display();
// Clear the buffer.
display.clearDisplay();
//Interupptions:
attachInterrupt(digitalPinToInterrupt(3), counting, RISING); //Interrupt
}
//===============================================================================
// Main
//===============================================================================
void loop(){
btn.tick();
int wynikR = counter * 46 / interval;
float wynikS = float(wynikR / 100.0);
String seconds = String(interval);
String militime = String(interval - (timeElapsed) / 1000);
if (timeflag == 0){
String impunit = String(" imp.");
impulses = String(counter + impunit);
}
if (mikroS == 1 && timeflag == 1) {
String Sunit = String(" uSv/h");
result = String(wynikS + Sunit);
}
if (mikroR == 1 && timeflag ==1){
String Runit = String(" uR/h");
result = String(wynikR + Runit);
}
if (startflag == 0){
timeElapsed = 0;
}
if (startflag == 1){
if((timeElapsed >= interval*1000) && timeflag == 0){
timeflag = 1;
startflag = 0;
}
}
if (pic == 0 && timeflag == 0){
display.clearDisplay();
Text(militime, 82, 1, 1, false);
Text("(", 100, 1, 1, false);
Text(seconds, 105, 1, 1, false);
Text(")", 122, 1, 1, false);
Text(impulses, 0, 15, 2, false);
display.display();
}
if (pic == 0 && timeflag == 1){
display.clearDisplay();
Text(militime, 82, 1, 1, false);
Text("(", 100, 1, 1, false);
Text(seconds, 105, 1, 1, false);
Text(")", 122, 1, 1, false);
Text(result, 0, 15, 2, false);
display.display();
}
if (pic == 10){
display.clearDisplay();
Text(">30s", 0, 0, 2, false);
Text("60s", 0, 15, 2, false);
Text("120s", 65, 0, 2, false);
Text("300s", 65, 15, 2, false);
display.display();
interval = 30;
flag2 = 1;
}
if (pic == 11){
display.clearDisplay();
Text("30s", 0, 0, 2, false);
Text(">60s", 0, 15, 2, false);
Text("120s", 65, 0, 2, false);
Text("300s", 65, 15, 2, false);
display.display();
interval = 60;
flag2 = 1;
}
if (pic == 12){
display.clearDisplay();
Text("30s", 0, 0, 2, false);
Text("60s", 0, 15, 2, false);
Text(">120s", 65, 0, 2, false);
Text("300s", 65, 15, 2, false);
display.display();
interval = 120;
flag2 = 1;
}
if (pic == 13){
display.clearDisplay();
Text("30s", 0, 0, 2, false);
Text("60s", 0, 15, 2, false);
Text("120s", 65, 0, 2, false);
Text(">300s", 65, 15, 2, false);
display.display();
interval = 300;
flag2 = 1;
}
if (pic == 20){
display.clearDisplay();
Text(">uSv/h", 30, 0, 2, false);
Text("uR/h", 30, 15, 2, false);
display.display();
mikroR= 0;
mikroS= 1;
flag3 = 1;
}
if (pic == 21){
display.clearDisplay();
Text("uSv/h", 30, 0, 2, false);
Text(">uR/h", 30, 15, 2, false);
display.display();
mikroR= 1;
mikroS= 0;
flag3 = 1;
}
}
Может быть нужно было бы сменить библиотеку для дисплея OLED-SSD1306 128х32 на соответствующую плате ESP8266-12E ? Посоветуйте какую библиотеку применить.
Я бы вообще исключил бы начальный экран, и тогда может быть все бы заработало, но не знаю как это сделать. Где вообще в этой программе вывод начальной картинки ???
Очень прошу помощи.
Ножки и ручки в Adafruit посмотрите...
Проблему уже почти решил, но частично. Изменил объявление переменной "counter":
//================================
// Functions ISR
//================================
ICACHE_RAM_ATTR void counting () {
if (timeflag == 0 && startflag == 1) {
counter ++;
}
}
Счетчик уже почти нормально функционирует, переключаются меню выбора, включается режим счета, и т.п.
Но есть еще одна проблема. Количество подсчитанных импульсов высвечивается на дисплее не в числовом вид, а в виде знаков, символов, непонятных "Кустов".
Т.е. почему-то переменная "counter" не числовая а символьная. Почему это так у меня ???
Вот помещаю отредактированный текст программы:
/* =========================== File........... Geiger sensor counter Purpose........ Geiger sensor Author......... Igor Tylman E-mail......... igortylman@gmail.com Started........ 8/30/2020 Finished....... -/--/---- Updated........ -/--/---- =================================================== Notes =================================================== Updates */ // Libraries //================================================= #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <OneButton.h> #include <elapsedMillis.h> elapsedMillis timeElapsed; #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 32 //#define OLED_RESET 14 //Adafruit_SSD1306 display(OLED_RESET); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); //========================================= // Constants //======================================= #define NUMFLAKES 10 #define XPOS 0 #define YPOS 1 #define DELTAY 2 #define LOGO16_GLCD_HEIGHT 16 // do not change this. Error in video #define LOGO16_GLCD_WIDTH 16 // do not change this. Error in video //================================================ // Variables //================================================ //Counter variables: int pic = 0; volatile long int counter = 0; int flag = 0; int flag2 = 0; int flag3 = 0; int timeflag = 0; int startflag = 0; int mikroR = 0; int mikroS = 1; //Time related: unsigned long interval = 30; //Strings: String result; String impulses; //=================================== // Pin Declarations //=================================== //OLED related: //SDA --> A4 k displeju, nawszaja A4 //SCL --> A5 k displeju, nawszaja A5 //InputSignal related: #define InputSignal 14 //schetnyj signal (3) //Button related: #define Button 12 //knopka w originale (2) OneButton btn = OneButton( Button, // Input pin for the button true, // Button is active LOW true // Enable internal pull-up resistor ); //================================ // Functions ISR //================================ ICACHE_RAM_ATTR void counting () { if (timeflag == 0 && startflag == 1) { counter ++; } } //================================ // Functions //================================ //void counting (){ //if (timeflag == 0 && startflag == 1){ //counter ++; //} //} void Text(String text, int x, int y, int size, boolean d) { display.setTextSize(size); display.setTextColor(WHITE); display.setCursor(x,y); display.println(text); if(d){ display.display(); } } void oneclick(){ if (pic >= 10 && pic <= 13){ pic ++; } if (pic >= 20 && pic <= 21){ pic ++; } if (pic == 0){ timeElapsed = 0; counter = 0; timeflag = 0; startflag = 1; } if (pic > 13 && pic < 20){ pic = 10; } if (pic > 21){ pic = 20; } } void doubleclick(){ } void longPressStop(){ } void longPressStart(){ if (pic == 0 && flag2 == 0){ pic = pic + 10; } if (flag2 == 1){ pic = 20; flag2 = 0; } if (flag3 == 1){ pic = 0; flag3 = 0; } if (pic == 0){ timeElapsed = 0; counter = 0; timeflag = 0; startflag = 1; } } //================================ // Initialization //================================= void setup() { //Input signal related: pinMode(InputSignal, INPUT_PULLUP); //Button related: pinMode(Button, INPUT_PULLUP); btn.attachClick(oneclick); btn.attachDoubleClick(doubleclick); btn.attachLongPressStop(longPressStop); btn.attachLongPressStart(longPressStart); //Others: Serial.begin(9600); //OLED: display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) display.display(); delay(1000); // Clear the buffer. display.clearDisplay(); //Interupptions: pinMode(14, INPUT); attachInterrupt(digitalPinToInterrupt(14), counting, RISING); //Interrupt; } //=============================================================================== // Main //=============================================================================== void loop(){ btn.tick(); int wynikR = counter * 46 / interval; float wynikS = float(wynikR / 100.0); String seconds = String(interval); String militime = String(interval - (timeElapsed) / 1000); if (timeflag == 0){ String impunit = String(" imp."); impulses = String(counter + impunit); } if (mikroS == 1 && timeflag == 1) { String Sunit = String(" uSv/h"); result = String(wynikS + Sunit); } if (mikroR == 1 && timeflag ==1){ String Runit = String(" uR/h"); result = String(wynikR + Runit); } if (startflag == 0){ timeElapsed = 0; } if (startflag == 1){ if((timeElapsed >= interval*1000) && timeflag == 0){ timeflag = 1; startflag = 0; } } if (pic == 0 && timeflag == 0){ display.clearDisplay(); Text(militime, 82, 1, 1, false); Text("(", 100, 1, 1, false); Text(seconds, 105, 1, 1, false); Text(")", 122, 1, 1, false); Text(impulses, 0, 15, 2, false); display.display(); } if (pic == 0 && timeflag == 1){ display.clearDisplay(); Text(militime, 82, 1, 1, false); Text("(", 100, 1, 1, false); Text(seconds, 105, 1, 1, false); Text(")", 122, 1, 1, false); Text(result, 0, 15, 2, false); display.display(); } if (pic == 10){ display.clearDisplay(); Text(">30s", 0, 0, 2, false); Text("60s", 0, 15, 2, false); Text("120s", 65, 0, 2, false); Text("300s", 65, 15, 2, false); display.display(); interval = 30; flag2 = 1; } if (pic == 11){ display.clearDisplay(); Text("30s", 0, 0, 2, false); Text(">60s", 0, 15, 2, false); Text("120s", 65, 0, 2, false); Text("300s", 65, 15, 2, false); display.display(); interval = 60; flag2 = 1; } if (pic == 12){ display.clearDisplay(); Text("30s", 0, 0, 2, false); Text("60s", 0, 15, 2, false); Text(">120s", 65, 0, 2, false); Text("300s", 65, 15, 2, false); display.display(); interval = 120; flag2 = 1; } if (pic == 13){ display.clearDisplay(); Text("30s", 0, 0, 2, false); Text("60s", 0, 15, 2, false); Text("120s", 65, 0, 2, false); Text(">300s", 65, 15, 2, false); display.display(); interval = 300; flag2 = 1; } if (pic == 20){ display.clearDisplay(); Text(">uSv/h", 30, 0, 2, false); Text("uR/h", 30, 15, 2, false); display.display(); mikroR= 0; mikroS= 1; flag3 = 1; } if (pic == 21){ display.clearDisplay(); Text("uSv/h", 30, 0, 2, false); Text(">uR/h", 30, 15, 2, false); display.display(); mikroR= 1; mikroS= 0; flag3 = 1; } }ВСЕ!!! УРА!!! Проблема решена, сделал несколько изменений, программа работает идеально.
Тема закрыта.