pierfraxxxx

CAR ARDUINO APP INVENTOR BT

Nov 21st, 2020 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  int A1A = 3;//define pin 3 for A1A
  3.  int A1B = 5;//define pin 5 for A1B
  4.  
  5.  int B1A = 4;//define pin 4 for B1A
  6.  int B1B = 6;//define pin 6 for B1B
  7.  
  8.  int PW = 120; //power motor
  9.  
  10. #include <SoftwareSerial.h>// import the serial library
  11.  
  12. SoftwareSerial Genotronex(10,11); // D10 -> TX, D11 -> RX
  13.  
  14. int BluetoothData;
  15.  
  16. void setup() {
  17.  
  18.        Genotronex.begin(9600);
  19.        Serial.begin(9600);
  20.      
  21.   pinMode(B1A,OUTPUT);// define pin as output
  22.   pinMode(B1B,OUTPUT);
  23.  
  24.   pinMode(A1A,OUTPUT);
  25.   pinMode(A1B,OUTPUT);  
  26.   motorA('O');  
  27.   delay(500);
  28. }
  29.  
  30. void loop() {
  31.  
  32.  
  33.    if (Genotronex.available()){
  34.   BluetoothData=Genotronex.read();
  35.    Serial.println(BluetoothData);}
  36.  
  37.     if ( BluetoothData == 50)   motorB('L');// Turn motor B to LEFT;
  38.     if ( BluetoothData == 51)   motorB('R');// Turn motor B to RIGHT;
  39.     if ( BluetoothData == 49)   motorB('O');// Turn motor B to OFF;
  40.     if ( BluetoothData == 52)   motorA('O');// Turn motor A to OFF;
  41.     if ( BluetoothData == 53)   motorA('S');// Turn motor A to SLOW;    
  42.     if ( BluetoothData == 54)   motorA('R');// Turn motor A to FAST;
  43.     if ( BluetoothData == 55)  {if (A1A == 3) {A1A = 5; A1B = 3;} else {A1A = 3; A1B = 5;}} // Reverse motor A;
  44. delay(5);
  45. }
  46.  
  47.  
  48. void motorA(char d)
  49. {
  50.   if(d =='R'){ PW = 180;
  51.     digitalWrite(A1A,LOW);
  52.     analogWrite(A1B,PW);
  53.   }else if (d =='S') { PW = 120;
  54.     digitalWrite(A1A,LOW);
  55.     analogWrite(A1B,PW);
  56.   }else{  
  57.     analogWrite(A1A,0);
  58.     analogWrite(A1B,0);    
  59.   }
  60. }// motorA end
  61.  
  62.  
  63. void motorB(char d)
  64. {
  65.     if(d =='R'){
  66.       digitalWrite(B1A,LOW);
  67.       digitalWrite(B1B,HIGH);
  68.       analogWrite(A1B,PW+10); //more power
  69.     }else if(d =='L'){
  70.       digitalWrite(B1A,HIGH);
  71.       digitalWrite(B1B,LOW);
  72.       analogWrite(A1B,PW+10); //more power    
  73.     }else{
  74.     // Turn motor OFF      
  75.       digitalWrite(B1A,LOW);
  76.       digitalWrite(B1B,LOW);    
  77.     }
  78.  
  79. }// motorB end
Advertisement
Add Comment
Please, Sign In to add comment