置頂文字

歡迎來訪 !! 如有任何問題歡迎一起討論或是 eMail 給我 legorunmail@Gmail.com

2015年11月20日 星期五

Arduino 與 全彩LED燈泡測試

Arduino 與 全彩LED燈泡測試

這次要測試剛買來的全彩LED燈泡測試

網路上的示意圖但紅線(V腳位)不能接5V,要接Ground才能亮



//範例用PWM來控制所以用9,10,11三隻PWM腳位,其他非PWM腳位也都可以但只有HIGH or LOW(1 or o) 看使用者應用,因為Arduino UNO腳位不多因此所有腳位都要充分利用。
//或是用Analog腳位A0~A5也是可以的

int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the  blue LED
int greenpin =9;// select the pin for the green LED

或是數位腳
int redpin = 7; //select the pin for the red LED
int greenpin =2;// select the pin for the green LED
int bluepin =4; // select the pin for the  blue LED



int val;



void setup() {
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  Serial.begin(9600);


}

void loop()
{

  analogWrite(A3, 255);   //255 R 紅 其餘0
  analogWrite(A5, 0);   //255 G 綠 其餘0
   analogWrite(A4, 0);    //255 B 藍 其餘0
   //黃=R255,G255,B0  橘=228,120,51
  delay(1000);
 
 
   analogWrite(A3, 0);   //255 R 紅 其餘0
  analogWrite(A5, 255);   //255 G 綠 其餘0
   analogWrite(A4, 0);    //255 B 藍 其餘0
  delay(1000);


  analogWrite(A3, 255);   //255 R 紅 其餘0
  analogWrite(A5, 255);   //255 G 綠 其餘0
   analogWrite(A4, 0);    //255 B 藍 其餘0
  delay(1000);


digitalWrite(redpin, HIGH);   //255 R 紅 其餘0
  digitalWrite(greenpin, LOW);   //255 G 綠 其餘0
   digitalWrite(bluepin, LOW);    //255 B 藍 其餘0



  /*
for(val=255; val>0; val--)
  {
   analogWrite(11, val);
   analogWrite(10, 255-val);
   analogWrite(9, 128-val);
   delay(10);
  }
for(val=0; val<255 p="" val="">  {
   analogWrite(11, val);
   analogWrite(10, 255-val);
   analogWrite(9, 128-val);
   delay(10);
  }
 Serial.println(val, DEC);
 */
}

2015年10月9日 星期五

Arduino WIFI 注意事項

Arduino WIFI 注意事項

ESP8266 WiFi 是Arduino常用的WIFI模組,該模組有兩種運行模式(AP模式和STA模式)

AP模式(相當於區域網路連結,手機本地控制):
把ESP8266 WiFi 熱點,也就是預先設定ESP8266 AP熱點名稱與密碼,這樣手機搜尋WIFI找到設定的名稱與密碼就可以連線使用。

執行手機APK程式就可以用WIFI方式與Arduino ESP8266溝通。


STA模式(相當於Internet網路連結,用於外部網路讓手機、網頁控制):

網路資源





另一塊WIFI模組 :nRF24L01 (電壓不可超過3.6V)
nFR24L01 2.4G無線模組只能單向雙工,一個模組只能單純發送或接收,不能改變,所以我們使用兩塊Arduino開發板來分別收發


網路資源  網路資源02  網路資源03  網路資源04


使用器材
arduino UNR R3*2
NRF2401 *2

使用庫
RF24
RF24Network


//////////////////////////////////////主機-發送信號/////////////////////////////////////
#include
#include
#include

// nRF24L01(+) radio attached using Getting Started board 
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 1;
// Address of the other node
const uint16_t other_node = 0;
// How often to send 'hello world to the other unit
const unsigned long interval = 150; //ms
// When did we last send?
unsigned long last_sent;
// How many have we sent already
//unsigned long packets_sent;

// Structure of our payload
struct payload_t
{
  uint32_t ms;
  uint32_t sensorDataA;
  uint32_t sensorDataB;
};

