2017年1月24日 星期二

蜂鳴器音樂盒

int sp=2;
int frequency[] = { //C調音符表
  //C(1), C+(2), D(3), D+(4), E(5), F(6), F+(7), G(8), G+(9), A(10), A+(11), B(12),
  262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
  523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
  1046, 1109, 1175, 1245, 1318, 1397, 1480, 1568, 1661, 1760, 1865, 1976
};
byte magicalprincessTone[] =
{ 10, 17, 17, 15, 17, 17,   20, 15, 0,   10, 17, 17, 15, 17, 17,
  20, 0, 17, 20,   22, 22, 22, 22, 22, 22,   20, 15, 17, 0,
  15, 15, 15, 17, 15, 10,   17, 0,   10, 17, 17, 15, 17, 20,
  15, 0,   10, 17, 17, 15, 17, 17, 20,   20, 0, 17, 20,
  22, 22, 22, 22, 22,   20, 15, 17, 0,   15, 15, 15, 17, 15, 10, 13,
  13, 0,   22, 22, 22, 24, 22, 22,   25, 24, 17, 0,
  15, 20, 17, 15, 17,   24, 24, 24, 25, 24, 20,   25, 24, 17, 0, 17,
  22, 20, 18, 20,   0, 17, 20,   22, 25, 24, 17, 20,
  22, 0, 17, 20,   22, 25, 24, 17, 20,   22,  0, 0,
};
byte magicalprincessBeat[] =     //d=84
{ 1, 1, 1, 1, 3, 1,   1, 5, 2,   1, 1, 1, 1, 3, 1,
  4, 2, 1, 1,   1, 1, 1, 1, 3, 1,   2, 2, 2, 2,
  1, 1, 1, 1, 2, 2,   6, 2,   1, 1, 1, 1, 3, 1,
  6, 2,   1, 1, 1, 1, 1, 2, 1,   4, 2, 1, 1,
  2, 1, 1, 3, 1,   2, 2, 2, 2,   1, 1, 1, 1, 1, 2, 1,
  6, 2,   1, 1, 1, 1, 3, 1,   2, 2, 2, 2,
  2, 2, 2, 1, 1,   1, 1, 1, 1, 3, 1,   2, 2, 2, 1, 1,
  1, 3, 1, 4,   2, 1, 1,   2, 2, 1, 2, 1,
  4, 2, 1, 1,   2, 2, 1, 2, 1,   12,   2, 2,
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int len=sizeof(magicalprincessTone);
  for (int i=0;i<len;i++)
  {
    tone(sp,frequency[magicalprincessTone[i]]);
    delay(351*magicalprincessBeat[i] );
  }
  noTone(sp);
}
===================================
按鈕控制歌1或歌2即停止

int sp=2;
int sw1=0;
int sw2=0;

int frequency[] = { //C調音符表
  //C(1), C+(2), D(3), D+(4), E(5), F(6), F+(7), G(8), G+(9), A(10), A+(11), B(12),
  262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
  523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
  1046, 1109, 1175, 1245, 1318, 1397, 1480, 1568, 1661, 1760, 1865, 1976
};
byte magicalprincessTone[] =
{ 10, 17, 17, 15, 17, 17,   20, 15, 0,   10, 17, 17, 15, 17, 17,
  20, 0, 17, 20,   22, 22, 22, 22, 22, 22,   20, 15, 17, 0,
  15, 15, 15, 17, 15, 10,   17, 0,   10, 17, 17, 15, 17, 20,
  15, 0,   10, 17, 17, 15, 17, 17, 20,   20, 0, 17, 20,
  22, 22, 22, 22, 22,   20, 15, 17, 0,   15, 15, 15, 17, 15, 10, 13,
  13, 0,   22, 22, 22, 24, 22, 22,   25, 24, 17, 0,
  15, 20, 17, 15, 17,   24, 24, 24, 25, 24, 20,   25, 24, 17, 0, 17,
  22, 20, 18, 20,   0, 17, 20,   22, 25, 24, 17, 20,
  22, 0, 17, 20,   22, 25, 24, 17, 20,   22,  0, 0,
};
byte magicalprincessBeat[] =     //d=84
{ 1, 1, 1, 1, 3, 1,   1, 5, 2,   1, 1, 1, 1, 3, 1,
  4, 2, 1, 1,   1, 1, 1, 1, 3, 1,   2, 2, 2, 2,
  1, 1, 1, 1, 2, 2,   6, 2,   1, 1, 1, 1, 3, 1,
  6, 2,   1, 1, 1, 1, 1, 2, 1,   4, 2, 1, 1,
  2, 1, 1, 3, 1,   2, 2, 2, 2,   1, 1, 1, 1, 1, 2, 1,
  6, 2,   1, 1, 1, 1, 3, 1,   2, 2, 2, 2,
  2, 2, 2, 1, 1,   1, 1, 1, 1, 3, 1,   2, 2, 2, 1, 1,
  1, 3, 1, 4,   2, 1, 1,   2, 2, 1, 2, 1,
  4, 2, 1, 1,   2, 2, 1, 2, 1,   12,   2, 2,
};

