2016年3月28日 星期一

利用 手機控制 8 LED 4種功能-----左移 右移 左右移 閃爍 (EX3-2)

MIT  AI2 






程式





//利用 手機控制 8 LED 4種功能

#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial BT(11,10); //define serial BT  RX=11, TX=10
//SoftwareSerial mySerial (rxPin, txPin);
const byte LEDPin[8] = {2,3,4,5,6,7,8,9};
int i=0 , j=0 ;
void setup() 
{
  for (j=0; j<8; j++) 
  pinMode(LEDPin[j],OUTPUT);
  Serial.begin(9600);
  BT.begin(9600);
}

//=============================
void loop()
{
      
  if(BT.available()) //received BT data
  {
    int LED = BT.read();
    Serial.print("Received BT cmd: ");
    if (LED==0)
     {     
           Serial.println(LED);
           LED1_Function();
     }
     else if (LED==2)     
     { 
           Serial.println(LED);
           LED2_Function();
     }
     else if (LED==4)
      { 
           Serial.println(LED);
           LED3_Function();
      }
      else if (LED==6) 
      {
           Serial.println(LED);
           LED4_Function();
      }

}  //if

}   //loop


//=========================================

void LED1_Function() {
  Serial.println("LED1");
  for(j=0;j<=4;j++){
  for (i=0; i<=8; i++)
  {
    digitalWrite(LEDPin[i], HIGH);
    delay (50);
    digitalWrite(LEDPin[i], LOW);
    delay(50);
  }  // for i
   Serial.println(j);
  }  //for j
  BT.write(0x11); 



//===============================
void LED2_Function() {
  Serial.println("LED2"); 
 for (j=0;j<=4;j++) { 
 for (i=7; i>=0 ; i--)
  {
    digitalWrite(LEDPin[i], HIGH);
    delay (50);
    digitalWrite(LEDPin[i], LOW);
    delay(50);
  }  // for i
  Serial.println(j); 
 }  // for j
 BT.write(0x21); 

}

//===============================
void LED3_Function() {
 for(j=0;j<=4;j++){  
 for(i = 0;i<=8;i++){
  digitalWrite(LEDPin[i],HIGH);
  delay(50);
  digitalWrite(LEDPin[i],LOW);
}  

for(i=8;i>=0;i--){
  digitalWrite(LEDPin[i],HIGH);
  delay(50);
  digitalWrite(LEDPin[i],LOW) ;
}
  Serial.println(j); 
 } //for j
 BT.write(0x31);
 Serial.println("LED3");
}

//===============================
void LED4_Function() {
  for (j=0;j<=4;j++) {
   for ( i=0; i<=8 ; i++) {
    digitalWrite(LEDPin[i], HIGH);
   }
    delay (250);
    
   for ( i=0; i<=8 ; i++) {
   digitalWrite(LEDPin[i], LOW);
   }
   
   delay(250);
   
   Serial.println(j); 
  } // for j
   BT.write(0x41);  
   Serial.println("LED4");
}  

//================================

2016年3月27日 星期日

LED 交替閃爍

//LED SWAP交替閃爍
const byte LEDPin[8] = {6,7,8,9,10,11,12,13};
const byte LEDs[8] = {0,0,0,0,1,1,1,1};
int i=0 , j=0 ;

void setup() 
{
  for (j=0; j<8; j++) 
  pinMode(LEDPin[j],OUTPUT);
}


//=============================

void loop() {
  {
   for (j=0; j<8 ; j++)
    digitalWrite(LEDPin[j], LEDs[j]);
    delay (250);
    
   for (i=7; i>=0 ; i--)
   digitalWrite(LEDPin[i], LEDs[7-i]);
    delay(250);
  }  // for
} //loop

//LED 同時閃爍


//LED  同時閃爍

const byte LEDs[] = {6,7,8,9,10,11,12,13};
const byte total = sizeof(LEDs);

void setup() 
{
  for (byte j=0; j<=total; j++) 
  {
    pinMode(LEDs[j], OUTPUT);
  }
}


//=============================

void loop() {
  {
   for (byte j=0; j<=total ; j++)
    digitalWrite(LEDs[j], HIGH);
    delay (250);
    
   for (byte j=0; j<=total ; j++)
   digitalWrite(LEDs[j], LOW);
    delay(250);
  }  // for
} //loop

// LED 左右移 跑馬燈

// LED 左右移  跑馬燈

const int ledCount=8;
int ledPins[] = {6,7,8,9,10,11,12,13};

void setup(){
for(int thisLed=0; thisLed < ledCount;  thisLed++) {
  pinMode(ledPins[thisLed],OUTPUT);
}
}

void loop(){
for(int thisLed = 0;thisLed <=ledCount;thisLed++){
  digitalWrite(ledPins[thisLed],HIGH);
  delay(600);
  digitalWrite(ledPins[thisLed],LOW);
}  

for(int thisLed=ledCount;thisLed>=0;thisLed--){
  digitalWrite(ledPins[thisLed],HIGH);
  delay(600);
  digitalWrite(ledPins[thisLed],LOW) ;
}
delay(1000);
}  //loop

//LED 右移 跑馬燈

//LED 右移 跑馬燈
const byte LEDs[] = {0,1,2,3,4,5,6,7};
const byte total = sizeof(LEDs);

void setup() 
{
  for (byte j=7; j<=0; j--) 
  {
    pinMode(LEDs[j], OUTPUT);
  }
}


//=============================

void loop() {
  for (byte j=7; j>=0 ; j--)
  {
    digitalWrite(LEDs[j], HIGH);
    delay (250);
    digitalWrite(LEDs[j], LOW);
    delay(250);
  }  // for

Arduino:stk500_getsync(): not in sync

源自於

http://yehnan.blogspot.tw/2014/10/arduinostk500getsync-not-in-sync.html



Arduino:stk500_getsync(): not in sync

燒錄時,若看到錯誤訊息「avrdude: stk500_getsync(): not in sync: resp=0x00」,雖然知道這代表著電腦端的燒錄程式avrdude不能與微控制器內的bootloader進行溝通,但卻無法知曉錯誤的源頭在哪,因為可能出錯的地方太多了。

底下列出可能導致此錯誤的原因,以及解決辦法:

原因:「工具-板子(Tools-Board)」與「工具-序列埠(Tools-Serial Port)」沒選好
辦法:確認你的板子到底是哪一塊,確認它所連接的序列埠是哪一個,重新設定正確的值

原因:序列埠被佔用了
辦法:關閉佔用序列埠的軟體,或電腦重開機

原因:驅動程式壞了、沒裝好
辦法:重新安裝驅動程式

原因:零件影響了燒錄所需的線路,譬如Uno板燒錄時須經由腳位0與1的線路
辦法:燒錄時拔除相關接線、暫時先拔掉擴充板

原因:舊板子與某些較小型的板子,需要自己按板子上的Reset鍵
辦法:觀看avrdude燒錄時輸出的訊息,在適當的時機按下Reset鍵;多試幾次,嘗試在不同的時間點按下

原因:若是自己買「USB轉Serial介面」模組再加上微控制器晶片,有可能TX與RX接反了
辦法:反過來接試試看

原因:微控制器裡的bootloader壞了,或根本沒有bootloader
辦法:找個ISP燒錄器,重新燒錄bootloader

原因:不明
辦法:電腦重開機

原因:不明
辦法:重新啟動Arduino IDE

原因:不明
辦法:重新拔插USB連接線

原因:不明
辦法:換插別的USB埠

原因:不明
辦法:改用USB集線器或改為直接插電腦的USB埠

原因:不明
辦法:換條USB線

//左移 跑馬燈

//左移 跑馬燈
//Shift Left LED  
const byte LEDs[] = {0,1,2,3,4,5,6,7};
const byte total = sizeof(LEDs);

void setup() 
{
  for (byte i=0; i<total; i++) 
  {
    pinMode(LEDs[i], OUTPUT);
  }
}


//=============================

void loop() {
  for (byte i=0; i<total; i++)
  {
    digitalWrite(LEDs[i], HIGH);
    delay (250);
    digitalWrite(LEDs[i], LOW);
    delay(250);
  }  // for
} //loop


在電腦上 Serial Monitor 依序輸出0~100

//下面的程式將會在電腦上依序輸出0~100

// 範例程式:

int x=0; //宣告正整數變數x初始值為0(全域變數)
void setup(){
Serial.begin(9600); //設定鮑率9600來與電腦連結
}


void loop(){ //主程式開始
 x+=1; //不斷的累加x
if(x>100){ //判斷x是否超過100
x=0; //將x值歸0
}
else
{
 Serial.println(x); //在螢幕上顯示出x值


if (x>1000)  x=0 ;

 delay(20);
}

LED digital Blink

//LED_1    ON, OFF  delay 500ms

int LEDPin=13;

void setup()
{
  pinMode(LEDPin,OUTPUT); 
}

void loop()

{
 digitalWrite(LEDPin , HIGH);
 delay(500);
 digitalWrite(LEDPin , LOW);
 delay(500);
 }

範例程式,會將x不斷累加但僅在x=100 、200 、300時在螢幕輸出x數值

l本範例程式,會將x不斷累加但僅在x=100 200300時在螢幕輸出x數值
範例程式:

int x=0;   //宣告變數(全域變數)
void setup() 
{
  Serial.begin(9600);
}

void loop() 
{

 x+=1;     //不斷的累加1 x = x + 1 , x++ 意義都一樣
 switch(x){  //判定x值,當x值有相對應的case時執行該內容
  case 100:
       Serial.println(x);  //在螢幕上顯示出x值
       break;
  case 200:
      Serial.println(x);
      break;
  case 300:
      Serial.println(x);
      break;
  default:
      //可有可無,若有,當不合以上條件時執行
      break ;  
  }  //switch
  delay (5);
  if (x>500)  x=0;
}  //loop



寫一個 9x9 乘法表 swtch.... case

//4)利用  switch …case  Serial.print  Serial.println 指令


void setup()
{
 Serial.begin(9600); 
}
void loop()
{
 for (int i=1 ;i<=9 ;i++)
  {
    switch (i) {
    case 1 :  {
     for (int j=1 ; j<=9;j++)
     {
      int x=1*j; 
      Serial.print ("1") ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }  //for
     break;
  } //case
  
    case 2 :  {
     for (int j=1 ; j<=9;j++)
     {
      int x=2*j; 
      Serial.print ("2") ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }  //for
     break;
  } //case
  
   case 3 :  {
     for (int j=1 ; j<=9;j++)
     {
      int x=3*j; 
      Serial.print ("3") ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }  //for
     break;
  } //case

   case 4 :  {
     for (int j=1 ; j<=9;j++)
     {
      int x=4*j; 
      Serial.print ("4") ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }  //for
     break;
  } //case
   
  case 5 :  {
     for (int j=1 ; j<=9;j++)
     {
      int x=5*j; 
      Serial.print ("5") ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }  //for
     break;
  } //case

    case 6 :  {
    for (int j=1 ; j<=9;j++)
     {
      int x=6*j; 
      Serial.print ("6") ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }  //for
     break;
   } //case


    case 7 :  {
    for (int j=1 ; j<=9;j++)
     {
      int x=7*j; 
      Serial.print ("7") ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }  //for
     break;
  } //case

   case 8 :  {
    for (int j=1 ; j<=9;j++)
     {
      int x=8*j; 
      Serial.print ("8") ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }  //for
     break;
  } //case

    default :  {
    for (int j=1 ; j<=9;j++)
     {
      int x=9*j; 
      Serial.print ("9") ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }  //for 
     
    } //case
     
 //============================
  }  //switch
  
  }   //for
  
}  //loop

寫一個 9x9 乘法表 do while

// 3)利用 do    while 與  Serial.print  Serial.println 指令

// 設定引用函數庫

// 參數設定


//Setup 
void setup()
{
 Serial.begin(9600);
}


//Loop
void loop()
{
  int x=1;  
   do  {
    int y=1;
    do {
       int  i=x*y;
      Serial.print (x) ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (y) ;
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (i) ;
      delay (500);  
      y++;
     }  while (y<10);    //do  while
     x++;


  }  while  (x<10) ; //do while
 
}

寫一個 9x9 乘法表 while

//2)利用  while 與  Serial.print  Serial.println 指令

// 設定引用函數庫

// 參數設定


//Setup 

void setup()
{
 Serial.begin(9600); 
}


//Loop

void loop()
{
  int x=1;   
  while (x<=9) { 
    int y=1; 

    while (y<=9) { 
        int  i=x*y; 
      Serial.print (x) ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (y) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (i) ; 
      delay (500);    
      y++; 
     }   //while
     x++;
  } //while
  
}  

寫一個 9x9 乘法表 for

// 1)利用  for Serial.print  Serial.println指令

int x=0;
void setup()
{
 Serial.begin(9600); 
}
void loop()
{
 for (int i=1 ;i<=9 ;i++)
  {
    for (int j=1 ; j<=9;j++)
     {
      x=i*j; 
      Serial.print (i) ;
      Serial.print ("\t") ;
      Serial.print ("*") ;
      Serial.print (j) ; 
      Serial.print ("\t") ;
      Serial.print ("=") ;
      Serial.print ("\t") ;
      Serial.println (x) ; 
      delay (500);     
     }
  }
}  

Hello world! for Arduino.


/*
  *Hello World! *
  * This is the Hello World! for Arduino.
   * It shows how to send data to the computer
*/
void setup()   // run once, when the sketch starts
{
Serial.begin(9600);
}  // set up Serial library at 9600 bps
void  loop()  // run over and over again
{
Serial.println("Hello world! for Arduino.  ");
// prints hello with ending line break }
Delay (500);    //delay 500ms

 }

MQTT Explorer 與 Node-Red 介面的實驗

 MQTT Explorer 與 Node-Red 介面的實驗 MQTT EXplorer 與 Node-Red介面的設定 (1) 設定  MQTT EXplorer Client    (2)        Node-Red LED ON  --> MQTT Explor...