วันเสาร์ที่ 1 เมษายน พ.ศ. 2560

HC06+Inventor 2 +Arduino











Code



#include<SoftwareSerial.h>

char receivedChar;

int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=9;//connected to Arduino's port 9


SoftwareSerial mySerial(10, 11); // RX, TX



//RX on Bluetooth to TCX on Arduino through a voltage divider 50ohm from arduino and 100ohm to ground. This is so we can drop the the voltage down to 3.3v (roughly)

// Use blueterm on android to get messages



void setup()



 pinMode(ENA,OUTPUT);//output
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);
  
  mySerial.begin(9600); 
}



void loop() // run over and over



{

  


  while (!mySerial.available());   // stay here so long as COM port is empty

  receivedChar = mySerial.read();

  if (receivedChar == 'A') {

            //digitalWrite(ENA,LOW);
            // digitalWrite(ENB,LOW);// High is start driving car
              
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,LOW); 
             digitalWrite(IN2,HIGH);//setting motorA's directon
             digitalWrite(IN3,LOW);
             digitalWrite(IN4,HIGH);//setting motorB's directon
  }
              
  if (receivedChar == 'B') {

             // digitalWrite(ENA,LOW);
             // digitalWrite(ENB,LOW);// High is start driving car
              
              
                    
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,HIGH); 
             digitalWrite(IN2,LOW);//setting motorA's directon
             digitalWrite(IN3,HIGH);
             digitalWrite(IN4,LOW);//setting motorB's directon

                         
  }      
  if (receivedChar == 'C') 
  
              {

             // digitalWrite(ENA,LOW);
             // digitalWrite(ENB,LOW);// High is start driving car
  
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,HIGH); 
             digitalWrite(IN2,LOW);//setting motorA's directon
             digitalWrite(IN3,LOW);
             digitalWrite(IN4,HIGH);//setting motorB's directon

             

  } // if it is a 3 flash the LED

  if (receivedChar == 'D') {
  
              //digitalWrite(ENA,LOW);
              //digitalWrite(ENB,LOW);// High is start driving car
              
                          
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,LOW); 
             digitalWrite(IN2,HIGH);//setting motorA's directon
             digitalWrite(IN3,HIGH);
             digitalWrite(IN4,LOW);//setting motorB's directon


  } // if it is a 4 print out lots of sensor data


  if (receivedChar == 'E') {
  
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,LOW); 
             digitalWrite(IN2,LOW);//setting motorA's directon
             digitalWrite(IN3,LOW);
             digitalWrite(IN4,LOW);//setting motorB's directon
  
  }
  
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น