byte CastleintheSkyTone[] =  
{ 22, 24,
  25, 24, 25, 29,   24, 17,   22, 20, 22, 25,   20, 17, 17,
  18, 17, 18, 25,   17, 0, 25, 25, 25,   24, 19, 18, 24,   24, 0, 22, 24,
  25, 24, 25, 29,   24, 17, 17,   22, 20, 22, 25,   20, 0, 17,
  18, 25, 24, 25,   27, 27, 29, 25,   25, 24, 22, 22, 24, 21,   22, 0, 25, 27,
  29, 27, 29, 32,   27, 20, 20,   25, 24, 25, 29,   29, 0,
  22, 24, 25, 24, 25, 27, 27,   25, 20, 20, 0,   30, 29, 27, 25,   29,
  0, 29,   34, 34, 32, 32,   29, 27, 25, 0, 25,   27, 25, 27, 32,
  29, 0, 29,   34, 34, 32,   29, 27, 25, 0, 25,   27, 25, 27, 24,
  22,   0,
};
byte CastleintheSkyBeat[] =     //d=100
{ 1, 1,
  3, 1, 2, 2,   6, 2,   3, 1, 2, 2,   6, 1, 1,
  3, 1, 2, 2,   4, 1, 1, 1, 1,   3, 1, 2, 2,   4, 2, 1, 1,
  3, 1, 2, 2,   6, 1, 1,   3, 1, 2, 2,   6, 1, 1,
  2, 1, 3, 2,   2, 1, 1, 4,   1, 1, 1, 1, 2, 2,   4, 2, 1, 1,
  3, 1, 2, 2,   6, 1, 1,   3, 1, 2, 2,   6, 2,
  1, 1, 2, 1, 1, 2, 2,   3, 1, 2, 2,   2, 2, 2, 2,   12,
  2, 2,   3, 1, 3, 1,   1, 1, 4, 1, 1,   2, 1, 3, 2,
  4, 2, 2,   3, 1, 4,   1, 1, 4, 1, 1,   2, 1, 3, 2,
  14,   2,
};
byte SpiritedAwayTone[] =
{ 13,15,
17,13,20,17,   15,20,15,   13,10,17,13,
12,0,12,   10,12,13,15,   8,13,15,17,   18,18,17,15,13,
15,13,15,   17,13,20,17,   15,20,15,   13,10,17,13,
12,0,12,   10,12,13,15,   8,13,15,17,   18,18,17,15,13,
13,   13,0,17,18,   20,20,20,
20,20,22,20,18,   17,17,17,   17,17,18,17,15,   13,13,13,12,
10,12,12,13,   15,15,17,15,17,   15,17,18,   20,20,20,
20,20,22,20,18,   17,17,17,   17,18,17,15,13,12,   10,10,12,13,15,
8,13,15,17,   15,15,15,13,   13,
};
byte SpiritedAwayBeat[] =     //d=116
{1,1,
1,1,3,1,   2,2,2,   1,1,3,1,
2,2,2,   2,2,1,1,   2,2,1,1,   2,1,1,1,1,
4,1,1,   1,1,3,1,   2,2,2,   1,1,3,1,
2,2,2,   2,2,1,1,   2,2,1,1,   2,1,1,1,1,
6,   2,2,1,1,   2,2,2,
2,1,1,1,1,   2,2,2,   2,1,1,1,1,   2,2,1,1,
2,2,1,1,   2,1,1,1,1,   4,1,1,   2,2,2,
2,1,1,1,1,   2,2,2,   1,1,1,1,1,1,   2,1,1,1,1,
2,2,1,1,   3,1,1,1,   6,
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
    sw1 = digitalRead(4);
    sw2=digitalRead(5);

    Serial.print(sw1);
    Serial.println();
    Serial.print(sw2);
   noTone(sp);
   if (sw1==1)
   {
       int len=sizeof(magicalprincessTone);
   
      for (int i=0;i<len;i++)
      {
        int kp=digitalRead(3);
        if(kp==1)
        {
          break;
        }
        tone(sp,frequency[magicalprincessTone[i]]);
        delay(351*magicalprincessBeat[i] );
      }
      noTone(sp);
   }
   else if (sw2==1)
   {
    int len=sizeof(SpiritedAwayTone);
  for (int i=0;i<len;i++)
  {
        int kp=digitalRead(3);
        if(kp==1)
        {
          break;
        }
    tone(sp,frequency[SpiritedAwayTone[i]]);
    delay(351* SpiritedAwayBeat[i] );
  }
  noTone(sp);
   }
}

