Advertisement
DrAungWinHtut

robot_tank.ino

May 8th, 2025
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  keyestudio Robot Car v2.0
  3.  lesson 14.2
  4.  bluetooth car
  5.  http://www.keyestudio.com
  6. */
  7.  
  8. //Array, used to store the data of pattern, can be calculated by yourself or obtained from the modulus tool
  9. unsigned char start01[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
  10. unsigned char front[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x12,0x09,0x12,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
  11. unsigned char back[] = {0x00,0x00,0x00,0x00,0x00,0x24,0x48,0x90,0x48,0x24,0x00,0x00,0x00,0x00,0x00,0x00};
  12. unsigned char left[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0x10,0x44,0x28,0x10,0x44,0x28,0x10,0x00};
  13. unsigned char right[] = {0x00,0x10,0x28,0x44,0x10,0x28,0x44,0x10,0x28,0x44,0x00,0x00,0x00,0x00,0x00,0x00};
  14. unsigned char STOP01[] = {0x2E,0x2A,0x3A,0x00,0x02,0x3E,0x02,0x00,0x3E,0x22,0x3E,0x00,0x3E,0x0A,0x0E,0x00};
  15. unsigned char clear[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  16. #define SCL_Pin  A5  //Set clock pin to A5
  17. #define SDA_Pin  A4  //Set data pin to A4
  18.  
  19. #define ML_Ctrl 13  //define direction control pin of left motor
  20. #define ML_PWM 11   //define PWM control pin of left motor
  21. #define MR_Ctrl 12  //define direction control pin of right motor
  22. #define MR_PWM 3    //define PWM control pin of right motor
  23.  
  24. char bluetooth_val; //save the value of Bluetooth reception
  25.  
  26. void setup(){
  27.   Serial.begin(9600);
  28.  
  29.   pinMode(SCL_Pin,OUTPUT);
  30.   pinMode(SDA_Pin,OUTPUT);
  31.   matrix_display(clear);    //Clear the display
  32.   matrix_display(start01);  //display start pattern
  33.  
  34.   pinMode(ML_Ctrl, OUTPUT);
  35.   pinMode(ML_PWM, OUTPUT);
  36.   pinMode(MR_Ctrl, OUTPUT);
  37.   pinMode(MR_PWM, OUTPUT);
  38. }
  39.  
  40. void loop(){
  41.   if (Serial.available())
  42.   {
  43.     bluetooth_val = Serial.read();
  44.     Serial.println(bluetooth_val);
  45.   }
  46.   switch (bluetooth_val)
  47.   {
  48.      case 'F':  //forward command
  49.         Car_front();
  50.         matrix_display(front);  // show forward design
  51.         break;
  52.      case 'B':  //Back command
  53.         Car_back();
  54.         matrix_display(back);  //show back pattern
  55.         break;
  56.      case 'L':  // left-turning instruction
  57.         Car_left();
  58.         matrix_display(left);  //show “left-turning” sign
  59.         break;
  60.      case 'R':  //right-turning instruction
  61.         Car_right();
  62.         matrix_display(right);  //display right-turning sign
  63.        break;
  64.      case 'S':  //stop command
  65.         Car_Stop();
  66.         matrix_display(STOP01);  //show stop picture
  67.         break;
  68.   }
  69. }
  70.  
  71. /**************The function of dot matrix****************/
  72. //this function is used for dot matrix display
  73. void matrix_display(unsigned char matrix_value[])
  74. {
  75.   IIC_start();
  76.   IIC_send(0xc0);  //Choose address
  77.  
  78.   for(int i = 0;i < 16;i++) //pattern data has 16 bits
  79.   {
  80.      IIC_send(matrix_value[i]); //data to convey patterns
  81.   }
  82.   IIC_end();   //end to convey data pattern
  83.  
  84.   IIC_start();
  85.   IIC_send(0x8A);  //display control, set pulse width to 4/16
  86.   IIC_end();
  87. }
  88. //The condition starting to transmit data
  89. void IIC_start()
  90. {
  91.   digitalWrite(SCL_Pin,HIGH);
  92.   delayMicroseconds(3);
  93.   digitalWrite(SDA_Pin,HIGH);
  94.   delayMicroseconds(3);
  95.   digitalWrite(SDA_Pin,LOW);
  96.   delayMicroseconds(3);
  97. }
  98. //transmit data
  99. void IIC_send(unsigned char send_data)
  100. {
  101.   for(char i = 0;i < 8;i++)  //Each byte has 8 bits
  102.   {
  103.       digitalWrite(SCL_Pin,LOW);  //pull down clock pin SCL Pin to change the signals of SDA    
  104. delayMicroseconds(3);
  105.       if(send_data & 0x01)  //set high and low level of SDA_Pin according to 1 or 0 of every bit
  106.       {
  107.         digitalWrite(SDA_Pin,HIGH);
  108.       }
  109.       else
  110.       {
  111.         digitalWrite(SDA_Pin,LOW);
  112.       }
  113.       delayMicroseconds(3);
  114.       digitalWrite(SCL_Pin,HIGH); //pull up clock pin SCL_Pin to stop transmitting data
  115.       delayMicroseconds(3);
  116.       send_data = send_data >> 1;  // Detect bit by bit, so move the data right by one
  117.   }
  118. }
  119. //The sign that data transmission ends
  120. void IIC_end()
  121. {
  122.   digitalWrite(SCL_Pin,LOW);
  123.   delayMicroseconds(3);
  124.   digitalWrite(SDA_Pin,LOW);
  125.   delayMicroseconds(3);
  126.   digitalWrite(SCL_Pin,HIGH);
  127.   delayMicroseconds(3);
  128.   digitalWrite(SDA_Pin,HIGH);
  129.   delayMicroseconds(3);
  130. }
  131. /*************the function to run motor**************/
  132. void Car_front()
  133. {
  134.   digitalWrite(MR_Ctrl,LOW);
  135.   analogWrite(MR_PWM,200);
  136.   digitalWrite(ML_Ctrl,LOW);
  137.   analogWrite(ML_PWM,200);
  138. }
  139. void Car_back()
  140. {
  141.   digitalWrite(MR_Ctrl,HIGH);
  142.   analogWrite(MR_PWM,200);
  143.   digitalWrite(ML_Ctrl,HIGH);
  144.   analogWrite(ML_PWM,200);
  145. }
  146. void Car_left()
  147. {
  148.   digitalWrite(MR_Ctrl,LOW);
  149.   analogWrite(MR_PWM,255);
  150.   digitalWrite(ML_Ctrl,HIGH);
  151.   analogWrite(ML_PWM,255);
  152. }
  153. void Car_right()
  154. {
  155.   digitalWrite(MR_Ctrl,HIGH);
  156.   analogWrite(MR_PWM,255);
  157.   digitalWrite(ML_Ctrl,LOW);
  158.   analogWrite(ML_PWM,255);
  159. }
  160. void Car_Stop()
  161. {
  162.   digitalWrite(MR_Ctrl,LOW);
  163.   analogWrite(MR_PWM,0);
  164.   digitalWrite(ML_Ctrl,LOW);
  165.   analogWrite(ML_PWM,0);
  166. }
  167. void Car_T_left()
  168. {
  169.   digitalWrite(MR_Ctrl,LOW);
  170.   analogWrite(MR_PWM,255);
  171.   digitalWrite(ML_Ctrl,LOW);
  172.   analogWrite(ML_PWM,180);
  173. }
  174. void Car_T_right()
  175. {
  176.   digitalWrite(MR_Ctrl,LOW);
  177.   analogWrite(MR_PWM,180);
  178.   digitalWrite(ML_Ctrl,LOW);
  179.   analogWrite(ML_PWM,255);
  180. }
  181.   //****************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement