跳到主要內容區

 

研究方法_自造家作業

 
 
\\伺服馬達作業
 
#include <Servo.h> //引入lib
 
Servo motor; //定義一顆伺服馬達名為motor
 
void setup() 
{
motor.attach(5); //定義motor的信號來自pin11
}
 
void loop() 
{
motor.write(90); //寫入motor角度
}
 
 
----------------------------------------------------------------------------------------------------
 
\\電位器作業
#include <Servo.h>
Servo motor;
int degree0 = A0; //宣告一個變數可變電阻的值
int degree1; //宣告一個變數記錄馬達角度 
 
void setup()
{
motor.attach(5); 
}
 
void loop()
{
 
degree0 = analogRead(A0);
degree1 = map(degree0, 0, 1023, 0, 179); 
// degree0 的值在0~1023之間
// 但Servo僅能轉180度,所以要用map轉換
 
motor.write(degree1);
 
delay(100);
 
}
 
----------------------------------------------------------------------------------------------------
 
\\可變電阻調光作業
 
 
 
 
----------------------------------------------------------------------------------------------------
 
\\光敏電阻實驗
 
 
----------------------------------------------------------------------------------------------------
 
\\溫度感測器實驗
 
#define LM35 A0
 
int val = 0;
float temp = 0;
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  val = analogRead(LM35);
  temp = val * 0.48876;
  Serial.print("temp = ");
  Serial.println(temp);
  delay(1000);
}
----------------------------------------------------------------------------------------------------
 
\\七段顯示器作業
 
 
 
#define SEG_a 2
#define SEG_b 3
#define SEG_c 4
#define SEG_d 5
#define SEG_e 6
#define SEG_f 7
#define SEG_g 8
#define SEG_h 9
 
 
unsigned char table[10][8] =
{
{0, 0, 1, 1, 1, 1, 1, 1}, //0
{0, 0, 0, 0, 0, 1, 1, 0}, //1
{0, 1, 0, 1, 1, 0, 1, 1}, //2
{0, 1, 0, 0, 1, 1, 1, 1}, //3
{0, 1, 1, 0, 0, 1, 1, 0}, //4
{0, 1, 1, 0, 1, 1, 0, 1}, //5
{0, 1, 1, 1, 1, 1, 0, 1}, //6
{0, 0, 0, 0, 0, 1, 1, 1}, //7
{0, 1, 1, 1, 1, 1, 1, 1}, //8
{0, 1, 1, 0, 1, 1, 1, 1} //9
};
 
void setup()
{
pinMode(SEG_a,OUTPUT);
pinMode(SEG_b,OUTPUT);
pinMode(SEG_c,OUTPUT);
pinMode(SEG_d,OUTPUT);
pinMode(SEG_e,OUTPUT);
pinMode(SEG_f,OUTPUT);
pinMode(SEG_g,OUTPUT);
pinMode(SEG_h,OUTPUT);
}
 
