20/07/2567

ปีการศึกษา 2567 Arduino IDE สื่อการเรียนการสอน"สำหรับพัฒนาผู้เรียน" ทางการศึกษา

 


 AnalogInput ตัวต้านทานปรับค่าได้ ปรับความสว่างหลอดไฟ LED ทางจอ LCD


#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

float input_voltage = 0.0;
int outputValue = 0;
int LED_Pin = 9;

void setup()
{
Serial.begin(9600);
lcd.begin();
}
void loop()
{
int analog_value = analogRead(A0);
outputValue = map(analog_value, 0, 1023, 0, 255);
input_voltage = (analog_value * 5.0) / 1024.0;
analogWrite(LED_Pin, outputValue);

if (input_voltage < 0.1)
{
input_voltage=0.0;
}
Serial.print(“v= “);
Serial.println(input_voltage);
lcd.setCursor(0, 0);
lcd.print(“DIGITAL VOLTMETER”);
lcd.setCursor(2, 1);
lcd.print(“Voltage = “);
lcd.print(input_voltage);
lcd.setCursor(16, 1);
lcd.print(” V”);
delay(2);
}


 Arduino กับ Buzzer  passive ปุ่มกดเสียง 


#define BUZZER_PIN 2
#define SWTICH1 8
#define SWTICH2 9
#define SWTICH3 10
void setup() {
  Serial.begin(9600);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(SWTICH1, INPUT_PULLUP);
  pinMode(SWTICH2, INPUT_PULLUP);
  pinMode(SWTICH3, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(SWTICH1) == LOW) {
    delay(20);
    tone(BUZZER_PIN, 300);  
    while (digitalRead(SWTICH1) == LOW) {
      delay(20);
    }
  }
  if (digitalRead(SWTICH2) == LOW) {
    delay(20);
    tone(BUZZER_PIN, 900); 
    while (digitalRead(SWTICH2) == LOW) {
      delay(20);
    }
  }
  if (digitalRead(SWTICH3) == LOW) {
    delay(20);
    tone(BUZZER_PIN, 1500);   
    while (digitalRead(SWTICH3) == LOW) {
      delay(20);
    }
  }
  noTone(BUZZER_PIN);
}

 IoT ESP8266 NodeMCU เปิด ปิด ไฟ ผ่านเว็บ ( LED) 2 ดวง

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "NodeMCU"; // Enter SSID here
const char* password = "1xxxxxx8"; 

IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

ESP8266WebServer server(80);

uint8_t LED1pin = D7;
bool LED1status = LOW;

uint8_t LED2pin = D6;
bool LED2status = LOW;

void setup() {
Serial.begin(115200);
pinMode(LED1pin, OUTPUT);
pinMode(LED2pin, OUTPUT);

WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);

server.on("/", handle_OnConnect);
server.on("/led1on", handle_led1on);
server.on("/led1off", handle_led1off);
server.on("/led2on", handle_led2on);
server.on("/led2off", handle_led2off);
server.onNotFound(handle_NotFound);

server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
if(LED1status)
{digitalWrite(LED1pin, HIGH);}
else
{digitalWrite(LED1pin, LOW);}

if(LED2status)
{digitalWrite(LED2pin, HIGH);}
else
{digitalWrite(LED2pin, LOW);}
}

void handle_OnConnect() {
LED1status = LOW;
LED2status = LOW;
Serial.println("GPIO7 Status: OFF | GPIO6 Status: OFF");
server.send(200, "text/html", SendHTML(LED1status,LED2status));
}

void handle_led1on() {
LED1status = HIGH;
Serial.println("GPIO7 Status: ON");
server.send(200, "text/html", SendHTML(true,LED2status));
}

void handle_led1off() {
LED1status = LOW;
Serial.println("GPIO7 Status: OFF");
server.send(200, "text/html", SendHTML(false,LED2status));
}

void handle_led2on() {
LED2status = HIGH;
Serial.println("GPIO6 Status: ON");
server.send(200, "text/html", SendHTML(LED1status,true));
}

void handle_led2off() {
LED2status = LOW;
Serial.println("GPIO6 Status: OFF");
server.send(200, "text/html", SendHTML(LED1status,false));
}

void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}

String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +=".button-on {background-color: #1abc9c;}\n";
ptr +=".button-on:active {background-color: #16a085;}\n";
ptr +=".button-off {background-color: #34495e;}\n";
ptr +=".button-off:active {background-color: #2c3e50;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>ESP8266 Web Server</h1>\n";
ptr +="<h3>Using Access Point(AP) Mode</h3>\n";

