12/04/2568

Theme 5 วงจรพัฒนาโครงงานจำลอง IMU Simulation ( Inertial Measurement Unit )

 

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้ คำสั่งเขียน 

#include <Servo.h>
Servo myservo1; //create servo object
Servo myservo2; //create servo object
Servo myservo3; //create servo object
//EMA stands for (Exponential moving average)
//Used pins
//accelerometer
const int x_pin = A0;
const int y_pin = A1;
const int z_pin = A2;
//Potentiometer
const int gx_pin = A3;
const int gy_pin = A4;
const int gz_pin = A5;
//Servo
const int servoPin1 = 3;
const int servoPin2 = 5;
const int servoPin3 = 11;
//variables
//initialization of sensor variable, equibalaent 
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
//EMA 
float EMA_a = 0.02; //initialization of EMA alpha
int EMA_S1 = 0; //initialization of EMA S1
int EMA_S1_map = 0; //initialization of variable for servo control
int EMA_S2 = 0; //initialization of EMA S2
int EMA_S2_map = 0; //initialization of variable for servo control
int EMA_S3 = 0; //initialization of EMA S3
int EMA_S3_map = 0; //initialization of variable for servo control
int angleX = 0;
int angleY = 0;
int angleZ = 0;
// time
unsigned long startTime = 0;
unsigned long currentTime = 0;
unsigned long elapsedTime = 0;

// Volts per G-Force
const float sensitivity = 0.206;
void setup() {
  Serial.begin(115200);
  analogReference(EXTERNAL);
  EMA_S1 = analogRead(x_pin); //set EMA S for t = 1
  EMA_S2 = analogRead(y_pin); //set EMA S for t = 1
  EMA_S3 = analogRead(z_pin); //set EMA S for t = 1
  myservo1.attach(servoPin1);
  myservo2.attach(servoPin2);
  myservo3.attach(servoPin3);
}
void loop() {
  currentTime = millis();
  elapsedTime = currentTime - startTime;
  // Read pins and convert to G
  // accelerometer rate
  float ax = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);
  float ay = (analogRead(y_pin) - 512) * 3.3 / (sensitivity * 1023);
  float az = (analogRead(z_pin) - 512) * 3.3 / (sensitivity * 1023);
  // gyroscope rate
  float gx = (analogRead(gx_pin) - 512) * 3.3 / (sensitivity * 1023);
  float gy = (analogRead(gy_pin) - 512) * 3.3 / (sensitivity * 1023);
  float gz = (analogRead(gz_pin) - 512) * 3.3 / (sensitivity * 1023);
  // serva 1
  move_motor(x_pin, EMA_S1, EMA_S1_map, sensorValue1, gx);
  angleX = complementaryFilter(angleX, gx, elapsedTime, EMA_S1);
  myservo1.write(angleX);//send the latest value to the servo
//  myservo1.write(EMA_S1_map);//send the latest value to the servo
  // serva 2
  move_motor(y_pin, EMA_S2, EMA_S2_map, sensorValue2, gy);
  angleY = complementaryFilter(angleY, gy, elapsedTime, EMA_S2);
  myservo2.write(angleX);//send the latest value to the servo
  //myservo2.write(EMA_S2_map);//send the latest value to the servo
  // serva 3
  move_motor(z_pin, EMA_S3, EMA_S3_map, sensorValue3, gz);
  angleZ = complementaryFilter(angleZ,gz, elapsedTime, EMA_S3);
  myservo3.write(angleZ);//send the latest value to the servo
 // myservo3.write(EMA_S3_map);//send the latest value to the servo
  delay(100);
  startTime = currentTime;
}
void move_motor(int input_pin, int &EMA_S, int &EMA_S_map, int &sensorValue, float gyroVar){
  sensorValue = analogRead(input_pin); //read the sensor
  EMA_S = (EMA_a*sensorValue) + ((1-EMA_a)*EMA_S);//run the EMA
  Serial.print("pin ");
  Serial.print(input_pin);
  Serial.print(": ");
  Serial.print(sensorValue);//the first variable for plotting
  Serial.print(",");//seperator
  Serial.println(EMA_S);//the second variable for plotting including line break 
  EMA_S_map = map(EMA_S, 0, 1023, 0, 180);//map ADC values to servo values (0-180)
};
//Complementary Filter, similar to exponential, this example is for an accelerometer
// The 0.98 and 0.02 are the same as the weights in the exponential filter 
//angle = 0.98 *(angle+gyro*dt) + 0.02*acc

