Skip to content

Instantly share code, notes, and snippets.

@jensjeflensje
Last active August 31, 2024 13:30
Show Gist options
  • Save jensjeflensje/571256babd928201e4edb200e024d404 to your computer and use it in GitHub Desktop.
Save jensjeflensje/571256babd928201e4edb200e024d404 to your computer and use it in GitHub Desktop.
Code van mijn snelheidsmeter (https://www.youtube.com/watch?v=uQMSEfN3kMo)
import serial
import tkinter as tk
import threading
# seriele port verschilt per pc/arduino
ser = serial.Serial("/dev/tty.usbmodem21301", 9600, timeout=0.1)
root=tk.Tk()
value = tk.StringVar()
label = tk.Label(root,textvariable=value, font=("Arial", 96))
label.pack()
def read_input():
while True:
line = ser.readline()
if line:
value.set(f"{line.decode().strip()} km/u")
threading.Thread(target=read_input, daemon=True).start()
root.geometry("600x110")
root.mainloop()
#define LDR1 A0
#define LDR2 A1
const int MIN_LIGHT = 800;
const double DISTANCE = 0.05;
long firstMeasure = 0;
long secondMeasure = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
int ldr1 = analogRead(LDR1);
int ldr2 = analogRead(LDR2);
if (ldr1 < MIN_LIGHT && firstMeasure == 0) {
firstMeasure = millis();
}
if (ldr2 < MIN_LIGHT && secondMeasure == 0 && firstMeasure != 0) {
secondMeasure = millis();
// in seconds
double timePassed = (secondMeasure - firstMeasure) / 1000.0;
double speed = (DISTANCE / timePassed);
Serial.println(speed*3.6);
firstMeasure = 0;
secondMeasure = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment