// 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, 0x78 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 4Adafruit_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/xxint scale = 87;//Waiting time for train, to move out of measurement array (ms)int waitingtime = 12000;//Name of variables for time and speed measuringlong int deltatime, starttime;//N-Modellbahn.de Logo - MoBa Speed for Arduinoconst 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, 0x00,0x7F, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x21, 0xC1, 0x80, 0x03, 0xFC, 0x00, 0x00, 0x00,0x7F, 0xFF,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xFF, 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 displayvoid 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, 0x78); // 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 >>>"); }
Ik ben code
Trouwens, stond tijdens de "avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00" fout per ongeluk de Serial monitor open?Oplossing: Serial Monitor sluiten voor uploaden (een heerlijke instinker waar je lang naar kunt zoeken )
#include <Wire.h> void setup(){ Wire.begin(); Serial.begin(9600); while (!Serial); // Leonardo: wait for serial monitor Serial.println("\nI2C Scanner");} void loop(){ byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } else if (error==4) { Serial.print("Unknown error at address 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(5000); // wait 5 seconds for next scan}
"Blink" werkt, het ledje knippert vrolijk .
Bibliotheekbeheer, ""I2C scanner" gezocht, gevonden en geïnstalleerd. Wat nu?
Wat is de serial monitor? Hoe sluit je die?
scanning....12C device found at 0x3C !