Created
June 15, 2018 23:03
-
-
Save rac2030/fee4510c11ce4f7c0157bc6221aa3425 to your computer and use it in GitHub Desktop.
EIn unfinished project, wir haben ein Vorderrad eines Fahrades mit Nabnmotor über einen Voltage Divider an einen Analog pin gemacht und entsprechend die Geschwindigkeit anhand des rad umfanges und der umdrehungen pro minute (aus den Pulsen des Nabenmotors) die Geschwindigkeit auf einem OLED ausgegeben.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <EEPROM.h> | |
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/ | |
int addr = 0; | |
#define OLED_RESET 4 // not used / nicht genutzt bei diesem Display | |
Adafruit_SSD1306 display(OLED_RESET); | |
#include <elapsedMillis.h> | |
elapsedMillis timeElapsed; | |
elapsedMillis timeElapsedUmdrehung; | |
int pulseProUmdrehung = 13; | |
int pulseCounter = 0; | |
unsigned int timeSinceLastUmdrehung = 0; | |
int speedIn = 2; | |
double rotationsPerMeter = 1.596; | |
unsigned int timeSinceLastPulse = 0; | |
int lastPulseState = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
pinMode(speedIn, INPUT); | |
// initialize with the I2C addr 0x3C / mit I2C-Adresse 0x3c initialisieren | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); | |
Serial.println("Nöggi's speedometer v.0.0.0.1 alpha"); | |
Serial.println("ms/r\trps\tm/s\tkm/h"); | |
display.clearDisplay(); | |
// set text color / Textfarbe setzen | |
display.setTextColor(WHITE); | |
// set text size / Textgroesse setzen | |
display.setTextSize(4); | |
// set text cursor position / Textstartposition einstellen | |
display.setCursor(1, 0); | |
display.println("Noah"); | |
display.display(); | |
timeElapsed = 0; | |
timeElapsedUmdrehung = 0; | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if (timeElapsed > 300) { | |
display.clearDisplay(); | |
// set text color / Textfarbe setzen | |
display.setTextColor(WHITE); | |
// set text size / Textgroesse setzen | |
display.setTextSize(4); | |
// set text cursor position / Textstartposition einstellen | |
display.setCursor(1, 0); | |
display.println(0.0); | |
display.display(); | |
} | |
int pulse = digitalRead(speedIn); | |
if (pulse > lastPulseState) { | |
timeSinceLastPulse = timeElapsed; | |
//Serial.println(timeSinceLastPulse); | |
pulseCounter = pulseCounter + 1; | |
timeElapsed = 0; | |
} | |
lastPulseState = pulse; | |
//Serial.println(pulse); | |
if (pulseCounter >= pulseProUmdrehung) { | |
pulseCounter = 0; | |
timeSinceLastUmdrehung = timeElapsedUmdrehung; | |
timeElapsedUmdrehung = 0; | |
double rotationPerSecond = ((double)1000) / timeSinceLastUmdrehung; | |
double speedInMeterPerSecond = rotationPerSecond * rotationsPerMeter; | |
double speedInKilometerPerHour = speedInMeterPerSecond * 3.6; | |
Serial.print(timeSinceLastUmdrehung); | |
Serial.print("\t"); | |
Serial.print(rotationPerSecond); | |
Serial.print("\t"); | |
Serial.print(speedInMeterPerSecond); | |
Serial.print("\t"); | |
Serial.println(speedInKilometerPerHour); | |
display.clearDisplay(); | |
// set text color / Textfarbe setzen | |
display.setTextColor(WHITE); | |
// set text size / Textgroesse setzen | |
display.setTextSize(4); | |
// set text cursor position / Textstartposition einstellen | |
display.setCursor(1, 0); | |
display.println(speedInKilometerPerHour); | |
display.display(); | |
} | |
delay(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment