วันเสาร์ที่ 22 กรกฎาคม พ.ศ. 2560

Mobile app control Robot via App inventor+HC06 Bluetooth+Arduino









Bluetooth Module HC06

https://www.olimex.com/Products/Components/RF/BLUETOOTH-SERIAL-HC-06/resources/hc06.pdf





Wiring




HC06 to Arduino

RX ---> 11
TX ---> 10







Motor Wiring


Arduino to Wheel motor
2 ---> ENA
3 ---> IN1
4 ---> IN2
5 ---> IN3
6 ---> IN4
7 ---> ENB

Arduino to Arm motor
A0 ---> ENA
A1 ---> IN1
A2 ---> IN2
A3 ---> IN3
A4 ---> IN4
A5 ---> ENB





MIT Inventor App build 


Code

#include<SoftwareSerial.h>

char receivedChar;

int ENA=2;
int IN1=3;
int IN2=4;
int IN3=5;
int IN4=6;
int ENB=7;

int ENA1=A0;
int IN11=A1;
int IN12=A2;
int IN13=A3;
int IN14=A4;
int ENB1=A5;


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

 pinMode(ENA1,OUTPUT);//output
 pinMode(ENB1,OUTPUT);
 pinMode(IN11,OUTPUT);
 pinMode(IN12,OUTPUT);
 pinMode(IN13,OUTPUT);
 pinMode(IN14,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,LOW);//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,LOW);//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
  
  }
  
}




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

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