wiki:wade/embedded/arduino/example/temp

相關資料

  • arduino sparkfun

程式一:LED 矩陣測試程式

#define start_pin 2
#define end_pin 18
#define low_start_pin 10
#define low_end_pin 17
#define high_start_pin 2
#define high_end_pin 9
#define led_delay_time 150
#define button 2

boolean flag;

void setup()
{
  for (int i=start_pin; i<=end_pin; i++)
  {
    pinMode(i, OUTPUT);
  }
//  pinMode(button, INPUT);
//  digitalWrite(button, HIGH);
//  attachInterrupt(0, button_event, LOW);
  Serial.begin(9600);
  clean();
}

void loop()
{
  clean();
  delay(led_delay_time);
  for (int i=high_start_pin; i<=high_end_pin; i++)
  {
    Serial.print("[");
    Serial.print(i);
    Serial.println("]  ");
    digitalWrite(i, HIGH);
    for (int j=low_start_pin; j<=low_end_pin; j++)
    {
      Serial.print("  ");
      Serial.print(j);
      digitalWrite(j, LOW);
      delay(led_delay_time);
      digitalWrite(j, HIGH);
    }
    Serial.println();
    digitalWrite(i, LOW);
  }
}

void clean()
{
  Serial.println("clean");
  for (int i=low_start_pin; i<=low_end_pin; i++)
    digitalWrite(i, HIGH);
  for (int i=high_start_pin; i<=high_end_pin; i++)
    digitalWrite(i, LOW);
}

void button_event()
{
  delay(500);
  flag = !flag;
  while(flag)
  {
  }
}

程式二:產生「王」字

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */

void setup() {
  // Set Vcc Pin
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);  
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);  
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  
  // Set GND Pin
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
  digitalWrite(17, HIGH);
}