float complementaryFilter(float angle, float gyro, float dt, float acc)
{
float totalAngle = .5 * (angle + gyro * dt) + .5 * acc;
    return totalAngle;
}

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้ วงจร 




Theme 5 วงจรพัฒนาโครงงาน Sensor acelerómetro y ultrasónico

 

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้  คำสั่งเขียน 

#include <LiquidCrystal.h>
LiquidCrystal lcd (2,3,4,5,6,7);
const int x_pin = A0;
float sinVal;
int toneVal;
long cm = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  return (pulseIn(echoPin, HIGH)*0.01723);
}
void setup() {
  analogReference(EXTERNAL);
  lcd.begin(16,2);
  pinMode(8,OUTPUT);
}
void loop() {
  cm =readUltrasonicDistance(9, 10);
  lcd.setCursor(0,1);
  lcd.print(nivelAgua(cm));
  float x=0;
  x = (analogRead(x_pin)); 
  if(x != 511){
    tone(8,2000);
  lcdAlerta();
  }
  else{
  lcdNormal();
  noTone(8);
  }
  delay(1000);
  lcd.clear();
}
void lcdNormal(){
  //lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("ESTADO: NORMAL");
}
void lcdAlerta(){
  //lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("ESTADO: ALTO");  
}
String nivelAgua(long distancia){
  String texto="";
  if (distancia > 100) {
      texto="N. AGUA: ESTABLE";
  }

  if (distancia <= 100 && distancia > 10) {
      texto="N. AGUA: RIESGO";
  }

  if (distancia <= 10) {
    texto="N. AGUA: PELIGRO";
  }
  return texto;
}

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้  วงจร 





Theme 5 วงจรพัฒนาโครงงานจำลองประยุกต์ใช้งาน Simulated Accelerometer

 

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้  คำสั่งเขียน 


int xReading = 0;
int yReading = 0;
int zReading = 0;
void setup()
{
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}
void loop()
{
  xReading = analogRead(A0);
  yReading = analogRead(A1);
  zReading = analogRead(A2);
  Serial.print("X: ");
  Serial.print(xReading);
  Serial.print("\tY: ");
  Serial.print(yReading);
  Serial.print("\tZ: ");
  Serial.println(zReading);
  if (xReading >= 800) {
    digitalWrite(2, HIGH);
  } else {
    digitalWrite(2, LOW);
  }
  if (yReading >= 800) {
    digitalWrite(3, HIGH);
  } else {
    digitalWrite(3, LOW);
  }
  if (zReading >= 800) {
    digitalWrite(4, HIGH);
  } else {
    digitalWrite(4, LOW);
  }
  delay(200); // Wait for 200 millisecond(s)
  // ADD YOUR CODE FOR TILT ANGLE CALCULATIONS HERE   
}


📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้ วงจร 






Theme 5 วงจรพัฒนาโครงงานจำลองระบบ Water quality monitoring system

 

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้ คำสั่งเขียน 