void loop()
{
unsigned char i;
 
for( i = 0 ; i < 10 ; i++)
{
digitalWrite(SEG_a,table[i][7]);
digitalWrite(SEG_b,table[i][6]);
digitalWrite(SEG_c,table[i][5]);
digitalWrite(SEG_d,table[i][4]);
digitalWrite(SEG_e,table[i][3]);
digitalWrite(SEG_f,table[i][2]);
digitalWrite(SEG_g,table[i][1]);
digitalWrite(SEG_h,table[i][0]);
 
delay(1000);
 
}
 
----------------------------------------------------------------------------------------------------
 
/*
L9110S馬達控制晶片實驗
 
 
 
 
4.目標讓小車依序做出前進、後退、順時針旋轉、逆時針旋轉四種動作

*/

int LA=10;
int LB=11;
int RA=6;
int RB=5;
 
void setup() {
  pinMode(RA, OUTPUT); 
  pinMode(RB, OUTPUT);
  pinMode(LA, OUTPUT);
  pinMode(LB, OUTPUT);
}
 
void loop() { 
  
  //前進
  analogWrite(RA,0);
  analogWrite(RB,200); 
  analogWrite(LA,200);
  analogWrite(LB,0);
  delay(2000); 
  stop();
  
  //後退
  analogWrite(RA,200);
  analogWrite(RB,0); 
  analogWrite(LA,0);
  analogWrite(LB,200);
  delay(2000); 
  stop();
  
  //逆時針原地轉
  analogWrite(RA,0);
  analogWrite(RB,200); 
  analogWrite(LA,0);
  analogWrite(LB,200);
  delay(2000); 
  stop();
  
  //順時針原地轉
  analogWrite(RA,200);
  analogWrite(RB,0); 
  analogWrite(LA,200);
  analogWrite(LB,0);
  delay(2000); 
  stop();
 
 
void stop() {
  analogWrite(RA,0);
  analogWrite(RB,0);
  analogWrite(LA,0);
  analogWrite(LB,0);
  delay(300); 
}
 
----------------------------------------------------------------------------------------------------
 
/*
L9110S馬達控制晶片實驗
 
 
 
 
4.目標讓小車依序做出前進、後退、順時針旋轉、逆時針旋轉四種動作

*/

int LA=10;
int LB=11;
int RA=6;
int RB=5;
 
void setup() {
  pinMode(RA, OUTPUT); 
  pinMode(RB, OUTPUT);
  pinMode(LA, OUTPUT);
  pinMode(LB, OUTPUT);
}
 
void loop() { 
  
  //前進
  analogWrite(RA,0);
  analogWrite(RB,200); 
  analogWrite(LA,200);
  analogWrite(LB,0);
  delay(2000); 
  stop();
  
  //後退
  analogWrite(RA,200);
  analogWrite(RB,0); 
  analogWrite(LA,0);
  analogWrite(LB,200);
  delay(2000); 
  stop();
  
  //逆時針原地轉
  analogWrite(RA,0);
  analogWrite(RB,200); 
  analogWrite(LA,0);
  analogWrite(LB,200);
  delay(2000); 
  stop();
  
  //順時針原地轉
  analogWrite(RA,200);
  analogWrite(RB,0); 
  analogWrite(LA,200);
  analogWrite(LB,0);
  delay(2000); 
  stop();
 
 
void stop() {
  analogWrite(RA,0);
  analogWrite(RB,0);
  analogWrite(LA,0);
  analogWrite(LB,0);
  delay(300); 
}
 
----------------------------------------------------------------------------------------------------
 
/*
藍牙遙控車實驗
 
0.手機安裝藍牙遙控程式 (android手機)
 
 
 
 
4.目標讓小車依序做出前進、後退、順時針旋轉、逆時針旋轉四種動作

*/

#include <SoftwareSerial.h>    // 引用串列傳輸的函式庫

// 定義連接藍牙模組的序列埠

SoftwareSerial BT(8, 9);    // 接收腳tx, 傳送腳rx,指UNO腳位

int val;                     // 儲存接收自藍芽模組所傳輸資料的變數

 

int LA=10; //定義馬達使用L9110

int LB=11;

int RA=6;

int RB=5;

 

int carSpeed;   //設定車速,即馬達的PWM值

 

void setup() {

  Serial.begin(9600);   // 與電腦序列埠連線

  Serial.println("BT is ready!");

 

  // 設定藍牙模組的連線速率

  BT.begin(9600);

  carSpeed=100;                  //預設車行(馬達)速度

  pinMode(RA, OUTPUT); 

  pinMode(RB, OUTPUT);

  pinMode(LA, OUTPUT);

  pinMode(LB, OUTPUT);

 

}

 

void loop() {

   // 若收到藍牙模組的資料,則設定給動作變數

  if (BT.available()) {

    val = BT.read();

    

    //判斷變數所對應的動作

  switch(val)

  {

    case 'f':   //小車前進

    ???????????????

    break;

 

    case 'b':  //小車後退

    ???????????????

    break;

 

    case 's':   //小車停止

    ???????????????

    break;

 

    case 'r':   //小車右轉,降低小車轉速

    ???????????????

    break;

 

    case 'l':    //小車左轉,降低小車轉速

    ???????????????

    break;

 

      

    case 'F':   //設定小車車速為高速

     carSpeed=200;

    break;

 

    case 'L':   //設定小車車速為低速

     carSpeed=100;

    break;

    

    default:

    break;

      }

      

  }

}

----------------------------------------------------------------------------------------------------
 

 

瀏覽數: