วันอาทิตย์ที่ 9 กันยายน พ.ศ. 2561

Arduino 14



Electronic Miter box! Control a Stepper Motor with a Keypad







/* Arduino Control Stepper with Keypad and LCD

Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!

*/

#include "AccelStepper.h" // AccelStepper Library
#include <Keypad.h>  // Keypad Library
#include "U8glib.h"  // U8glib for Nokia LCD

// Variables to hold entered number on Keypad
volatile int firstnumber=99;  // used to tell how many numbers were entered on keypad
volatile int secondnumber=99;
volatile int thirdnumber=99;

// Variables to hold Distance and CurrentPosition
int keyfullnumber=0;  // used to store the final calculated distance value
String currentposition = "";  // Used for display on Nokia LCD


// Keypad Setup
const byte ROWS = 4; // Four Rows
const byte COLS = 4; // Four Columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition


// U8glib Setup for Nokia LCD
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino pins connected to Nokia pins:
                                    // CLK=3, DIN=4, CE=6, DC=5, RST=7
                                    
                                    
// AccelStepper Setup
AccelStepper stepper(1, A0, A1);  // 1 = Easy Driver interface
                                  // Arduino A0 connected to STEP pin of Easy Driver
                                  // Arduino A1 connected to DIR pin of Easy Driver
                                  


void setup(void) {
  
  //  Light up the LCD backlight LEDS
  analogWrite(backlight_pin, 0);  // Set the Backlight intensity (0=Bright, 255=Dim)
    
  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(1500);  // Not to fast or you will have missed steps
  stepper.setAcceleration(400);  //  Same here
  
  // Draw starting screen on Nokia LCD
  u8g.firstPage();
  do {
  u8g.drawHLine(0, 15, 84);
  u8g.drawVLine(50, 16, 38);
  u8g.drawHLine(0, 35, 84); 
  u8g.setFont(u8g_font_profont11);
  u8g.drawStr(0, 10, "ENTER DISTANCE");
  u8g.drawStr(62, 29, "MM");
  u8g.drawStr(4, 46, "cur-pos");
  }
  while( u8g.nextPage() );
  
}


void loop(){
  
  char keypressed = keypad.getKey();  // Get value of keypad button if pressed
  if (keypressed != NO_KEY){  // If keypad button pressed check which key it was
    switch (keypressed) {
      
      case '1':
        checknumber(1);
      break;
        
      case '2':
        checknumber(2);
      break;

      case '3':
        checknumber(3);
      break;

      case '4':
        checknumber(4);
      break;

      case '5':
        checknumber(5);
      break;

      case '6':
        checknumber(6);
      break;

      case '7':
        checknumber(7);
      break;

      case '8':
        checknumber(8);
      break;

      case '9':
        checknumber(9);
      break;

      case '0':
        checknumber(0);
      break;

      case '*':
        deletenumber();
      break;

      case '#':
        calculatedistance();
      break;
    }
  }

}

void checknumber(int x){
  if (firstnumber == 99) { // Check if this is the first number entered
    firstnumber=x;
    String displayvalue = String(firstnumber);  //  Transform int to a string for display
    drawnokiascreen(displayvalue); // Redraw Nokia lcd
    
  } else {
    if (secondnumber == 99) {  // Check if it's the second number entered
      secondnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

    } else {  // It must be the 3rd number entered
      thirdnumber=x;
      String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
      drawnokiascreen(displayvalue);

    }
  }
}

 
void deletenumber() {  // Used to backspace entered numbers
  if (thirdnumber !=99) {
      String displayvalue = (String(firstnumber) + String(secondnumber));
      drawnokiascreen(displayvalue);

    thirdnumber=99;
  } 
  else {
    if (secondnumber !=99) {
      String displayvalue = String(firstnumber);
      drawnokiascreen(displayvalue);

      secondnumber=99;
   } 
   else {
     if (firstnumber !=99) {
       String displayvalue = "";
       drawnokiascreen(displayvalue);

       firstnumber=99;
      }
    }
  }
}
  
void calculatedistance() {  // Used to create a full number from entered numbers

    if (thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
      keyfullnumber=firstnumber;
      movestepper(keyfullnumber);
    }
    
    if (secondnumber != 99 && thirdnumber == 99) {
      keyfullnumber=(firstnumber*10)+secondnumber;
      movestepper(keyfullnumber);
    }
    
    if (thirdnumber != 99) {
      keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
      movestepper(keyfullnumber);
    }
    
    resetnumbers(); // Reset numbers to get ready for new entry
  } 