npn1815

int tsw=7;

void setup() {
  // put your setup code here, to run once:
  Serial.begin( 9600);
  pinMode(tsw,OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()>0)
  {
    char i=Serial.read();
    if (i=='0')
    {
      digitalWrite(tsw,LOW);
    }
    else
    {
       digitalWrite(tsw,HIGH);
    }
  }
}
==========================
http://www.codedata.com.tw/social-coding/mblock-arduino-15-transistor-relay/

認識電晶體

實際上,提到電晶體時,多半是指雙接面電晶體(Bipolar junction transistor, BJT),依組合的半導體材料,可以分為 NPN 與 PNP 兩大類,以 NPN 電晶體為例,是由 N 型半導體與 P 型半導體組合而成:
雙極性電晶體
電晶體有兩接面、三個端點,端點分別為集極(Collector)、基極(Base)與射極(Emitter),NPN 電晶體的基本特性是,射極電流 Ie 為基極電流 Ib 與集極電流 Ic 之和,而增益 α = Ic / Ie,β = Ic / Ib,Ib 通常遠小於 Ic。
當三個端點施加之偏壓不同時,電晶體會處於不同狀態:
雙極性電晶體
  • B-E 為順向偏壓,且 B-C 為順向偏壓時,此時電晶體狀態處於飽和區(Saturation region),此時 C 與 E 可視為短路,依電路實際連接情況,Ic 會處於一個飽和電流值。
  • B-E 為逆向偏壓,且 B-C 為逆向偏壓時,此時電晶體狀態處於截止區(Cutoff region),此時 C 與 E 可視為開路,不過,實際上還是會有少量電流通過,這個現象稱為「漏電」(Leakage)。
  • B-E 為順向偏壓,且 B-C 為逆向偏壓時,此時電晶體狀態處於動作區(Active region),此時可用於信號放大。

2017年1月19日 星期四

尻lcd 16*2 spi通訊練習二

選擇
#include<Wire.h>
#include<LiquidCrystal.h>

LiquidCrystal lcd(8,9,4,5,6,7);
#define right 1
#define up 2
#define down 3
#define left 4
#define select 5
#define noselect 0

int akey=0;

void selectfun()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("select button");
  lcd.setCursor(0,1);
  lcd.print("1:R,2:U,3:D,4:L,5:S,0:N#");
}
int btfun()
{
  akey=analogRead(A0);
  if (akey>1000)
  {
    return noselect;
  }
  else if (akey<50)
  {
    return right;
  }
  else if (akey<195)
  {
    return up;
  }
  else if (akey<380)
  {
    return down;
  }
  else if (akey<555)
  {
    return left;
  }
  else if (akey<790)
  {
    return select;
  }
  return noselect;
}
void setup() {
  // put your setup code here, to run once:
  lcd.begin(16,2);
  selectfun();
}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.clear();
  int a=btfun();
  switch (a)
  {
    case noselect:
    {
      selectfun();
      break;
    }
    case right:
    {
      lcd.print("Right");
      break;
    }
    case up:
    {
      lcd.print("Up");
      break;
    }
    case down:
    {
      lcd.print("Down");                                                                                                                              
      break;
    }
    case left:
    {
      lcd.print("Left");
      break;
    }
    case select:
    {
      lcd.print("Select");
      break;
    }

  }
  delay(500);
}
==============================================
控制LED燈

