วันอาทิตย์ที่ 10 มีนาคม พ.ศ. 2562

มินิโปรเจค Arduino Calculator (แก้ไข)



 https://www.electronicshub.org/arduino-calculator/





















#include <LiquidCrystal.h>
#include <Keypad.h> 

const byte ROWS = 4; 
const byte COLS = 4; 

char keys[ROWS][COLS] = {

  {'7','8','9','D'},

  {'4','5','6','C'},

  {'1','2','3','B'},

  {'*','0','#','A'}

};

byte rowPins[ROWS] = { 0, 1, 2, 3 };
byte colPins[COLS] = { 4, 5, 6, 7 }; 

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 

const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; 
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

 long Num1,Num2,Number;
 char key,action;
 boolean result = false;

void setup() {
  lcd.begin(16, 2); 
  lcd.print("DIY Calculator"); 
  lcd.setCursor(0, 1);  
  lcd.print("-CircuitDigest"); 

   delay(2000); 
    lcd.clear(); 
}

void loop() {
  
key = kpd.getKey();

if (key!=NO_KEY)
DetectButtons();

if (result==true)
CalculateResult();

DisplayResult();   
}

void DetectButtons()
     lcd.clear(); 
    if (key=='*') 
    {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}
    
     if (key == '1') 
    {Serial.println ("Button 1"); 
    if (Number==0)
    Number=1;
    else
    Number = (Number*10) + 1; 
    }
    
     if (key == '4')
    {Serial.println ("Button 4"); 
    if (Number==0)
    Number=4;
    else
    Number = (Number*10) + 4; 
    }
    
     if (key == '7') 
    {Serial.println ("Button 7");
    if (Number==0)
    Number=7;
    else
    Number = (Number*10) + 7; 
    } 
  

    if (key == '0')
    {Serial.println ("Button 0"); 
    if (Number==0)
    Number=0;
    else
    Number = (Number*10) + 0; 
    }
    
     if (key == '2') 
    {Serial.println ("Button 2"); 
     if (Number==0)
    Number=2;
    else
    Number = (Number*10) + 2; 
    }
    
     if (key == '5')
    {Serial.println ("Button 5"); 
     if (Number==0)
    Number=5;
    else
    Number = (Number*10) + 5; 
    }
    
     if (key == '8')
    {Serial.println ("Button 8"); 
     if (Number==0)
    Number=8;
    else
    Number = (Number*10) + 8; 
    }   
  

    if (key == '#')
    {Serial.println ("Button Equal"); 
    Num2=Number;
    result = true;
    }
    
     if (key == '3')
    {Serial.println ("Button 3"); 
     if (Number==0)
    Number=3;
    else
    Number = (Number*10) + 3; 
    }
    
     if (key == '6')
    {Serial.println ("Button 6"); 
    if (Number==0)
    Number=6;
    else
    Number = (Number*10) + 6; 
    }
    
     if (key == '9')
    {Serial.println ("Button 9");
    if (Number==0)
    Number=9;
    else
    Number = (Number*10) + 9;
    }  

      if (key == 'A' || key == 'B' || key == 'C' || key == 'D')
  {
    Num1 = Number;    
    Number =0;
    if (key == 'A')
    {Serial.println ("Addition"); action = '+';}
     if (key == 'B')
    {Serial.println ("Subtraction"); action = '-'; }
     if (key == 'C')
    {Serial.println ("Multiplication"); action = '*';}
     if (key == 'D')
    {Serial.println ("Devesion"); action = '/';}  

    delay(100);
  }
  
}

void CalculateResult()
{
  if (action=='+')
    Number = Num1+Num2;

  if (action=='-')
    Number = Num1-Num2;

  if (action=='*')
    Number = Num1*Num2;

  if (action=='/')
    Number = Num1/Num2; 
}

void DisplayResult()
{
  lcd.setCursor(0, 0);   
  lcd.print(Num1); lcd.print(action); lcd.print(Num2); 
  
  if (result==true)
  {lcd.print(" ="); lcd.print(Number);} 
  
  lcd.setCursor(0, 1);  
  lcd.print(Number); 
}