void movestepper(int z) {  //  Move the stepper

  int calculatedmove=((z*1600)/80);  //  Calculate number of steps needed in mm
  stepper.runToNewPosition(calculatedmove);
  currentposition = String(z);
  u8g.firstPage();
  do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84); 
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    u8g.setPrintPos(57,47);
    u8g.print(currentposition);       
  }
  while( u8g.nextPage() ); 
}
                
void resetnumbers() {  // Reset numbers for next entry
  firstnumber=99;
  secondnumber=99;
  thirdnumber=99;
} 
  

void drawnokiascreen(String y) {

    u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 38);
      u8g.drawHLine(0, 35, 84); 
      u8g.setFont(u8g_font_profont11);
      u8g.drawStr(0, 10, "ENTER DISTANCE");
      u8g.setPrintPos(0,29);
      u8g.print(y);  // Put entered number on Nokia lcd    
      u8g.drawStr(62, 29, "MM");
      u8g.drawStr(4, 46, "cur-pos");
      u8g.setPrintPos(57,47); 
      u8g.print(currentposition);  //  Display current position of stepper
    }
      while( u8g.nextPage() );

}

Arduino 13

Arduino Based Heartbeat Monitor Project




อุปกรณ์

1. Arduino UNO
2.Sensor module (Heartbeat module )
3.LCD 16x2
4.Bread board
5.Power and Connecting wires

ตัวอย่างโค้ด


#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int in = 8;
int Reset=6;
int start=7;
int count=0,i=0,k=0,rate=0;
unsigned long time2,time1;
unsigned long time;
byte heart[8] = 
{
  0b00000,
  0b01010,
  0b11111,
  0b11111,
  0b11111,
  0b01110,
  0b00100,
  0b00000
};
void setup()
{
  lcd.createChar(1, heart);
  lcd.begin(16,2);
  
  lcd.print("Heart Beat ");
  lcd.write(1);
  lcd.setCursor(0,1);
  lcd.print("Monitering");
  pinMode(in, INPUT);
  pinMode(Reset, INPUT);
  pinMode(start, INPUT);
  digitalWrite(Reset, HIGH);
  digitalWrite(start, HIGH);
  delay(1000);
}
void loop()
{
  if(!(digitalRead(start)))
  {
    k=0;
    lcd.clear();
    lcd.print("Please wait.......");
    while(k<5)
    {
     if(digitalRead(in))
     {
      if(k==0)
      time1=millis();
      k++;
      while(digitalRead(in));
     }
    }
      time2=millis();
      rate=time2-time1;
      rate=rate/5;
      rate=60000/rate;
      lcd.clear();
      lcd.print("Heart Beat Rate:");
      lcd.setCursor(0,1);
      lcd.print(rate);
      lcd.print(" ");
      lcd.write(1);      
      k=0;
      rate=0;
    }
  if(!digitalRead(Reset))
  {
    rate=0;
     lcd.clear();
      lcd.print("Heart Beat Rate:");
      lcd.setCursor(0,1);
      lcd.write(1);
      lcd.print(rate);
      k=0;
  }
}

วันอาทิตย์ที่ 2 กันยายน พ.ศ. 2561

Flowchart

สัญลักษณ์ Flowchart

สัญลักษณ์ Flowchart

ความหมายของ Flowchart
Flowchart หรือ ผังงาน คือ รูปภาพ หรือ สัญลักษณ์ที่ใช้เขียนแทนขั้นตอน คำอธิบาย ข้อความ หรือ คำพูดที่ใช้ในอัลกอริทึม (Algorithm) เพราะการนำเสนอขั้นตอนของงานให้เข้าใจตรงกัน ระหว่างผู้เกี่ยวข้อง ด้วยคำพูด หรือ ข้อความทำได้ยากกว่า

ประเภทของ Flowchart
แบ่งได้ 2 ประเภท คือ
  1. ผังงานระบบ (System Flowchart) คือ ผังงานที่แสดงขั้นตอนการทำงานในระบบอย่างกว้างๆ แต่ไม่เจาะลงในระบบงานย่อย
  2. ผังงานโปรแกรม (Program Flowchart) คือ ผังงานที่แสดงถึงขั้นตอนในการทำงานของโปรแกรม ตั้งแต่รับข้อมูล คำนวณ จนถึงแสดงผลลัพธ์

ประโยชน์ของ Flowchart
  1. ทำให้เข้าใจ และแยกแยะปัญหาได้ง่าย (Problem Define)
  2. แสดงลำดับการทำงาน (Step Flowing)
  3. หาข้อผิดพลาดได้ง่าย (Easy to Debug)
  4. ทำความเข้าใจโปรแกรมได้ง่าย (Easy to Read)
  5. ไม่ขึ้นกับภาษาใดภาษาหนึ่ง (Flexible Language)