#include<Wire.h>
#include<LiquidCrystal.h>

LiquidCrystal lcd(8,9,4,5,6,7);
#define right 1
#define up 2
#define down 3
#define left 4
#define select 5
#define noselect 0
int led1=1;
int led2=2;
int led3=3;
int led4=A1;

int akey=0;

void selectfun()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("select button");
  lcd.setCursor(0,1);
  lcd.print("1:R,2:U,3:D,4:L:N#");

  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
}
int btfun()
{
  akey=analogRead(A0);
  if (akey>1000)
  {
    return noselect;
  }
  else if (akey<50)
  {
    return right;
  }
  else if (akey<195)
  {
    return up;
  }
  else if (akey<380)
  {
    return down;
  }
  else if (akey<555)
  {
    return left;
  }
  else if (akey<790)
  {
    return select;
  }
  return noselect;
}
void ledclear()
{
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}
void setup() {
  // put your setup code here, to run once:
  lcd.begin(16,2);
  selectfun();

}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.clear();
  int a=btfun();
  switch (a)
  {
    case noselect:
    {
      selectfun();
      break;
    }
    case right:
    {
      ledclear();
      digitalWrite(led1,HIGH);
      lcd.print("Right");
      break;
    }
    case up:
    {
      ledclear();
      digitalWrite(led2,HIGH);
      lcd.print("Up");
      break;
    }
    case down:
    {
      ledclear();
      digitalWrite(led3,HIGH);
      lcd.print("Down");                                                                                                                                
      break;
    }
    case left:
    {
      ledclear();
      digitalWrite(led4,HIGH);
      lcd.print("Left");
      break;
    }
    case select:
    {
      ledclear();
      lcd.print("STOP");
      break;
    }
 
  }
  delay(500);
}
================================

2017年1月17日 星期二

lcd 16*2 spi 通訊練習(一)

Arduino UNO內定10,11,12,13為SPI通信界面使用
10 :SS chip select從設備致能信號,由主設備控制
11 : MOSI  主設備數據輸出,從設備數據輸入
12 : MISO  主設備數據輸入,從設備數據輸出
13 : CLOCK 時鐘信號,由主設備產生

9.

http://coopermaa2nd.blogspot.tw/2010/12/arduino-lab9-2x16-lcd-world.html
[image[20].png]

腳位編號名稱說明
1Vss接地 (0V)
2Vdd電源 (+5V)
3Vo 或稱 Vee對比(0-5V), 可接一顆 1k 電阻,或利可變電阻調整適當的對比
4RSRegister Select:
  1: D0 – D7 當作資料解釋
  0: D0 – D7 當作指令解釋
5R/WRead/Write mode:
  1: 從 LCD 讀取資料
  0: 寫資料到 LCD

因為很少從 LCD 這端讀取資料,可將此腳位接地以節省 I/O 腳位。
6EEnable
7D0Bit 0 LSB
8D1Bit 1
9D2Bit 2
10D3Bit 3
11D4Bit 4
12D5Bit 5
13D6Bit 6
14D7Bit 7 MSB
15A+背光(串接 330R 電阻到電源)
16K-背光(GND)