if(led1stat)
{ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";}
else
{ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";}

if(led2stat)
{ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";}
else
{ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";}

ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}

 NodeMCU ESP8266 ควบคุม เปิดปิดไฟ LED 4 ดวง ผ่าน wifi web server 

#include <ESP8266WiFi.h>

const char* ssid = "my_tengtechno";
const char* password = "0xxxxxxxxx";

int ledPin1 = D1; // ขา D1
int ledPin2 = D2; // ขา D2
int ledPin3 = D3; // ขา D3
int ledPin4 = D4; // ขา D4

int ch1 = 0;
int ch2 = 0;
int ch3 = 0;
int ch4 = 0;

WiFiServer server(80);

void setup() {
  Serial.begin(9600);
  delay(100);

  // ประกาศขา เป็น Output
  pinMode(ledPin1, OUTPUT);  
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);

  // เริ่มต้น ให้ Logic ตำแหน่งขาเป็น LOW
  digitalWrite(ledPin1, HIGH);   
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  // Match the request

    // รับ Link ช่อง 1 

  if (request.indexOf("/LED1=ON") != -1) {
    digitalWrite(ledPin1, LOW);
    ch1 = 1;
  }
  if (request.indexOf("/LED1=OFF") != -1) {
    digitalWrite(ledPin1, HIGH);
    ch1 = 0;
  }

  // รับ Link ช่อง 2
  if (request.indexOf("/LED2=ON") != -1) {
    digitalWrite(ledPin2, LOW);
    Serial.println("OK2");
    ch2 = 1;
  }

  if (request.indexOf("/LED2=OFF") != -1) {
    digitalWrite(ledPin2, HIGH);
    Serial.println("OK2");
    ch2 = 0;
  }

    // รับ Link ช่อง 3
  if (request.indexOf("/LED3=ON") != -1) {
    digitalWrite(ledPin3, LOW);
    Serial.println("OK3");
    ch3 = 1;
  }

  if (request.indexOf("/LED3=OFF") != -1) {
    digitalWrite(ledPin3, HIGH);
    Serial.println("OK3");
    ch3 = 0;
  }

    // รับ Link ช่อง 4
  if (request.indexOf("/LED4=ON") != -1) {
    digitalWrite(ledPin4, LOW);
    ch4 = 1;
  }

  if (request.indexOf("/LED4=OFF") != -1) {
    digitalWrite(ledPin4, HIGH);
    ch4 = 0;
  }

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); // do not forget this one
  client.println("");
  client.println("");

  client.print("Led pin D1 : ");
  if (ch1 == 1) {
    client.print("On<br>");
  } else {
    client.print("Off<br>");
  }
  client.print("Led pin D2 : ");
  if (ch2 == 1) {
    client.print("On<br>");
  } else {
    client.print("Off<br>");
  }
  client.print("Led pin D3 : ");
  if (ch3 == 1) {
    client.print("On<br>");
  } else {
    client.print("Off<br>");
  }
  client.print("Led pin D4 : ");
  if (ch4 == 1) {
    client.print("On<br>");
  } else {
    client.print("Off<br>");
  }

  client.println("");

client.println("CH1 <a href=\"/LED1=ON\"\"><button> On </button></a><a href=\"/LED1=OFF\"\"><button> OFF </button></a><br>");  

client.println("CH2 <a href=\"/LED2=ON\"\"><button> On </button></a><a href=\"/LED2=OFF\"\"><button> OFF </button></a><br>"); 

client.println("CH3 <a href=\"/LED3=ON\"\"><button> On </button></a><a href=\"/LED3=OFF\"\"><button> OFF </button></a><br>");

client.println("CH4 <a href=\"/LED4=ON\"\"><button> On </button></a><a href=\"/LED4=OFF\"\"><button> OFF </button></a><br>");

  client.println("");

  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
}

Analog InPut ตัวต้านทานปรับค่าได้ ปรับความสว่างหลอดไฟ LED

int ledPin = 3;
int analogPin = 5; 
int val = 0;
void setup() {
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
}

void loop() {
  val = analogRead(analogPin);  
  Serial.print("val = "); 
  Serial.println(val); 
  analogWrite(ledPin, val / 4); 
  delay(5);
}


ไม่มีความคิดเห็น:

แสดงความคิดเห็น

ครูเต้ง เทคโนฯ