การโปรแกรมแบบมีโครงสร้าง
ประกอบด้วยหลักการ 3 อย่าง คือ
  1. การทำงานแบบตามลำดับ (Sequence) เป็นรูปแบบการเขียนโปรแกรมที่ง่ายที่สุดคือ เขียนให้ทำงานจากบนลงล่าง เขียนคำสั่งเป็นบรรทัด และทำทีละบรรทัดจากบรรทัดบนสุดลงไปจนถึงบรรทัดล่างสุด สมมติให้มีการทำงาน 3 กระบวนการคือ อ่านข้อมูล คำนวณ และพิมพ์
  2. การเลือกกระทำตามเงื่อนไข (Decision) เป็นการตัดสินใจ หรือเลือกเงื่อนไขคือ เขียนโปรแกรมเพื่อนำค่าไปเลือกกระทำ โดยปกติจะมีเหตุการณ์ให้ทำ 2 กระบวนการ คือเงื่อนไขเป็นจริงจะกระทำกระบวนการหนึ่ง และเป็นเท็จจะกระทำอีกกระบวนการหนึ่ง แต่ถ้าซับซ้อนมากขึ้น จะต้องใช้เงื่อนไขหลายชั้น เช่น การตัดเกรดนักศึกษา เป็นต้น ตัวอย่างผังงานนี้ จะแสดงผลการเลือกอย่างง่าย เพื่อกระทำกระบวนการเพียงกระบวนการเดียว
  3. การทำซ้ำ (Loop) เป็นการทำกระบวนการหนึ่งหลายครั้ง โดยมีเงื่อนไขในการควบคุม หมายถึงการทำซ้ำเป็นหลักการที่ทำความเข้าใจได้ยากกว่า 2 รูปแบบแรก เพราะการเขียนโปรแกรมแต่ละภาษา จะไม่แสดงภาพอย่างชัดเจนเหมือนการเขียนผังงาน ผู้เขียนโปรแกรมต้องจินตนาการด้วยตนเอง

สัญลักษณ์ความหมาย

การกำหนดค่า หรือ การประมวลผลทั่วไป
การประมวลของโปรแกรมย่อย (Subroutine)
การตัดสินใจ การเปรียบเทียบ จะมีผลใน 2 ทิศทาง คือ กรณีผลตรวจสอบเงื่อนไขเป็นเท็จ และเป็นจริง
รับ หรือ แสดงข้อมูล โดยไม่ระบุชนิดอุปกรณ์
โปรแกรมย่อย หรือ โมดูล เริ่มทำงานหลักจากจบคำสั่งในโปรแกรมย่อยแล้ว จะกลับมาทำคำสั่งต่อไป
การเก็บข้อมูลภายใน
การแสดงผลเอกสาร หรือ การแสดงผลออกทางเครื่องพิมพ์
การแสดงผลหลายเอกสารพร้อมกัน
การเริ่มต้น หรือ การสิ้นสุด
การกำหนดค่าต่างๆ ล่วงหน้า ซึ่งเป็นการทำงานภายในช่วงหนึ่งที่ซ้ำๆ กัน
การรับข้อมูลเข้าทางแป้นพิมพ์
การทำซ้ำจนกระทั้งสิ้นสุดตามจำนวนที่กำหนด
จุดเชื่อมต่อในหน้าเดียวกัน
จุดเชื่อมต่อคนละหน้า
การรับข้อมูลเข้า หรือ แสดงผลโดยใช้บัตรเจาะรู
การรับข้อมูลเข้า หรือ แสดงผลโดยใช้เทปกระดาษเจาะรู
จุดร่วมการเชื่อมต่อ
หรือ
การจัดลำดับรายการของข้อมูลในรูปแบบมาตรฐาน
การจัดลำดับรายการของข้อมูล
การแยกให้เป็นสองขั้นตอนย่อย
การรวมสองขั้นตอนย่อยให้เป็นขั้นตอนเดียว
แหล่งเก็บข้อมูล Online หน่วยความจำสำรอง
การหน่วงเวลา
การรับ หรือ แสดงผลข้อมูลทางเทปแม่เหล็ก
การรับข้อมูลเข้า หรือ แสดงผลโดยใช้จานแม่เหล็ก
การจัดเก็บข้อมูลแบบการเข้าถึงโดยตรง
จอภาพแสดงผล


ตัวอย่าง

Internet of Things (IoT)

Internet of Things หรือ IoT Internet of Things (IoT)  คือ การที่อุปกรณ์อิเล็กทรอนิกส์ต่างๆ สามารถเชื่อมโยงหรือส่งข้อมูลถึงกันได้ด้วยอิน...