boolean power_SW;
int led_VOL;

void setup(void)
{
  Serial.begin(115200);
  Serial.println("RF24Network/examples/helloworld_tx/");

  SPI.begin();
  radio.begin();
  radio.setDataRate( RF24_250KBPS ) ;
  network.begin(/*channel*/ 50, /*node address*/ this_node);
  randomSeed(analogRead(0));
  pinMode(7,INPUT_PULLUP); 
}

void loop(void)
{
  power_SW=!digitalRead(7);
  led_VOL=analogRead(A0)/4;

  // Pump the network regularly
  network.update();
  // If it's time to send a message, send it!
  unsigned long now = millis();
  if ( now - last_sent >= interval  )
  {
    last_sent = now;

    Serial.print("power_SW:");
    Serial.println(power_SW);
    Serial.print("led_VOL:");
    Serial.println(led_VOL);

    Serial.print("Sending...");
    payload_t payload = { 
      millis(),power_SW,led_VOL    };
    RF24NetworkHeader header(/*to node*/ other_node);
    bool ok = network.write(header,&payload,sizeof(payload));
    if (ok)
      Serial.println("ok.");
    else
      Serial.println("failed.");
  }
}


//////////////////////////////////////從機-接收信號/////////////////////////////////////


#include
#include
#include

// nRF24L01(+) radio attached using Getting Started board 
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 0; 
// Address of the other node
const uint16_t other_node = 1;

// Structure of our payload
struct payload_t
{
  uint32_t ms;
  uint32_t sensorDataA;
  uint32_t sensorDataB;
};

#define power_PIN 5
#define led_PIN 6

boolean power_SW;
int led_VOL;


void setup(void)
{
  Serial.begin(115200);

  SPI.begin();
  radio.begin();
  radio.setDataRate( RF24_250KBPS ) ;
  network.begin(/*channel*/ 50, /*node address*/ this_node);

  pinMode(power_PIN,OUTPUT);
  pinMode(led_PIN,OUTPUT);
}

void loop(void)
{  
  // Pump the network regularly
  network.update();
  // Is there anything ready for us?
  while ( network.available() )
  {
    // If so, grab it and print it out
    RF24NetworkHeader header;
    payload_t payload;
    network.read(header,&payload,sizeof(payload));
    power_SW=payload.sensorDataA;
    led_VOL=payload.sensorDataB;

    Serial.print("power_SW:");
    Serial.println(power_SW);
    Serial.print("led_VOL:");
    Serial.println(led_VOL);
  }

  digitalWrite(power_PIN,power_SW);
  analogWrite(led_PIN,led_VOL);

}





2015年10月5日 星期一

Arduino 與藍芽模組設定 HC-05 HC06

Arduino 與藍芽模組設定  HC-05 HC06

網昱多媒體      網路參考資料  透過藍芽傳輸程式  透過藍芽傳輸程式02

透過藍芽傳輸程式03




HC-06 和 Arduino 的腳位對應如下:

HC-06 VCC → Arduino 5V
HC-06 GND → Arduino GND
HC-06 TXD → Arduino pin 10
HC-06 RXD → Arduino pin 11

#include
SoftwareSerial BTSerial(10, 11); // RX | TX
void setup()
{
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  BTSerial.begin(9600);  // HC-06 current bound rate (default 9600)
}
void loop()
{
  // Keep reading from HC-06 and send to Arduino Serial Monitor
  if (BTSerial.available())
    Serial.write(BTSerial.read());
  // Keep reading from Arduino Serial Monitor and send to HC-06
  if (Serial.available())
    BTSerial.write(Serial.read());
}


