2017年1月10日 星期二

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);
    }
  }
}

沒有留言:

張貼留言