Hallo martinamandel
Nimm am Besten einen Compiler in der Version < 2.0.
Der Compiler verdaut ohne Warnungen und Fehlermeldungen dieses Programm:
// MoBa-Speed for ARDUINO
// powered by N-Modellbahn.de - Joerg Kiessling - 2018
// Arduino Sketch for model speed measurement
// for Arduino Nano.V3, UNO.V3, MEGA
// 2 IR Line Trackers with LM393 komperator output
// Display OLED 128 x 64px
// Note1 for power supply: connect OLED display VDD to Arduino 3.3V !
// Note1 for OLED adresse: SSD1306_SWITCHCAPVCC, 0x3C is for third party products! Use adresse 0x3D for original Adafruit products!
// Digital sensor input Left and Right
#define SENSOR_L 7
#define SENSOR_R 6
////// DECLARATIONS
#include <SPI.h> // Not needed when using I2C
#include <Wire.h> //I2C library
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // OLED DISPLAY INIT
//Distance from sensor Left to sensor Right - your distance (mm)
const double distance = 150.0;
//Your model scale 1/xx
int scale = 160;
//Waiting time for train, to move out of measurement array (ms)
int waitingtime = 12000;
//Name of variables for time and speed measuring
long int deltatime, starttime;
//N-Modellbahn.de Logo - MoBa Speed for Arduino
const unsigned char PROGMEM logo [] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x21, 0x81, 0x80, 0x03, 0xF0, 0x00, 0x00,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xE0, 0x00, 0x00, 0x00, 0x00,
0x7F, 0xE0, 0x00, 0x07, 0xFF, 0xFE, 0x7F, 0xFF, 0xE3, 0xE7, 0xE3, 0xE0, 0x00, 0x00, 0x00, 0x00,
0x7F, 0xE0, 0x00, 0x03, 0xFF, 0xFE, 0x7F, 0xFF, 0xE3, 0x0E, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0xC0, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xE3, 0x0C, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0xC0, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xE3, 0x0C, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0x80, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xE3, 0x0E, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0x80, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xE3, 0x07, 0xE3, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xE3, 0x03, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7E, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7E, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7E, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
0x7C, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xE0, 0x70, 0x00, 0x06, 0x00, 0x30, 0x00, 0x00,
0x7C, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xE0, 0x70, 0x00, 0x06, 0x00, 0x30, 0x00, 0x00,
0x78, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xE0, 0xD8, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
0x78, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xE0, 0xD8, 0x7C, 0x7E, 0x63, 0x33, 0xF0, 0x78,
0x70, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xE1, 0x8C, 0x7C, 0xFE, 0x63, 0x33, 0xF8, 0xFC,
0x70, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xE1, 0x8C, 0x61, 0xC6, 0x63, 0x33, 0x19, 0xCE,
0x60, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xE1, 0xFC, 0x61, 0x86, 0x63, 0x33, 0x19, 0x86,
0x60, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xE3, 0xFE, 0x61, 0x86, 0x63, 0x33, 0x19, 0x86,
0x40, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xE3, 0x06, 0x61, 0xC6, 0x63, 0x33, 0x19, 0xCE,
0x40, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xE6, 0x03, 0x60, 0xFE, 0x7F, 0x33, 0x18, 0xFC,
0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xE6, 0x03, 0x60, 0x7E, 0x3F, 0x33, 0x18, 0x78,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
//Sensor Input
pinMode(SENSOR_L, INPUT_PULLUP);
pinMode(SENSOR_R, INPUT_PULLUP);
//Interface baud rate
Serial.begin(9600);
//Display
initialize_display();
}
void loop() {
//waiting for signal, sensor Left
if (digitalRead(SENSOR_L)== LOW)
{
//Sensor Left was activated, start time = system time (ms)
starttime = millis();
//waiting for signal sensor Right
while (digitalRead(SENSOR_R)== HIGH) {}
//Measured time in ms
deltatime = (millis() - starttime);
refresh_display();
}
//waiting for signal, sensor Right
if (digitalRead(SENSOR_R)== LOW)
{
//Sensor Right was activated, start time = system time (ms)
starttime = millis();
//waiting for signal sensor Left
while (digitalRead(SENSOR_L)== HIGH) {}
//Measured time in ms
deltatime = (millis() - starttime);
refresh_display();
}
} //End of loop
//Output on display
void refresh_display() {
//Output on serial monitor
Serial.println("");
Serial.println("Deltatime:");
Serial.print(deltatime);
Serial.println(" ms");
Serial.println("---------");
Serial.println("Speed:");
Serial.print(distance/deltatime);
Serial.println(" m/s (real)");
Serial.print(scale*distance/deltatime*3.6, 0);
Serial.print(" km/h (scale 1/");
Serial.print(scale);
Serial.println(")");
//Output on OLED
display.clearDisplay();
display.setTextSize(5);
display.setCursor(0,28);
display.print(scale*distance/deltatime*3.6, 0); //calculating speed in km/h
display.setTextSize(1);
display.setCursor(91,56);
display.print("km/h");
display.setCursor(98,28);
display.print("1/");
display.setCursor(110,28);
display.print(scale);
display.drawFastHLine(0, 18, 128, WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.print(deltatime*0.001);
display.setTextSize(1);
display.setCursor(50,7);
display.print("s");
display.drawFastVLine(63, 0, 16, WHITE);
display.setTextSize(2);
display.setCursor(71,00);
display.print(distance/deltatime);
display.setTextSize(1);
display.setCursor(121,-2);
display.print("m");
display.drawFastHLine(121, 7, 5, WHITE);
display.setCursor(121,8);
display.print("s");
display.display();
delay(waitingtime);
display.clearDisplay();
display.setTextSize(4);
display.setCursor(5,28);
display.print("READY");
display.drawFastHLine(0, 18, 128, WHITE);
display.setTextSize(2);
display.setCursor(37,0);
display.print("1/");
display.setCursor(60,0);
display.print(scale);
display.display();
Serial.println("");
Serial.println("<<< READY >>>");
}
void initialize_display() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize OLED display (use adresse 0x3D for original Adafruit product)
display.display();
display.clearDisplay();
display.drawBitmap(0, 0, logo, 128, 64, WHITE);
display.display();
display.setTextColor(INVERSE);
delay(2500);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,20);
display.println("powered by");
display.println("");
display.print("www.N-Modellbahn.de");
display.display();
delay(2500);
display.clearDisplay();
display.setTextSize(4);
display.setCursor(5,28);
display.print("READY");
display.drawFastHLine(0, 18, 128, WHITE);
display.setTextSize(2);
display.setCursor(37,0);
display.print("1/");
display.setCursor(60,0);
display.print(scale);
display.display();
Serial.println("<<< READY >>>");
}