執行AT命令修改相關資料







  • AT:測試,回應「OK」
  • AT+VERSION:回應靭體的版本。
  • AT+NAMExyz:將裝置名稱改為「xyz」。                                  
  • AT+PIN1234:將連線密碼換為「1234」。
  • AT+BAUD4:將 baud rate 換為 9600。
  • AT+BAUD5:將 baud rate 換為 19200
  • AT+BAUD6:將 baud rate 換為 38400
  • AT+BAUD7:將 baud rate 換為 57600

  • 注意事項: HC-06 一輸入完「AT」就馬上會回應了,建議上面的指令用複製貼上的方法,不然很難跟晶片比快

    在間看模式時要注意
    HC-05 命令結尾\n\r
    HC--06 命令沒有\n\r

    2015年9月18日 星期五

    Arduino UNO R3 與光敏電阻感測器

    Arduino UNO R3 與光敏電阻感測器



    紅外線循跡感測器 (IR Tracker Sensor)

    /*
    KSM017
    入射光強,電阻減小,入射光弱,電阻增大

    1、VCC  電源3.3-5V
    2、GND  接地
    3、DO   TTL開關信号输出 數位輸出(0 or 1)
    4、AO   模擬信號输出 類比輸出(電壓)
    */

    int iAo = A1;
    int iDo = 7;


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

    }

    void loop() {
      // put your main code here, to run repeatedly:
      int pr = analogRead(iAo);
      //int pr2 = analogRead(iDo);
      Serial.print("Dgt=");
      Serial.print(digitalRead(iDo));

      Serial.print("  |  ");
      Serial.print("Alg=");
      Serial.println(pr);


      //int sensorValue = analogRead(1);
      //float Rsensor=(float)(1023-sensorValue)*10/sensorValue;
      //Serial.println(Rsensor);


      delay(1000);
    }

    2015年9月13日 星期日

    Arduino UNO R3 與 L298N馬達驅動板 測試

    Arduino UNO R3 與  L298N馬達驅動板的配合是蠻多人使用的方式,一開始我也是照網路上大家的接法但是馬達就是不動作,後來又爬文爬好久才知道自己的錯誤點(就是沒把L298N與Arduino 板用GRD串接起來)因此怎麼測試馬達就是不動作。


    另外要注意的一點就是你用L298N接2個DC馬達,其中ENA或ENB接在D9、D10腳位控制PWM輸出時,如果又用上Servo時會發現D9、D10失效




    我之前組裝尋跡自走車時用L298N控制2顆DC馬達可以走得穩穩的,後來加上一顆SG90 Servo9G腳位接在D12時其中一顆DC馬達ENA就會發生不動作,原以為是電壓不足就外接電源但ENA依然不動作,上網查資料才知道當使用Servo.h函式庫時D9 與 D10 腳位的 PWM 功能就會被停用,所以ENA PWM失效解決辦法就是ENA PWM不要接D9或 D10

    Servo伺服器雖然使用了 PWM 訊號來控制馬達轉動,不過,這不表示要使用 Arduino 的 PWM 腳位,只不過根據 Servo library 的說明,除了 Arduino Mega 之外,如果使用了 Arduino 的 Servo 程式庫,D9 與 D10 腳位的 PWM 功能就會被停用。(網路資源)  (網路資源)


    原文如下
    The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.


    在Arduino板子中的第3、5、6、9、10、11腳可做PWM輸出,其頻率大都為490Hz,而在Uno板子,第5、6腳的頻率約980Hz
    L298N使用方式



    網路上另一篇文章


    這篇就是解決問題的文章
    /*
    首先來看電源的接法,電源的接法可分成三種,第一種是只提供一組外部電源+12V。
    將+12V電源的正負分別接到+12V與GND這兩個插座,同時將+12V上方的Jumper短路,
    此時+12V會連接到7805,然後降壓成5V,提供給板子上的IC使用。不需要將5V接到Arduino的板子取得5V電源。
    第二種接法同第一種接法,但將5V接到Arduino板子的5V,提供5V給Arduino板,所以Arduino板不需要另外再接5V電源來供電。
    第三種接法則是將+12V上方的Jumper拔起來,不透過7805來產生5V,
    但必須將+5V的插座接到Arduino的5V,也就是從Arduino提供5V給L298N使用。
    以上三種接法,不論哪一種接法,一定要將GND插座與Arduino的GND相連,如此`才有共同的零電壓準位。
    */

    /*
    int value;
    int dir;
    void setup() {
      pinMode(3, OUTPUT);  //接到ENA
      pinMode(4, OUTPUT);  //接到N1
      pinMode(5, OUTPUT);  //接到N2
      digitalWrite(4, HIGH);  //設定馬達正轉
      digitalWrite(5, LOW);
      value= 255;               //PWM輸出值
      dir = -1;                     //value值的遞增或遞減
    }

    void loop() {
      value= value+ dir;  
      if (value< 80)          //當value值小於80時,馬達不會轉,所以dir改成遞增(如果使用3V馬達 上下限值不能大於50~150,  5V 80~255)
        dir = 1;
      else if (value>= 255)  //當value值為最大255時,dir改成遞減
        dir = -1;
      analogWrite(3, value);
      delay(200);
    }

    */


    程式碼如下:

    //也可以用 Analog A0 ~ A5
    //Digital  PWM 3、5、6、9、10、11

    const int motorIn1=5;
    const int motorIn2=6;

    const int motorIn3=9;
    const int motorIn4=10;

    void setup() {

      pinMode(motorIn1, OUTPUT);  //接到N5
      pinMode(motorIn2, OUTPUT);  //接到N6

      pinMode(motorIn3, OUTPUT);  //接到N9
      pinMode(motorIn4, OUTPUT);  //接到N10

    }

    void loop() {

     


      //正轉
      for(int i=150;i<255 i="" p="">    analogWrite(motorIn1,i);
        analogWrite(motorIn2,0);
     
        analogWrite(motorIn3,i);
        analogWrite(motorIn4,0);
     
        delay(500);
      }

      analogWrite(motorIn1,0);
      analogWrite(motorIn2,0);
      analogWrite(motorIn3,0);
      analogWrite(motorIn4,0);
      delay(2000);

      //反轉

      for(int i=150;i<255 i="" p="">    analogWrite(motorIn1,0);
        analogWrite(motorIn2,i);
     
        analogWrite(motorIn3,0);
        analogWrite(motorIn4,i);
        delay(500);
      }

      analogWrite(motorIn1,0);
      analogWrite(motorIn2,0);
      analogWrite(motorIn3,0);
      analogWrite(motorIn4,0);
      delay(2000);


    }

    2015年6月24日 星期三

    Arduino Color Sensor 顏色感應器 TCS230

    Arduino Color Sensor 顏色感應器 TCS230

    Pin 腳接法




    #include

    #define S0     6   // Please notice the Pin's define
    #define S1     5
    #define S2     4
    #define S3     3
    #define OUT    2

    int   g_count = 0;    // count the frequecy
    int   g_array[3];     // store the RGB value
    int   g_flag = 0;     // filter of RGB queue
    float g_SF[3];        // save the RGB Scale factor


    // Init TSC230 and setting Frequency.
    void TSC_Init()
    {
      pinMode(S0, OUTPUT);
      pinMode(S1, OUTPUT);
      pinMode(S2, OUTPUT);
      pinMode(S3, OUTPUT);
      pinMode(OUT, INPUT);

      digitalWrite(S0, LOW);  // OUTPUT FREQUENCY SCALING 2%
      digitalWrite(S1, HIGH);
    }

    // Select the filter color
    void TSC_FilterColor(int Level01, int Level02)
    {
      if(Level01 != 0)
        Level01 = HIGH;

      if(Level02 != 0)
        Level02 = HIGH;

      digitalWrite(S2, Level01);
      digitalWrite(S3, Level02);
    }

    void TSC_Count()
    {
      g_count ++ ;
    }

    void TSC_Callback()
    {
      switch(g_flag)
      {
        case 0:
             Serial.println("->WB Start");
             TSC_WB(LOW, LOW);              //Filter without Red
             break;
        case 1:
             Serial.print("->Frequency R=");
             Serial.println(g_count);
             g_array[0] = g_count;
             TSC_WB(HIGH, HIGH);            //Filter without Green
             break;
        case 2:
             Serial.print("->Frequency G=");
             Serial.println(g_count);
             g_array[1] = g_count;
             TSC_WB(LOW, HIGH);             //Filter without Blue
             break;

        case 3:
             Serial.print("->Frequency B=");
             Serial.println(g_count);
             Serial.println("->WB End");
             g_array[2] = g_count;
             TSC_WB(HIGH, LOW);             //Clear(no filter)
             break;
       default:
             g_count = 0;
             break;
      }
    }

    void TSC_WB(int Level0, int Level1)      //White Balance
    {
      g_count = 0;
      g_flag ++;
      TSC_FilterColor(Level0, Level1);
      Timer1.setPeriod(1000000);             // set 1s period
    }

    void setup()
    {
      TSC_Init();
      Serial.begin(9600);
      Timer1.initialize();             // defaulte is 1s
      Timer1.attachInterrupt(TSC_Callback);
      attachInterrupt(0, TSC_Count, RISING);

      delay(4000);

      for(int i=0; i<3 i="" p="">    Serial.println(g_array[i]);

      g_SF[0] = 255.0/ g_array[0];     //R Scale factor
      g_SF[1] = 255.0/ g_array[1] ;    //G Scale factor
      g_SF[2] = 255.0/ g_array[2] ;    //B Scale factor

      Serial.println(g_SF[0]);
      Serial.println(g_SF[1]);
      Serial.println(g_SF[2]);

    }


    void loop()
    {
       g_flag = 0;
       for(int i=0; i<3 i="" p="">      Serial.println(int(g_array[i] * g_SF[i]));
       delay(4000);

    }




    2015年6月23日 星期二

    Arduino 與 步進馬達28BYJ-48 5v DC+ARDUINO MOTOR SHIELD L293D 驅動板

    Arduino 與 步進馬達28BYJ-48 5v DC+ARDUINO MOTOR SHIELD L293D驅動板 實做

     網路資源

    零件清單
    1.Arduino UNO
    2.步進馬達28BYJ-48 5v DC
    3. 驅動板


     #include  
     #define STEPS 200  //定義步進馬達每圈的步數 


     //steps:代表馬達轉完一圈需要多少步數。如果馬達上有標示每步的度數, 
     //將360除以這個角度,就可以得到所需要的步數(例如:360/3.6=100)。(int) 
         
     //如果步進馬達只會往一個方向轉動,不會反轉把 pin 2 與 pin3 要交換接

     Stepper stepper(STEPS, 9, 10, 11, 12);
         
      void setup(){ 
        stepper.setSpeed(140);     // 將馬達的速度設定成140RPM 最大  150~160  
      } 
         
      void loop(){ 

        stepper.step(1600);//正8圈 
        delay(1000); 
        stepper.step(-1600);//反8圈 
        delay(1000); 
       
      }  





    Part 2
    1.Arduino UNO
    2.直流馬達 DC Motor
    3.ARDUINO MOTOR SHIELD L293D

    網路資源   網路資源2  網路資源3 網路資源4



    在Arduino UNO 對應腳位Pine

    #define MOTO_M1_A 2
    #define MOTO_M1_B 3
    #define MOTO_M2_A 1
    #define MOTO_M2_B 4
    #define MOTO_M3_A 5
    #define MOTO_M3_B 7
    #define MOTO_M4_A 0
    #define MOTO_M4_B 6

    // Arduino pins for the PWM signals.
    #define MOTOR1_PWM 11
    #define MOTOR2_PWM 3
    #define MOTOR3_PWM 6
    #define MOTOR4_PWM 5
    #define SERVO1_PWM 10
    #define SERVO2_PWM 9






    //使用直流馬達範例 DC Motor

    #include            //呼叫該函式庫

    AF_DCMotor motor(4, MOTOR12_64KHZ); // AF_DCMotor 變數名稱(第幾個馬達控制, 頻率)
                                        // Motor1 & Motor2  有這幾個頻率:
                                        // MOTOR12_64KHZ, MOTOR12_8KHZ,
                                         //MOTOR12_2KHZ, MOTOR12_1KHZ
                                        // 
                                        // Motor3 & Motor4  有這幾個頻率:
                                        // MOTOR12_64KHZ, MOTOR12_8KHZ, MOTOR12_1KHZ
                                        //
    void setup() {
      Serial.begin(9600);           // set up Serial library at 9600 bps
      Serial.println("Motor test!");
      
      motor.setSpeed(200);     // set the speed to 200/255
    }

    void loop() {
      Serial.print("tick");
      
      motor.run(FORWARD);      // turn it on going forward
      delay(1000);

      Serial.print("tock");
      motor.run(BACKWARD);     // the other way
      delay(1000);
      
      Serial.print("tack");
      motor.run(RELEASE);      // stopped
      delay(1000);
    }



    //使用步進馬達 Step Motor



     `stepsPerRevolution` 必須設定為你的馬達實際上一圈會有多少步,如果是 28BYJ-48 5V DC 的話,規格上寫著,步進角為 `5.625 / 64`,因此這馬達轉一圈需要的步數是 `360 / (5.625 / 64)`,也就是 `4096` 步,這是一/二相激磁才會有的步數,Stepper 程式庫原始碼它是採二相激磁的實作方式,因此,使用這個 Stepper 程式庫實際上要設的 `stepsPerRevolution` 是 `2048` 步

    PWR Jumper 短路(套上)的話會對 Arduino UNO 板供電5V


    #include
    #include

    #define SERVO1_PWM 10
    #define SERVO2_PWM 9

    double passos_total = 2048; //Numero de passos para 1 rotacao total

    int porta_motor = 2; //1 para motor em M1/M2 e 2 para motor em M3/M4
    int angulo = 30; //Angulo de rotacao do eixo

    double numero_de_passos = 0; //Armazena o numero de passos que o motor vai girar

    AF_Stepper arduino(passos_total, porta_motor); //Define os parametros do motor
    Servo servo_1;

    void setup()
    {
    arduino.setSpeed(10); //Define a velocidade de rotacao
    Serial.begin(9600);
    servo_1.attach(SERVO1_PWM);
    }

    void loop()
    {
    //Calcula a quantidade de passos, baseado no angulo determinado
    numero_de_passos = angulo / (360 / passos_total);
    //170.67/步

    servo_1.write(0);
      delay(1000);
      servo_1.write(180);
      delay(2000);

    //Mostra no serial monitor o numero de passos calculados
    Serial.println(numero_de_passos);

    //Move o motor. Use FORWARD para sentido horario,
    //BACKWARD para anti-horario
    for(int i=0;i<=5;i++){
      arduino.step(numero_de_passos, FORWARD, SINGLE);
      arduino.release();
    }
    delay(2000);

    for(int i=0;i<=5;i++){
      arduino.step(numero_de_passos, BACKWARD, SINGLE);
      arduino.release();
    }
    delay(2000);
    }


    //---------------------------------------------------------

    #include
    AF_Stepper motor(48, 2);   //48表示每圈的步數(360度/步距角,48的步距角為7.5度)
                                                 //2表示第二路步進電機(共支持兩路)
    void setup() {
       Serial.begin(9600); // set up Serial library at 9600 bps
       Serial.println("Stepper test!");
       motor.setSpeed(10); // 10 rpm
       motor.step(100, FORWARD, SINGLE);
       motor.release();
       delay(1000);
    }
    void loop() {
       motor.step(100, FORWARD, SINGLE);
       motor.step(100, BACKWARD, SINGLE);
       motor.step(100, FORWARD, DOUBLE);
       motor.step(100, BACKWARD, DOUBLE);
       motor.step(100, FORWARD, INTERLEAVE);
       motor.step(100, BACKWARD, INTERLEAVE);
       motor.step(100, FORWARD, MICROSTEP);
       motor.step(100, BACKWARD, MI
    }