/* Lab9 - 在 2x16 LCD 上顯示 "Hello World" 訊息  
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * 10K Potentiometer:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 This example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// 引用 LiquidCrystal Library
#include <LiquidCrystal.h>

// 建立 LiquidCrystal 的變數 lcd
//                 LCD 接腳:  rs, enable, d4, d5, d6, d7  
//      對應到 Arduino 接腳:  12,     11,  5,  4,  3,  2
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // 設定 LCD 的行列數目 (2 x 16)
  lcd.begin(16, 2);

  // 列印 "Hello World" 訊息到 LCD 上
  lcd.print("hello, world!");
}

void loop() {
  // 將游標設到 column 0, line 1
  // (注意: line 1 是第二行(row),因為是從 0 開始數起):
  lcd.setCursor(0, 1);

  // 列印 Arduino 重開之後經過的秒數
  lcd.print(millis()/1000);
}
============================

#include<Wire.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
void setup() {
  // put your setup code here, to run once:
  lcd.begin(16,2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Hello,World!");

}
===========================
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11,10, 5, 4, 3, 2);
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.begin(16, 2);
  //lcd.clear;
  lcd.setCursor(0, 0);
}

void loop() {
  // put your main code here, to run repeatedly:
   if (Serial.available()>0)
   {
   // String msg=Serial.readString();
 
    //lcd.print(msg);
    //lcd.print(millis()/1000);
    while (Serial.available()>0)
    {
      lcd.write(Serial.read());
    }
 
   }
}
=========================
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11,10, 5, 4, 3, 2);
String msg;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  lcd.begin(16, 2);
  //lcd.clear;
  lcd.setCursor(0, 0);
  lcd.blink();
}
================================
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11,10, 5, 4, 3, 2);
String msg;
void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  lcd.clear();
}
void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(10,0);
  lcd.print("Hello");
  for(int i=0;i<10;i++)
  {
    lcd.scrollDisplayLeft();
    delay(500);
   }
   for(int i=0;i<10;i++)
  {
    lcd.scrollDisplayRight();
    delay(500);
   }
}
==============================
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11,10, 5, 4, 3, 2);
int count=0;
void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);

}
void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(0,1);
  lcd.print(count);
  if (count<0)
  {
    count=0;
  }
  else
  {
    lcd.print(count);
  }
  delay(1000);
  count++;
  if (count==0)
  {
    count=10;
  }
}
===============================

2017年1月12日 星期四

藍芽

HC-06 藍芽模組的設定

http://opensource.ntpc.edu.tw/moodle/mod/url/view.php?id=4992
 http://gsyan888.blogspot.tw/2014/03/arduino-hc-06-at-command.html 

#include  <SoftwareSerial.h>
SoftwareSerial BTSerial(8, 9); //   TX|RX
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("BTSerial is ready");
  BTSerial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
   if (Serial.available())
   {
    BTSerial.write(Serial.read());
   }
   if (BTSerial.available())
   {
    Serial.write(BTSerial.read());
   }
}
=============================
AT ==>OK
AT+VERSION  ==>1.8
=============================
98:D3:31:FD:2F:D5----------pin
1234----------------------------password
===================================
#include  <SoftwareSerial.h>
SoftwareSerial BTSerial(8, 9); //   TX|RX
int led1=5;
int led2=6;
int led3=7;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("BTSerial is ready");
  BTSerial.begin(9600);

  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led2,OUTPUT);
}
void cleared()
{
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
}
void loop() {
  // put your main code here, to run repeatedly:

   if (Serial.available())
   {
    BTSerial.write(Serial.read());
   }
   if (BTSerial.available())
   {
     char key=BTSerial.read();
   
      if (key=='L'||key=='l')
      {
        cleared();
      }
      else if(key=='F'||key=='f')
      {
        cleared();
        digitalWrite(led1,HIGH);
       }
       else if(key=='R'||key=='r')
      {
        cleared();
        digitalWrite(led2,HIGH);
      }
      else if(key=='B'||key=='b')
      {
        cleared();
        digitalWrite(led3,HIGH);
      }
   }
}
==================================
Arduino Libraray自訂函數庫;
1
========================
//放在library Serialcom3資料夾 Serialcom3.h
#ifndef Serialcom3_h
#define Serialcom3_h

class Serialcom3
{
public:
int fun1(int x,int b);
void fun2(int a,int y);
};
#endif
  ======================
Serialcom3.cpp
#include<Serialcom3.h>
#include<Arduino.h>

int Serialcom3::fun1(int x,int y)
{
int total=x+y;
return total;
}
void Serialcom3::fun2(int a,int b)
{
Serial.begin(9600);
        Serial.println(a+b);
}
=============================

2017年1月10日 星期二

raspberry

https://www.udemy.com/homebaked-raspberry-pi-django/learn/v4/t/lecture/5973658
https://carolhsu.gitbooks.io/django-girls-tutorial-traditional-chiness/content/

L293D晶片與馬達

「l293d」的圖片搜尋結果
int in1Pin=2;
int in2Pin=3;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("press S to stop");
  Serial.println("press R to right");
  Serial.println("press L to left");
  pinMode(in1Pin,OUTPUT);
  pinMode(in2Pin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
   if (Serial.available()>0)
  {
    char key=Serial.read();
    Serial.print("key:");
    Serial.println(key);
    if (key=='S'||key=='s')
    {
      digitalWrite(in1Pin,LOW);
      digitalWrite(in2Pin,LOW);
    }
    else if(key=='R'||key=='r')
    {
      digitalWrite(in1Pin,HIGH);
      digitalWrite(in2Pin,LOW);
    }
    else if(key=='L'||key=='l')
    {
      digitalWrite(in1Pin,LOW);
      digitalWrite(in2Pin,HIGH);
    }
  }
}
=========================
兩個5v 都要接
==================
3,6腳接led及馬達
=======================
接ㄧ個按鈕
int in1Pin=4;
int in2Pin=5;
int int0=0;
volatile boolean state1=false;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(in1Pin,OUTPUT);
  pinMode(in2Pin,OUTPUT);

  attachInterrupt(int0,fun1,RISING);
}
void fun1()
{
  state1=!state1;
}
void loop() {
  // put your main code here, to run repeatedly:
   if (state1)
  {
    digitalWrite(in1Pin,HIGH);
    digitalWrite(in2Pin,LOW);
    }
  else
  {
     digitalWrite(in1Pin,LOW);
     digitalWrite(in2Pin,LOW);
  }
}
================
int in1Pin=4;
int in2Pin=5;
int int0=0;  //在第二接腳
int int1=1;//在第三接腳
volatile boolean state1=false;
volatile boolean state2=false;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(in1Pin,OUTPUT);
  pinMode(in2Pin,OUTPUT);

  attachInterrupt(int0,fun1,RISING);
  attachInterrupt(int1,fun2,RISING);
}
void fun1()
{
  state1=!state1;
}
void fun2()
{
  state2=!state2;
}
void loop() {
  // put your main code here, to run repeatedly:
   if (state1)
  {
    digitalWrite(in1Pin,HIGH);
    digitalWrite(in2Pin,LOW);
    }
  else
  {
     digitalWrite(in1Pin,LOW);
     digitalWrite(in2Pin,LOW);
  }
    if (state2)
  {
    digitalWrite(in1Pin,LOW);
    digitalWrite(in2Pin,HIGH);
    }
  else
  {
     digitalWrite(in1Pin,LOW);
     digitalWrite(in2Pin,LOW);
  }
}
=========================
兩顆馬達
int in1Pin=4;
int in2Pin=5;
int in3Pin=6;
int in4Pin=7;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("press S to stop");
  Serial.println("press F to forword");
  Serial.println("press B to back");
  pinMode(in1Pin,OUTPUT);
  pinMode(in2Pin,OUTPUT);
  pinMode(in3Pin,OUTPUT);
  pinMode(in4Pin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
   if (Serial.available()>0)
  {
    char key=Serial.read();
    Serial.print("key:");
    Serial.println(key);
    if (key=='S'||key=='s')
    {
      digitalWrite(in1Pin,LOW);
      digitalWrite(in2Pin,LOW);
      digitalWrite(in3Pin,LOW);
      digitalWrite(in4Pin,LOW);
    }
    else if(key=='F'||key=='f')
    {
      digitalWrite(in1Pin,HIGH);
      digitalWrite(in2Pin,LOW);
      digitalWrite(in3Pin,HIGH);
      digitalWrite(in4Pin,LOW);
    }
    else if(key=='B'||key=='b')
    {
      digitalWrite(in1Pin,LOW);
      digitalWrite(in2Pin,HIGH);
      digitalWrite(in3Pin,LOW);
      digitalWrite(in4Pin,HIGH);
    }
  }
}

2017年1月5日 星期四

指標運用

int sum(int a,int b);
int (*f)(int x,int y)=sum;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  int s;
  s=(*f)(112,56);
  Serial.println(s);
}

void loop() {
  // put your main code here, to run repeatedly:

}
int sum(int a,int b)
{
  int total;
  total=a+b;
  return total;
}
=====================

void fun1();
void fun2();
void fun3();

void (*f[3])()={fun1,fun2,fun3};
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  (*f[0])();
  (*f[1])();
  (*f[2])();
}

void loop() {
  // put your main code here, to run repeatedly:

}
void fun1()
{
  Serial.println("block1");
}
void fun2()
{
  Serial.println("block2");
}
void fun3()
{
  Serial.println("block3");
}
==============================
#include <Keypad.h>
const byte numRows=4;
const byte numCols=4;

char Keymap[4][4]=
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};
byte rowPins[numRows]={9,8,7,6};
byte colPins[numCols]={5,4,3,2};
 Keypad myKeypad=Keypad(makeKeymap(Keymap),rowPins,colPins,numRows,numCols);
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
char keypressed=myKeypad.getKey();
if (keypressed!=NO_KEY)
{
  Serial.println(keypressed);
}

}
======================================
#include <Keypad.h>
const byte numRows=4;
const byte numCols=4;

char Keymap[4][4]=
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};
byte rowPins[numRows]={9,8,7,6};
byte colPins[numCols]={5,4,3,2};
 Keypad myKeypad=Keypad(makeKeymap(Keymap),rowPins,colPins,numRows,numCols);
 int led1=11;
 int led2=12;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
char keypressed=myKeypad.getKey();

if (keypressed!=NO_KEY)
{
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  Serial.println(keypressed);
  if (keypressed=='1')
  {
    digitalWrite(led1,HIGH);
  }
  else if (keypressed=='2')
  {
    digitalWrite(led2,HIGH);
  }
  else if (keypressed=='3')
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
  }
}

}
============================
馬達
const int motor=7;
void setup() {
  // put your setup code here, to run once:
 pinMode(motor,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(motor,HIGH);
  delay(2000);
  digitalWrite(motor,LOW);
  delay(1000);
}
==========================
const int motor=3;
int speed=0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  Serial.println("(+)--->up(-)--->stop");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()>0)
  {
    char k=Serial.read();
    Serial.print(k);
    if (k=='+')
    {
      speed=200;
    }
    if (k=='-')
    {
      speed=0;
    }
    analogWrite(motor,speed);
  }
}
==========================================
用Keypad驅動馬達
#include <Keypad.h>
const byte numRows=4;
const byte numCols=4;

char Keymap[4][4]=
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};
byte rowPins[numRows]={9,8,7,6};
byte colPins[numCols]={5,4,3,2};
 Keypad myKeypad=Keypad(makeKeymap(Keymap),rowPins,colPins,numRows,numCols);

const int motor=11;
int speed=0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
char keypressed=myKeypad.getKey();
if (keypressed!=NO_KEY)
{
  Serial.println(keypressed);
  if (keypressed=='1')
  {
    speed=200;
  }
  if (keypressed=='2')
  {
    speed=0;
  }
  analogWrite(motor,speed);
}

}

2017年1月3日 星期二

指標

 Serial.begin(9600);
  int a=50;
  int b=4;
  int *pa=&a;
  int *pb=&b;
  int total=*pa+*pb;

  Serial.println(total);

===========================
一維.陣列指標
Serial.begin(9600);
  int a[3]={12,15,18};;
  int* pa=a;   //陣列本身是指標
  Serial.println(*(pa+0));
  Serial.println(*(pa+1));
  Serial.println(*(pa+2));
========================
二維陣列指標
Serial.begin(9600);
  int a[2][3]={{12,15,18},{45,65,98}};
  int (*pa)[3]=a;
  Serial.println(*(*(pa+0)+0));
  Serial.println(*(*(pa+0)+1));
  Serial.println(*(*(pa+0)+2));
  Serial.println(*(*(pa+1)+0));
  Serial.println(*(*(pa+1)+1));
  Serial.println(*(*(pa+1)+2));
==================================
字元
  Serial.begin(9600);
  char k='a';
  char* pk;
  pk=&k;
  Serial.println(*pk);
==============================
字串
  Serial.begin(9600);
  char *k="hello";

  Serial.println(k);
}
===============================
字串陣列
  Serial.begin(9600);
  char *str[]={"hello","bill","mary"};
  for(int i=0;i<3;i++)
  {
  Serial.println(str[i]);
  }
=================
雙層指標
  Serial.begin(9600);
 int t=50;
 int *pt=&t;
 int **ph=&pt;
  Serial.println(**ph);
========================
函數指標
void  hello();   //先宣告

void (*f)()=hello;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  (*f)();

}

void loop() {
  // put your main code here, to run repeatedly:

}

void hello()
{
  Serial.println("Hello!");
}