Hallo Zusammen,
ich habe einen kleinen Sketch zusammengeschrieben. Leider bleibt er nach ein paar Sekunden hängen und ich weis nicht wie ich bei der Fehlersuche vorgehen soll. Ich vermute das, dass OLED Display den Absturz verursacht aber das ist leider nur eine Vermutung.
Wie kann ich hier Systematisch nach einem Fehler suchen? Bin über jede Hilfe dankbar...
/* Encoder Library - TwoKnobs Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these pin numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder knobLeft(2, 3);
Encoder knobRight(18, 19);
// avoid using pins with LEDs attached
//DISPLAY
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//Konstanten
const int LED_Schuss=9; //Überwachung Eingangsignal vom UR
const int Knopf=22; //Eingang Schusszähler
const int Reset=24; //Reset Schusszähler
const int Dauerlauf=26; //Dauerlauf
const int LED_Schuss_OUT=10; //Ausgang Schusszähler
const int FB=11; //Förderband
//Variable
int counter; //Zähler
int KnopfJETZT = 0; //Knopf aktuell
int KnopfALT = 0; //Knopf Zustand
void setup() {
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(22, INPUT); //Eingang Schusszähler
pinMode(24, INPUT); //Reset Schusszähler
pinMode(26, INPUT); //Dauerlauf
Serial.begin(9600);
Serial.println("TwoKnobs Encoder Test:");
u8g2.begin(); //DISPLAY
}
long positionLeft = -999;
long positionRight = -999;
void loop() {
float newLeft;
int newRight;
newLeft = knobLeft.read();
newRight = knobRight.read();
if (newLeft != positionLeft || newRight != positionRight) {
Serial.print("Left = ");
Serial.print(newLeft);
Serial.print(", Right = ");
Serial.print(newRight);
Serial.println();
positionLeft = newLeft;
positionRight = newRight;
}
// if a character is sent from the serial monitor,
// reset both back to zero.
if (Serial.available()) {
Serial.read();
Serial.println("Reset both knobs to zero");
knobLeft.write(0);
knobRight.write(0);
}
//Tacktung
KnopfJETZT = digitalRead(Knopf);
if (KnopfJETZT != KnopfALT) {
if (KnopfJETZT == HIGH) {
counter++;
Serial.println(counter);
digitalWrite(LED_Schuss, HIGH);
digitalWrite(LED_Schuss_OUT, HIGH);
} else {
digitalWrite(LED_Schuss, LOW);
digitalWrite(LED_Schuss_OUT, LOW);
}
delay(50);
}
KnopfALT = KnopfJETZT;
if(counter == newRight){
digitalWrite(FB, HIGH);
Serial.println("Foerderband laeuft");
delay(newLeft*100);
digitalWrite(FB, LOW);
Serial.println("Foerderband stopp");
counter = 0;
}
if(digitalRead(24) == HIGH){
counter=0;
}
//DISPLAY
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_7x14B_mf); // choose a suitable font
u8g2.drawStr(0,16,"Schuss"); // write something to the internal memory
u8g2.setCursor(90,16); // write something to the internal memory
u8g2.print(counter); // write something to the internal memory
u8g2.drawStr(0,38,"Taktung"); // write something to the internal memory
u8g2.setCursor(90, 38);
u8g2.print(newRight); // write something to the internal memory
u8g2.drawStr(0,62,"Laufzeit sec"); // write something to the internal memory
u8g2.setCursor(90, 62);
u8g2.print(newLeft/10,1); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(1);
}