Skip to content

Instantly share code, notes, and snippets.

View fxprime's full-sized avatar

ArduinoNa ขายอุปกรณ์ Arduino, เครื่องมือช่าง, raspberry pi, สอน ROS fxprime

View GitHub Profile
@fxprime
fxprime / TCS3200_esp32.ino
Created July 1, 2025 17:03
ตัวอย่างโค้ดสำหรับการใช้งาน TCS3200 Sensor บน ESP32 พร้อมการปรับเทียบเซ็นเซอร์เพื่อให้สามารถอ่านค่าสีได้อย่างแม่นยำ
/**
* ตัวอย่างโค้ดสำหรับการใช้งาน TCS3200 Sensor บน ESP32
* พร้อมการปรับเทียบเซ็นเซอร์เพื่อให้สามารถอ่านค่าสีได้อย่างแม่นยำ
*
* วิธีการต่อสาย:
* | ESP32 Pin | TCS3200 Pin |
* |------------|-------------|
* | 3.3v | Vcc |
* | GND | GND |
* | 3.3v | LED |
@fxprime
fxprime / sensor.h
Created July 1, 2025 13:41
tcs3200 ported to PTBOT need test
#define TCS34725_ADDRESS 0x29
#define TCS34725_COMMAND_BIT 0x80
SoftI2C SoftWire1(25, 26);
SoftI2C SoftWire2(27, 14);
// Assume S0 goes HIGH, S1 goes LOW
#define S2 26 /*Define S2 Pin Number of ESP32*/
#define S3 25 /*Define S3 Pin Number of ESP32*/
@fxprime
fxprime / testTCS3200_esp32.ino
Created July 1, 2025 13:23
testTCS3200 esp32
#include <Arduino.h>
#define S2 26 /*Define S2 Pin Number of ESP32*/
#define S3 25 /*Define S3 Pin Number of ESP32*/
#define sensorOut 14 /*Define Sensor Output Pin Number of ESP32*/
/*Define int variables*/
int Red = 0;
int Green = 0;
int Blue = 0;
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
unsigned long previousMillis = 0;
bool ledState = false;
const long interval = 1000;
// ปัญหา: ปุ่มกดไม่ไว
void loop() {
delay(5000); // หยุด 5 วินาที
if (digitalRead(buttonPin) == HIGH) {
// ปุ่มอาจถูกกดและปล่อยไปแล้วระหว่าง delay()
Serial.println("Button pressed"); // อาจไม่ทำงาน
}
}
// ตัวอย่างปัญหาของ delay()
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // หยุดทำงานทุกอย่าง 1 วินาที!
digitalWrite(LED_BUILTIN, LOW);
delay(1000); // หยุดอีก 1 วินาที
// ในขณะ delay() โปรแกรมไม่สามารถ:
// - อ่านเซนเซอร์
// - ตรวจสอบปุ่มกด
@fxprime
fxprime / battery.h
Created April 21, 2025 06:38
MEMO PROJECT (DROWSINESS DETECTOR DEVICE)
#ifndef __BATTERY_H__
#define __BATTERY_H__
#include <Arduino.h>
// ADC Configuration
#define BATTERY_PIN 34 // GPIO34 = ADC1_CH6
#define VOLTAGE_DIVIDER_RATIO 2.0 // 47kΩ / (47kΩ + 47kΩ) = 0.5 (so 1/0.5 = 2.0)
#define ADC_SAMPLES 20 // Must be odd number for true median
#define VOLTAGE_OFFSET 0.2463 // Offset for calibration
@fxprime
fxprime / ESP32_MAX485_DO_SENSOR.ino
Created March 30, 2025 05:26
ModuleMore RS-LDO-N01-1 (Modbus) Example code
/* -------------------------------------------------------------------------- */
/* ModuleMore RS-LDO-N01-1 (Modbus) Example code */
/* -------------------------------------------------------------------------- */
/* ------------------------------- วิธีการต่อ ------------------------------- */
/**
| ESP32 | 12V Adapter | MAX485 | DO RS485 |
|-------|-------------|--------|----------|
| GND | GND | GND | Black |
| 3.3V | | VCC | |
#include <Arduino.h>
/* Get all possible data from MPU6050
* Accelerometer values are given as multiple of the gravity [1g = 9.81 m/s²]
* Gyro values are given in deg/s
* Angles are given in degrees
* Note that X and Y are tilt angles and not pitch/roll.
*
* License: MIT
*/
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <TinyGPSPlus.h>
EspSoftwareSerial::UART gpsSerial;
TinyGPSPlus gps;
static void smartDelay(unsigned long ms);