Timer Putaran Slot Car Terpicu Laser dengan Layar GLCD – Pengukuran Waktu Akurat untuk Lintasan RC
Komponen dan Perlengkapan
- Arduino Mega 2560 × 1
- Laser KY 008 × 1
Aplikasi dan Layanan Online
Tentang Proyek Ini
Intro
Saya membeli pasangan modul laser KY008 dan sensor yang cocok, dan saya tahu saya harus membuat proyek keren dengan itu. Jadi saya mengambil pasangan itu, dan menciptakan gerbang pengukuran waktu listrik untuk lintasan mobil slot yang sudah ada di meja untuk proyek mobil slot buatan diri saya.
Hardware
3 komponen utama adalah modul laser, sensor penerima laser dan layar GLCD.
Saya sangat suka menggunakan GLCD, Anda dapat menemukan informasi tentang pin-out di sini: https://playground.arduino.cc/Code/GLCDks0108/
Dan Anda dapat mengunduh perpustakaan dari sini:
https://bitbucket.org/bperrybap/openglcd
Saya membuat konektor yang bagus dan mudah untuk itu, seperti yang ditunjukkan di gambar di bawah ini.
Modul laser KY008 memiliki 3 pin, -, + dan S, saya hanya memendekkan baik S dan + ke 5V. Jika Anda tidak ingin laser menyala terus-menerus, hubungkan pin S ke pin digital pada Arduino. ketika Anda mengaturnya tinggi laser akan menyala.
Sensor juga memiliki 3 pin, GND, VCC dan SIGNALE. hubungkan ke daya dan pin input pada Arduino.
Tantangan terkeras dalam pembuatan ini adalah untuk menyelaraskan semuanya.
Anda dapat menonton hasil akhir dalam klip ini.
Kode
File tanpa judulArduino
/* * Time keeping digital gate * Code by: Tal Ofer * talofer99@hotmail.com */ #include "openGLCD.h" // laps info unsigned long currentRunStartMillis; unsigned long lastRunInMillis; unsigned long bestRunInMillis; int currentLap; unsigned long savedMillis; gText t1; // will define runtime later gText t2; // will define runtime later gText t3; // will define runtime later // global for display int sec_val, milli_val; // laser gate const int gateSensorPin = 2; // the number of the gate sensor pin int gateSensorState; // the current reading from the sensor int lastgateSensorState = LOW; // the previous reading from sensor unsigned long lastDebounceTime = 0; // the last time the sensor pin was toggled int debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { // pin mode pinMode(gateSensorPin, INPUT); delay(50); // to late the sensor and laser work, so we wont get the lap triggered. // start GLCD GLCD.Init(NON_INVERTED); // define areas t1.DefineArea(textAreaTOP, lcdnums14x24); t2.DefineArea(0, GLCD.CenterY, 8, 2, fixednums7x15); t3.DefineArea(GLCD.CenterX, GLCD.CenterY, 8, 2, fixednums7x15); t3.SetFontColor(WHITE); // set font color t3.ClearArea(); // print text GLCD.SelectFont(System5x7); GLCD.CursorTo(1, 4); GLCD.print("LAST"); GLCD.CursorTo(11, 4); GLCD.print("BEST"); // reset params currentRunStartMillis = 0; lastRunInMillis = 0; bestRunInMillis = 0; currentLap = 0; } void loop() { // read the state of the laser sensor: int reading = digitalRead(gateSensorPin); // If the switch changed, due to noise or pressing: if (reading != lastgateSensorState) { // reset the debouncing timer lastDebounceTime = millis(); } //end if // if passes the debounce time if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != gateSensorState) { gateSensorState = reading; // If we went low, this mean the beam was broken if (gateSensorState == LOW) { // save the millis so all the math on it will be done with the same value. savedMillis = millis(); // if its not the first lap if (currentLap > 0) { // save the last run lastRunInMillis = savedMillis - currentRunStartMillis; // if last run is faster then best run if (lastRunInMillis < bestRunInMillis || bestRunInMillis == 0) { //save as best bestRunInMillis = lastRunInMillis; } //end if } //end if //reset the current currentRunStartMillis = savedMillis; // move lap counter currentLap++; } //end if } //enf if } //end if // save the reading. Next time through the loop, it'll be the lastgateSensorState: lastgateSensorState = reading; // print Laps t1.CursorTo(0, 0); // set in location t1.Printf(F("%02d"), currentLap); // save current milis savedMillis = millis(); // if we start the first lap if (currentLap > 0) { calcResultFromMillis(savedMillis - currentRunStartMillis, &sec_val, &milli_val); } else { calcResultFromMillis(0, &sec_val, &milli_val); } //end if // CURRENT RUN t1.CursorTo(3, 0); // column & row is relative to text area t1.Printf(F("%02d.%03d"), sec_val, milli_val); // LAST RUN calcResultFromMillis(lastRunInMillis, &sec_val, &milli_val); t2.CursorTo(1, 1); // column & row is relative to text area t2.Printf(F("%02d.%03d"), sec_val, milli_val); // BEST RUN calcResultFromMillis(bestRunInMillis, &sec_val, &milli_val); t3.CursorTo(1, 1); t3.Printf(F("%02d.%03d"), sec_val, milli_val); } //wnd loop // calculate millis into 2 values, seconeds and millis for display void calcResultFromMillis(unsigned long value, int *sec_val, int *milli_val) { *sec_val = int(value / 1000); *milli_val = value - *sec_val * 1000; }