#include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int redPin = 9;     // Red LED pin
int greenPin = 10;  // Green LED pin
int bluePin = 13;   // Blue LED pin
int motor = 7;
int pos = 0;
void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(6, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(motor, INPUT);
  // Set the pinMode for sensor pins in the setup() function.
  pinMode(14, INPUT);
  pinMode(A1, INPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
}
void loop() {
  // Read temperature
  int temp = analogRead(14);
  float voltage = temp * 5.0 / 1024.0;
  float temperatureC = (voltage - 0.5) * 100.0;
  // Read EC
  int elConductivity = analogRead(A1);
  float ec = map(elConductivity, 0, 1023, 0, 2000);
  // Read TDS
  int Turb1 = analogRead(2);
  float Turb = map(Turb1, 0, 1023, 0, 10);
  // Read pH
  int PH = analogRead(3);
  float PH1 = map(PH, 0, 1023, 0, 14);
 
  //sensor
  int sensor_data = analogRead(A4);
  int motor_data = analogRead(A5);
  /*Serial.print("Temperature: ");
  Serial.println(temperatureC);
  Serial.print("EC: ");
  Serial.println(ec);
  Serial.print("Turbidity: ");
  Serial.println(Turb);
  Serial.print("pH: ");
  Serial.println(PH1);*/
  Serial.println(motor_data);
  Serial.println(sensor_data);
  int dis = digitalRead(6);
  if (ec >= 30 && ec <= 1500 && Turb <= 5 && (PH1 >= 6.5 && PH1 <= 8.5) && (temperatureC >= 4 && temperatureC <= 25)) {
    setColor(0, 255, 0);  // Green color (safe)
  }    
  else {
    setColor(255, 0, 0);  // Red color (dangerous)
    delay(1000);
  }
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("TEMP: ");
  lcd.print(temperatureC);
  delay(1000);
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("EC: ");
  lcd.print(ec);
  delay(1000);
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("Turbidity: ");
  lcd.print(Turb);
  delay(1000);
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("pH: ");
  lcd.print(PH1);
  delay(1000);
  lcd.clear();
}
void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้ วงจร 





Theme 5 วงจรพัฒนาโครงงานจำลองระบบ เซนเซอร์วัดระยะ แบบ 3 ขา

 

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้  คำสั่งเขียน 

int cm = 0; //ให้ค่าเป็น 0
float temp; //ประกาศtempไว้เก็บค่า
long readUltrasonicDistance(int triggerPin, int echoPin) //ประกาศใช้triggerPin และ echoPin
{
  pinMode(triggerPin, OUTPUT); 
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  return pulseIn(echoPin, HIGH);
}
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  cm = 0.01723 * readUltrasonicDistance(12,12); //อ่านค่าจากขา 12 แล้วนำมาคูณ 0.01723 แล้วเก็บไว้ใน dist
  Serial.print(" CM : ");//พิมพ์คำว่า CM :
  Serial.print( cm); //แสดงค่าของcm
  Serial.println();
  delay(1000); 
}

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้ วงจร 




Theme 4 วงจรพัฒนาโครงงานประยุกต์ใช้งานการวัดระดับน้ำ

 

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้  คำสั่งเขียน 


#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(32, 16, 2);  // Adjust the I2C address if necessary
const int trigPin = 9;  // Digital pin connected to the Trig pin of the ultrasonic sensor
const int echoPin = 10; // Digital pin connected to the Echo pin of the ultrasonic sensor
const int buzzerPin = 11; // Digital pin connected to a buzzer
const int ledPin = 13; // Digital pin connected to an LED
const float waterLevelThreshold = 10.0; // Adjust this value based on your tank setup
void setup() {
  Serial.begin(9600);  // Open serial communication for debugging
  lcd.begin(16, 2);  // Initialize LCD display
  lcd.init();
  lcd.setCursor(0, 0);
  lcd.backlight();
  lcd.display();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
}
void loop() {
  // Send ultrasonic signal
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Receive the echo and calculate distance
  int duration = pulseIn(echoPin, HIGH);
  float distance = duration * 0.034 / 2;
  // Display distance on LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Distance: ");
  lcd.print(distance);
  lcd.print(" cm");
  // Check water level and trigger actions
  if (distance < waterLevelThreshold) {
    lcd.setCursor(0, 1);
    lcd.print("Low water level");
    digitalWrite(buzzerPin, HIGH);
    digitalWrite(ledPin, HIGH);
  } else {
    lcd.setCursor(0, 1);
    lcd.print("Normal water level");
    digitalWrite(buzzerPin, LOW);
    digitalWrite(ledPin, LOW);
  }
delay(1000);
}



📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้ วงจร 





Theme 4 วงจรพัฒนาโครงงานการจำลอง Servo motor ควบคุมด้วยการกดติ ปล่อยดับ

 

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้  คำสั่งเขียน 

#include <Servo.h>
Servo myservo;
const int buttonPin = 2;
int button1 = 0;
int click = 0;
int modeServo = 0;
void setup(){
pinMode (buttonPin, INPUT);
myservo.attach(9);
}
void loop(){
button1 = digitalRead(buttonPin);
if (button1 == LOW) {
click = 0;
myservo.write(0);
}
else if(button1 == HIGH){
click++;
if (click == 1) {
modeServo++; } }
if (modeServo == 1){
myservo.write(90);
}
delay(50);

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้ วงจร