void loop() {
  
  // time 1
  digitalWrite(2, HIGH);   // HIGH
//  digitalWrite(3, HIGH);   // HIGH
//  digitalWrite(4, HIGH);   // HIGH
//  digitalWrite(5, HIGH);   // HIGH
//  digitalWrite(6, HIGH);   // HIGH
//  digitalWrite(7, HIGH);   // HIGH
//  digitalWrite(8, HIGH);   // HIGH
//  digitalWrite(9, HIGH);   // HIGH
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  digitalWrite(16, LOW);
  digitalWrite(17, LOW);
//  delay(300);
  clean();
  
    
  // time 2
//  digitalWrite(2, HIGH);   // HIGH
  digitalWrite(3, HIGH);   // HIGH
//  digitalWrite(4, HIGH);   // HIGH
//  digitalWrite(5, HIGH);   // HIGH
//  digitalWrite(6, HIGH);   // HIGH
//  digitalWrite(7, HIGH);   // HIGH
//  digitalWrite(8, HIGH);   // HIGH
//  digitalWrite(9, HIGH);   // HIGH
//  digitalWrite(10, LOW);
//  digitalWrite(11, LOW);
//  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
//  digitalWrite(14, LOW);
//  digitalWrite(15, LOW);
//  digitalWrite(16, LOW);
//  digitalWrite(17, LOW);
//  delay(300);
  clean();


  // time 3
//  digitalWrite(2, HIGH);   // HIGH
//  digitalWrite(3, HIGH);   // HIGH
  digitalWrite(4, HIGH);   // HIGH
//  digitalWrite(5, HIGH);   // HIGH
//  digitalWrite(6, HIGH);   // HIGH
//  digitalWrite(7, HIGH);   // HIGH
//  digitalWrite(8, HIGH);   // HIGH
//  digitalWrite(9, HIGH);   // HIGH
//  digitalWrite(10, LOW);
//  digitalWrite(11, LOW);
//  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
//  digitalWrite(14, LOW);
//  digitalWrite(15, LOW);
//  digitalWrite(16, LOW);
//  digitalWrite(17, LOW);
//  delay(300);
  clean();

  // time 4
//  digitalWrite(2, HIGH);   // HIGH
//  digitalWrite(3, HIGH);   // HIGH
//  digitalWrite(4, HIGH);   // HIGH
  digitalWrite(5, HIGH);   // HIGH
//  digitalWrite(6, HIGH);   // HIGH
//  digitalWrite(7, HIGH);   // HIGH
//  digitalWrite(8, HIGH);   // HIGH
//  digitalWrite(10, LOW);
//  digitalWrite(11, LOW);
//  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
//  digitalWrite(14, LOW);
//  digitalWrite(15, LOW);
//  digitalWrite(16, LOW);
//  digitalWrite(17, LOW);
//  delay(300);
  clean();

  // time 5
//  digitalWrite(2, HIGH);   // HIGH
//  digitalWrite(3, HIGH);   // HIGH
//  digitalWrite(4, HIGH);   // HIGH
//  digitalWrite(5, HIGH);   // HIGH
  digitalWrite(6, HIGH);   // HIGH
//  digitalWrite(7, HIGH);   // HIGH
//  digitalWrite(8, HIGH);   // HIGH
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  digitalWrite(16, LOW);
  digitalWrite(17, LOW);
  clean();

  // time 6
//  digitalWrite(2, HIGH);   // HIGH
//  digitalWrite(3, HIGH);   // HIGH
//  digitalWrite(4, HIGH);   // HIGH
//  digitalWrite(5, HIGH);   // HIGH
//  digitalWrite(6, HIGH);   // HIGH
  digitalWrite(7, HIGH);   // HIGH
//  digitalWrite(8, HIGH);   // HIGH
//  digitalWrite(10, LOW);
//  digitalWrite(11, LOW);
//  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
//  digitalWrite(14, LOW);
//  digitalWrite(15, LOW);
//  digitalWrite(16, LOW);
//  digitalWrite(17, LOW);
//  delay(300);
  clean();
//  
//  // time 7
//  digitalWrite(2, HIGH);   // HIGH
//  digitalWrite(3, HIGH);   // HIGH
//  digitalWrite(4, HIGH);   // HIGH
//  digitalWrite(5, HIGH);   // HIGH
//  digitalWrite(6, HIGH);   // HIGH
//  digitalWrite(7, HIGH);   // HIGH
  digitalWrite(8, HIGH);   // HIGH
//    digitalWrite(9, HIGH);   // HIGH
//  digitalWrite(10, LOW);
//  digitalWrite(11, LOW);
//  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
//  digitalWrite(14, LOW);
//  digitalWrite(15, LOW);
//  digitalWrite(16, LOW);
//  digitalWrite(17, LOW);
//  delay(300);
  clean();
//  

  // time 8
//  digitalWrite(2, HIGH);   // HIGH
//  digitalWrite(3, HIGH);   // HIGH
//  digitalWrite(4, HIGH);   // HIGH
//  digitalWrite(5, HIGH);   // HIGH
//  digitalWrite(6, HIGH);   // HIGH
//  digitalWrite(7, HIGH);   // HIGH
//  digitalWrite(8, HIGH);   // HIGH
  digitalWrite(9, HIGH);   // HIGH
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  digitalWrite(16, LOW);
  digitalWrite(17, LOW);
  clean();
}

void clean()
{
  delay(300);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
  digitalWrite(17, HIGH);
}

程式三 UART 轉 RS232

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

void loop()
{
  for (int i=0; i<=100; i++)
  {
    Serial.println(i);
    delay(200);
  }
}

程式四:由 PC 傳送資料至 Arduino

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

void loop()
{
  if(Serial.available())
  {
//    Serial.println(Serial.read());
    int val = Serial.read();
    if (val == XX)
    {
    }
    delay(300);
  }
}

程式五:點亮左上至右下的LED

#define start_pin 2
#define end_pin 18
#define low_start_pin 10
#define low_end_pin 17
#define high_start_pin 2
#define high_end_pin 9
#define led_delay_time 150
#define button 2
int delay_time= 0;

void setup()
{
  for (int i=start_pin; i<=end_pin; i++)
  {
    pinMode(i, OUTPUT);
  }
  clean();
}