วันพุธที่ 6 มีนาคม พ.ศ. 2562

มินิโปรเจค Arduino Calculator




     https://www.electronicshub.org/arduino-calculator/



#include <LiquidCrystal.h>
#include <Keypad.h> 

const byte ROWS = 4; 
const byte COLS = 4; 

char keys[ROWS][COLS] = {

  {'7','8','9','D'},

  {'4','5','6','C'},

  {'1','2','3','B'},

  {'*','0','#','A'}

};

byte rowPins[ROWS] = { 0, 1, 2, 3 };
byte colPins[COLS] = { 4, 5, 6, 7 }; 

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 

const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; 
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

 long Num1,Num2,Number;
 char key,action;
 boolean result = false;

void setup() {
  lcd.begin(16, 2); 
  lcd.print("DIY Calculator"); 
  lcd.setCursor(0, 1);  
  lcd.print("-CircuitDigest"); 

   delay(2000); 
    lcd.clear(); 
}

void loop() {
  
key = kpd.getKey();

if (key!=NO_KEY)
DetectButtons();

if (result==true)
CalculateResult();

DisplayResult();   
}

void DetectButtons()
     lcd.clear(); 
    if (key=='*') 
    {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}
    
     if (key == '1') 
    {Serial.println ("Button 1"); 
    if (Number==0)
    Number=1;
    else
    Number = (Number*10) + 1; 
    }
    
     if (key == '4')
    {Serial.println ("Button 4"); 
    if (Number==0)
    Number=4;
    else
    Number = (Number*10) + 4; 
    }
    
     if (key == '7') 
    {Serial.println ("Button 7");
    if (Number==0)
    Number=7;
    else
    Number = (Number*10) + 7; 
    } 
  

    if (key == '0')
    {Serial.println ("Button 0"); 
    if (Number==0)
    Number=0;
    else
    Number = (Number*10) + 0; 
    }
    
     if (key == '2') 
    {Serial.println ("Button 2"); 
     if (Number==0)
    Number=2;
    else
    Number = (Number*10) + 2; 
    }
    
     if (key == '5')
    {Serial.println ("Button 5"); 
     if (Number==0)
    Number=5;
    else
    Number = (Number*10) + 5; 
    }
    
     if (key == '8')
    {Serial.println ("Button 8"); 
     if (Number==0)
    Number=8;
    else
    Number = (Number*10) + 8; 
    }   
  

    if (key == '#')
    {Serial.println ("Button Equal"); 
    Num2=Number;
    result = true;
    }
    
     if (key == '3')
    {Serial.println ("Button 3"); 
     if (Number==0)
    Number=3;
    else
    Number = (Number*10) + 3; 
    }
    
     if (key == '6')
    {Serial.println ("Button 6"); 
    if (Number==0)
    Number=6;
    else
    Number = (Number*10) + 6; 
    }
    
     if (key == '9')
    {Serial.println ("Button 9");
    if (Number==0)
    Number=9;
    else
    Number = (Number*10) + 9;
    }  

      if (key == 'A' || key == 'B' || key == 'C' || key == 'D')
  {
    Num1 = Number;    
    Number =0;
    if (key == 'A')
    {Serial.println ("Addition"); action = '+';}
     if (key == 'B')
    {Serial.println ("Subtraction"); action = '-'; }
     if (key == 'C')
    {Serial.println ("Multiplication"); action = '*';}
     if (key == 'D')
    {Serial.println ("Devesion"); action = '/';}  

    delay(100);
  }
  
}

void CalculateResult()
{
  if (action=='+')
    Number = Num1+Num2;

  if (action=='-')
    Number = Num1-Num2;

  if (action=='*')
    Number = Num1*Num2;

  if (action=='/')
    Number = Num1/Num2; 
}

void DisplayResult()
{
  lcd.setCursor(0, 0);   
  lcd.print(Num1); lcd.print(action); lcd.print(Num2); 
  
  if (result==true)
  {lcd.print(" ="); lcd.print(Number);} 
  
  lcd.setCursor(0, 1);  
  lcd.print(Number); 
}





