

void setup(){
pwmSetup();
}
void loop(){
int Duty = 128;//type in the voltage you want. Refer to guide on limiting comonents
OCR2A = Duty;
}
void pwmSetup(){//just run once at setup
pinMode(11, OUTPUT); //OCR2A
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20); //phase correct pwm 31250hz
TCCR2B = _BV(CS20);//change this as datasheet says to mainly get different pwm frequencies
OCR2A = 0;
OCR2B = 0;
}
int LedRED = 12;
int LEDgreen = 11;
int smokeA0 = A0;int sensorThres = 400; //ตั้ง ค่าความหนาของตวัน แก๊ส ก๊าซที่ 400void setup() {
Serial.begin(9600);
pinMode(LedRED, OUTPUT);
pinMode(LEDgreen, OUTPUT);
pinMode(smokeA0, INPUT);
}void loop() {
int analogSensor = analogRead(smokeA0);
Serial.print("Pin A0: ");
Serial.println(analogSensor);if (analogSensor > sensorThres) {
digitalWrite(LedRED, HIGH);
digitalWrite(LEDgreen, LOW);
}else {
digitalWrite(LedRED, LOW);
digitalWrite(LEDgreen, HIGH);
}
delay(100);
}
///////////////////////////////////////////////////////////////////// // Author: RSP @ ESL (Embedded System Lab), KMUTNB // Date: 2014-APR-03 // Target Board: Arduino (ATmega328P, 5V, 16MHz) // Arduino IDE: version 1.0.5 // Description: // This Arduino sketch demonstrates how to implement a simple human // motion detection system based on the AMN34112 sensor module. // When the motion is detected, the system triggers an alarm // (LED blinking and buzzer sound). //////////////////////////////////////////////////////////////////////// /* Board INT0 INT1 INT2 INT3 INT4 INT5 Uno, Ethernet 2 3 Mega2560 2 3 21 20 19 18 Leonardo 3 2 0 1 7 */ #define ALARM_PIN (3) #define BLINK_TIME_MS (1000) #define WAIT_TIME_MS (3000) typedef enum {ST_CHECK=0, ST_ALARM, ST_WAIT} state_type; state_type state; uint32_t ts; volatile uint16_t event_count = 0; void eint_isr() { event_count++; } void setup() { pinMode( ALARM_PIN, OUTPUT ); digitalWrite( ALARM_PIN, LOW ); delay( 3000 ); ts = millis(); event_count = 0; state = ST_CHECK; attachInterrupt( 0, eint_isr, RISING ); // use INT0 } void loop() { static uint8_t led_state = 0; switch (state) { case ST_CHECK: if ( event_count > 1 ) { state = ST_ALARM; // change state to ST_ALARM ts = millis(); // update timestamp } break; case ST_ALARM: if ( millis() - ts >= BLINK_TIME_MS ) { state = ST_WAIT; // change state to ST_WAIT ts = millis(); led_state = 0; digitalWrite( ALARM_PIN, led_state ); // turn off alarm } else { led_state ^= 1; digitalWrite( ALARM_PIN, led_state ); // toggle alarm delay(50); } break; case ST_WAIT: if ( millis() - ts >= WAIT_TIME_MS ) { event_count = 0; // reset event counter state = ST_CHECK; } break; default: state = ST_CHECK; break; } } ////////////////////////////////////////////////////////////////////////
#include <DHT.h> //เรียกใช้ library DHT.h #define DHTPIN 14 // เลือกขา GPIO14 #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE, 15); void setup() { Serial.begin(9600); dht.begin(); //เริ่มเรียกการใช้งาน dht จาก library } void sendJsonString(float temperature, float humidity, String dataStatus, String reason){ /* { "data" : { "temperature" : 0.0, // 0,NaN "humidity" : 0.0 }, "status" : "OK", //OK, Error "reason" : "" } */ String s = ""; s += String("{"); s += String("\"data\" : {"); s += String("\"temperature\" : " + String(temperature) + "," ); s += String("\"humidity\" : " + String(humidity) ); s += String("} ," ); s += String("\"status\" : \"" + dataStatus + "\"" + "," ); s += String("\"reason\" : \"" + error + "\""); s += String("}"); Serial.println(s); } void loop() { // Reading temperature or humidity takes about 250 milliseconds! delayMicroseconds(250); // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) delay(2000); // Set up data float h = dht.readHumidity(); float t = dht.readTemperature(); // check if returns are valid, if they are NaN (not a number) then something went wrong! float temperature = t; float humidity = h; String dataStatus = "OK"; String reason = ""; if (isnan(t) || isnan(h)) { temperature = 0; humidity = 0; dataStatus = "Error"; reason = "Failed to read from DHT"; } // Send data sendJsonString(temperature, humidity, dataStatus, reason);
Example By ArduinoAll | |
GND - GND | |
VCC - 3.3 หรือ 5V | |
AOUT - A0 | |
*/ | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
int val = analogRead(A0); | |
Serial.println(val); | |
delay(500); |
//Programa : Conectando Acelerômetro 3 Eixos MMA7361 no Arduino
//Autor : FILIPEFLOP
#include <AcceleroMMA7361.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
AcceleroMMA7361 accelero;
int
x;
int
y;
int
z;
void
setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
accelero.begin(13, 12, 11, 10, A0, A1, A2);
accelero.setARefVoltage(3.3);
//sets the AREF voltage to 3.3V
accelero.setSensitivity(LOW);
//sets the sensitivity to +/-6G
accelero.calibrate();
lcd.setCursor(0,0);
lcd.print(
"X: "
);
lcd.setCursor(8,0);
lcd.print(
"Y: "
);
lcd.setCursor(0,1);
lcd.print(
"Z: "
);
}
void
loop()
{
x = accelero.getXRaw();
lcd.setCursor(3,0);
lcd.print(x);
y = accelero.getYRaw();
lcd.setCursor(11,0);
lcd.print(y);
z = accelero.getZRaw();
lcd.setCursor(3,1);
lcd.print(z);
Serial.print(
"nx: "
);
Serial.print(x);
Serial.print(
"ty: "
);
Serial.print(y);
Serial.print(
"tz: "
);
Serial.print(z);
delay(500);
}
มาลองทำวงจร Arduino ง่ายๆแต่ได้ความรู้ คือวงจรที่มีทั้ง Input และ Output แบบ Digital โดยเราจะทำวงจรที่ใช้ Input Pin ของ Arduino มารับค่าการกดปุ่ม แล้วนำค่านั้นไปควบคุม LED อีกทีโดยใช้ Output Pin โดยเราจะใช้วงจรแบบ Pull-Down เพื่อกำหนดให้ LED ติดเมื่อกดปุ่มเท่านั้น | |||||||||||||
|
/* DigitalClockRTC1307 By IOXhop.com */ #include <Wire.h> #include <Time.h> #include <DS1307RTC.h> #include <TM1637Display.h> // Module connection pins (Digital Pins) #define CLK 2 #define DIO 3 unsigned long previousMillis; bool showdot = false; String SerialGET = ""; TM1637Display display(CLK, DIO); void setup() { Serial.begin(9600); } void loop() { tmElements_t tm; if (!RTC.read(tm)) { tm.Hour = 0; tm.Minute = 0; tm.Second = 0; RTC.write(tm); } display.write(tm.Hour/10, 0); display.write(tm.Hour%10, 1); display.write(tm.Minute/10, 2); display.write(tm.Minute%10, 3); // Dot blink unsigned long currentMillis = millis(); if (currentMillis-previousMillis>500){ previousMillis = currentMillis; display.dotShow(showdot=!showdot); } while (Serial.available()) { char c = Serial.read(); if (c == '\n') { int Hour, Min, Sec; if (sscanf(SerialGET.c_str(), "%d:%d:%d", &Hour, &Min, &Sec) == 3) { tm.Hour = Hour; tm.Minute = Min; tm.Second = Sec; RTC.write(tm); Serial.print("Set to "); Serial.print(Hour); Serial.print(":"); Serial.print(Min); Serial.print(":"); Serial.print(Sec); Serial.println(); }else{ Serial.println("Invalid format, Please enter Hour:Min:Sec"); } SerialGET = ""; } else SerialGET += c; } }
https://www.electronicshub.org/arduino-calculator/ #include <LiquidCrystal.h> ...