void loop()
{
  // time 1
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  delay(delay_time);
  clean();
  delay(delay_time);
  
   // time 2
  digitalWrite(3, HIGH);
  digitalWrite(11, LOW);
  delay(delay_time);
  clean();
  delay(delay_time);
  
   // time 3
  digitalWrite(4, HIGH);
  digitalWrite(12, LOW);
  delay(delay_time);
  clean();
  delay(delay_time);
  
   // time 4
  digitalWrite(5, HIGH);
  digitalWrite(13, LOW);
  delay(delay_time);
  clean();
  delay(delay_time);
  
   // time 5
  digitalWrite(6, HIGH);
  digitalWrite(14, LOW);
  delay(delay_time);
  clean();
  delay(delay_time);
  
   // time 6
  digitalWrite(7, HIGH);
  digitalWrite(15, LOW);
  delay(delay_time);
  clean();
  delay(delay_time);
  
   // time 7
  digitalWrite(8, HIGH);
  digitalWrite(16, LOW);
  delay(delay_time);
  clean();
  delay(delay_time);
  
   // time 8
  digitalWrite(9, HIGH);
  digitalWrite(17, LOW);
  delay(delay_time);
  clean();
  delay(delay_time);
}

void clean()
{
  Serial.println("clean");
  for (int i=low_start_pin; i<=low_end_pin; i++)
    digitalWrite(i, HIGH);
  for (int i=high_start_pin; i<=high_end_pin; i++)
    digitalWrite(i, LOW);
}

程式六:七段顯示器

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  clean();
}

void loop()
{
  all();
  delay(500);
  clean();
  delay(500);
}

void all()
{
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
}

void clean()
{
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
}

程式七:led 霹靂車

void setup() {
  for (int i=0; i<=7; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }
  

}

void loop() 
{
  for (int i=0; i<=7; i++)
  {
    digitalWrite(i, HIGH);
  }

}

程式八:基本序列傳輸

void setup() {
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly: 
  int input1 = digitalRead(2);
  Serial.println(input1);
  delay(300);
}

程式九:類比輸入

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

}

void loop() {
  // put your main code here, to run repeatedly: 
  int val = analogRead(0) ;
  Serial.println(val);
  
}

程式十:按鍵輸入

int led = 13;

int lighttpye = 0;
int switchbutton=1;

void setup() { 
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);
  Serial.begin(9600);
  pinMode(led, OUTPUT); 
}
void loop() {

int input1 = digitalRead(2);
Serial.println(input1);
delay(300);

if(switchbutton!=input1){
  if(input1!=0){
    if(lighttpye!=0){
      digitalWrite(led, HIGH); 
      lighttpye=0;
    }else{
      digitalWrite(led, LOW); 
    lighttpye=1;
  }}

switchbutton=input1;

}
}

程式十一:x-y 搖捍帶按鍵

void setup() {
  pinMode(3, INPUT);
  digitalWrite(3, HIGH);
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly: 
  int val_x = analogRead(0);
  int val_y = analogRead(1);
  int input1 = digitalRead(3);
  Serial.print("sw: ");
  Serial.print(input1);
  Serial.print("   x: ");
  Serial.print(val_x);
  Serial.print("   y: ");
  Serial.println(val_y);
  delay(300);
  
}

程式十二:x-y搖桿跟led