วันอาทิตย์ที่ 24 กุมภาพันธ์ พ.ศ. 2562

จำลองการเขียนโปรแกรม Arduino ด้วย https://circuits.io/

จำลอง ต่อวงจร Arduino
จำลองการเขียน Code circuits.io

การ Flash ESP8266 ด้วยบอร์ด Arduino Uno R3



ดัดแปลงเซอร์โว SG90 ให้หมุน 360 องศา

12-1
12

Arduino and Motor Control


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;
}

เซ็นเซอร์น้ำฝน ความชื้น Raindrop Module


ค้ดตัวอย่างการอ่านค่า Rain drop sensor 

Arduino เครื่องตรวจจับควัน MQ-2


โปรแกรม Code Arduino
int LedRED = 12;
int LEDgreen = 11;
int smokeA0 = A0;
int sensorThres = 400;  //ตั้ง ค่าความหนาของตวัน แก๊ส ก๊าซที่ 400
void 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);
}

"Embedded Space"


/////////////////////////////////////////////////////////////////////
// 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;
  }
}

////////////////////////////////////////////////////////////////////////

การใช้งาน 7 Segment กับ Arduino ตอนที่ 1 7 Segment หลักเดียว


void setup() {
  DDRD = 0xFF; // เซ็ตให้ขา 0 - 7 เป็นเอาต์พุต
  PORTD = 0xF9; // เซ็ตให้แสดงค่า 0xF9 ออกไป (แสดงเลข 1 ใน 7 Segment Comon Anode)
}

void loop() { }
หรือ
void setup() {
  DDRD = 0xFF; // เซ็ตให้ขา 0 - 7 เป็นเอาต์พุต
  PORTD = 0x06; // เซ็ตให้แสดงค่า 0xF9 ออกไป (แสดงเลข 1 ใน 7 Segment Comon Cathode)
}

void loop() { }
เมื่ออัพโหลดโค้ดลงไป ก็จะแสดงเลข 1 ออกทาง 7 Segment
หากต้องการแสดงเลขอื่นๆก็จะต้องทำแบบเดียวกัน คือ การพิจาณาแถบที่ต้องติด นำมาวางเป็นตาราง คิดเป็นเลขฐาน 2 แลัวแปลงเป็นเลขฐาน 16
แต่เพื่อความง่าย ผมได้คิดมาให้แล้ว และเอาลงอาร์เรย์ดังนี้
int num[] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F };
เวลาจะใช้งานก็ใช้คำสั่งเพียง PORTD = num[1]; ก็จะแสดงเลข 1 ออกมา และหากใช้เป็น PORTD = num[9]; ก็จะแสดงเลข 9 ออกมา ซึ่งโค้ดเต็มๆ คือ
int num[] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F };
void setup() {
  DDRD = 0xFF;
  PORTD = num[9];
}

Sensor DS18B20 Arduino วัดอุณหภูมิในน้ำ


#include <OneWire.h
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 //กำหนดขา
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);
void setup(void) { 
 Serial.begin(9600); 
 Serial.println("Dallas Temperature IC Control Library");  Serial.println("by 9arduino.com"); 
 sensors.begin(); 
void loop(void) {
 Serial.print(" Requesting temperatures..."); 
 sensors.requestTemperatures(); //อ่านข้อมูลจาก library
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0)); // แสดงค่า อูณหภูมิ sensor 0
delay(1000); 

Arduino กับ Nodejs


ผลการค้นหารูปภาพสำหรับ Arduino กับ Nodejs
#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); 

เซ็นเซอร์วัดความชื้นในดิน Soil Moisture Sensor


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);

การใช้งาน 3-axis Accelerometer Module (MMA7361) กับ Arduino

วงจร MMA7361

//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 Calculator (แก้ไข)

 https://www.electronicshub.org/arduino-calculator/ #include <LiquidCrystal.h> ...