void setup() {
  pinMode(3, INPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  Serial.begin(9600);


}

void loop() {
  // put your main code here, to run repeatedly: 
  int val_x = analogRead(0);
  int val_y = analogRead(1);
  int input1 = digitalRead(3);
  Serial.print("sw: ");
  Serial.print(input1);
  Serial.print("   x: ");
  Serial.print(val_x);
  Serial.print("   y: ");
  Serial.println(val_y);
  
  
  if ( 0 <=val_x && val_x < 128)
  {
//    Serial.println(" range 1");
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
    
  }
  if ( 128 <=val_x && val_x < 256)
  {
//    Serial.println(" range 2");
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  }
  if ( 256 <=val_x && val_x < 384)
  {
//    Serial.println(" range 3");
  }
  if ( 384 <=val_x && val_x < 512)
  {
//    Serial.println(" range 4");
  }
  if ( 512 <=val_x && val_x < 640)
  {
//    Serial.println(" range 5");
  }
  if ( 640 <=val_x && val_x < 768)
  {
//    Serial.println(" range 6");
  }
  if ( 768 <=val_x && val_x < 896)
  {
//    Serial.println(" range 7");
  }
  if ( 896 <=val_x && val_x < 1024)
  {
//    Serial.println(" range 8");
  }
  
  
  delay(300);
  
}

程式十三:led 矩陣作業一

void setup()
{
  for (int i = 2; i <= 17; i++)
  {
    pinMode(i, OUTPUT);
  }
  clean();
}

void loop()
{
  
}

void clean()
{
  for (int i = 2; i <= 9; i++)
  {
    digitalWrite(i, LOW);
  }
  for (int i = 10; i <= 17; i++)
  {
    digitalWrite(i, HIGH);
  }
}

程式十四:led 作業二

void setup()
{
  for (int i = 2; i <= 17; i++)
  {
    pinMode(i, OUTPUT);
  }
  clean();
}

void loop()
{
  //time 1
  clean();
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  
  //time 2
  clean();
  digitalWrite(3, HIGH);
  digitalWrite(11, LOW);
  
  //time 3
  clean();
  digitalWrite(4, HIGH);
  digitalWrite(12, LOW);
  
  //time 4
  clean();
  digitalWrite(5, HIGH);
  digitalWrite(13, LOW);
  
  //time 5
  clean();
  digitalWrite(6, HIGH);
  digitalWrite(14, LOW);
  
  //time 6
  clean();
  digitalWrite(7, HIGH);
  digitalWrite(15, LOW);
  
  //time 7
  clean();
  digitalWrite(8, HIGH);
  digitalWrite(16, LOW);
  
  //time 8
  clean();
  digitalWrite(9, HIGH);
  digitalWrite(17, LOW);
  
}

void clean()
{
  for (int i = 2; i <= 9; i++)
  {
    digitalWrite(i, LOW);
  }
  for (int i = 10; i <= 17; i++)
  {
    digitalWrite(i, HIGH);
  }
}

程式十五:RS232傳送與接收

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

void loop()
{
 
 
  if (Serial.available())
  {
    char c = Serial.read();
//    Serial.println(c)
    if (c == 'a')
    {
      digitalWrite(13, HIGH);
      delay(100);
      digitalWrite(13, LOW);
      delay(100);
      digitalWrite(13, HIGH);
      delay(100);
      digitalWrite(13, LOW);
      delay(100);
      digitalWrite(13, HIGH);
      delay(100);
      digitalWrite(13, LOW);
      delay(100);
    }
    
  }
}

程式十六:led 與 x_y 搖桿

void setup() {
  for (int i = 2; i <= 17; i++)
  {
    pinMode(i, OUTPUT);
  }
  clean();
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  int val_x = analogRead(4);
  int val_y = analogRead(5);
  Serial.print("   x: ");
  Serial.print(val_x);
  Serial.print("   y: ");
  Serial.println(val_y);
  
  
  if ( 0 <=val_x && val_x < 128)
  {
//    Serial.println(" range 1");
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  }
  if ( 128 <=val_x && val_x < 256)
  {
//    Serial.println(" range 2");
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  }
  if ( 256 <=val_x && val_x < 384)
  {
//    Serial.println(" range 3");
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  }
  if ( 384 <=val_x && val_x < 512)
  {
//    Serial.println(" range 4");
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  }
  if ( 512 <=val_x && val_x < 640)
  {
//    Serial.println(" range 5");
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(14, LOW);
  }
  if ( 640 <=val_x && val_x < 768)
  {
//    Serial.println(" range 6");
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  }
  if ( 768 <=val_x && val_x < 896)
  {
//    Serial.println(" range 7");
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  digitalWrite(16, LOW);
  }
  if ( 896 <=val_x && val_x < 1024)
  {
//    Serial.println(" range 8");
  digitalWrite(2, HIGH);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  digitalWrite(14, LOW);
  digitalWrite(15, LOW);
  digitalWrite(16, LOW);
  digitalWrite(17, LOW);
  }
  delay(10);
  clean();
}

void clean()
{
  for (int i = 2; i <= 9; i++)
  {
    digitalWrite(i, LOW);
  }
  for (int i = 10; i <= 17; i++)
  {
    digitalWrite(i, HIGH);
  }
}

Last modified 11 years ago Last modified on Jun 7, 2013, 4:22:14 PM

Attachments (5)