Advertisement
zsmate13

Arduino 433Mhz Robot Car - Remote v2.1

May 22nd, 2020
2,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Robot Car remote control v2.1
  3.  
  4.   This is one of my first Arduino projects.
  5.   Code for the car is available at:
  6.   https://pastebin.com/eDuD2shA
  7.   Enjoy!
  8.  
  9.   The circuit:
  10.  
  11.    433Mhz RF trasmitter:
  12.  ** DAT - pin 12
  13.  
  14.    Joystick module:
  15.  ** VRx - pin A6
  16.  ** VRy - pin A7
  17.    
  18.    Push button:
  19.  ** button - pin A2
  20.  
  21.   created  1 Febr 2020
  22.   modified 23 May 2020
  23.   by Mate Z.
  24.  
  25. */
  26.  
  27. #include <RH_ASK.h> //Radio Head RF library
  28. #include <SPI.h> //previous one needs it
  29. #include <string.h>
  30.  
  31. RH_ASK rf_driver;
  32.  
  33. int Xvalue;
  34. int Yvalue;
  35. int Bpress;
  36. int Brel = 1;
  37. char message[6];
  38. char prevMessage[6];
  39. int maxSpeed = 100;
  40. int currentSpeed = 0;
  41.  
  42.  
  43. #define Xpin A6
  44. #define Ypin A7
  45. #define Bpin A2
  46.  
  47. void setup() {
  48.   // put your setup code here, to run once:
  49.   rf_driver.init();
  50.   pinMode(LED_BUILTIN, OUTPUT);
  51.   pinMode(Xpin, INPUT);
  52.   pinMode(Ypin, INPUT);
  53.   pinMode(Bpin, INPUT);
  54.   Serial.begin(9600);
  55.  
  56. }
  57.  
  58. void maxSpeedChg(){
  59.   if(maxSpeed!=250) maxSpeed+=50;
  60.   else maxSpeed=100;
  61. }
  62.  
  63. void sendMsg(const String& output){
  64.   char *message = output.c_str();
  65.  
  66.   if(strcmp(message, prevMessage)!=0 || (millis()%1000 < 3 && message[0] != 'D')){
  67.     rf_driver.send((uint8_t *)message, strlen(message));
  68.     rf_driver.waitPacketSent();
  69.     Serial.print("Transmitting code:\t");
  70.     Serial.println(message);
  71.     strcpy(prevMessage, message);
  72.   }
  73. }
  74.  
  75.  
  76. void loop() {
  77.   // put your main code here, to run repeatedly:
  78.   Xvalue=analogRead(Xpin)-525;
  79.   Yvalue=analogRead(Ypin)-525;
  80.   Bpress=digitalRead(Bpin);
  81.  
  82.   if(millis()%250==0){
  83.     Serial.print("Joystick data: ");
  84.     Serial.print("X: ");
  85.     Serial.print(Xvalue);
  86.     Serial.print(" Y: ");
  87.     Serial.print(Yvalue);
  88.     Serial.print("\tButton: ");
  89.     Serial.println(Bpress);
  90.   }
  91.   String output;
  92.   if(Bpress && Brel){
  93.     maxSpeedChg();
  94.     Serial.print("speed change: ");
  95.     Serial.println(maxSpeed);
  96.     Brel = 0;
  97.   }
  98.   else{
  99.     if(!Bpress) Brel = 1;
  100.     if(abs(Yvalue)<abs(Xvalue)){
  101.       if (Xvalue > 120){ //FWD
  102.         currentSpeed = map(sqrt(Xvalue), 10, sqrt(500), 60, maxSpeed);
  103.         output = 'F';
  104.       }
  105.       else if (Xvalue < -120){ //BWD
  106.         currentSpeed = map(sqrt(abs(Xvalue)), 10, sqrt(525), 60, maxSpeed);
  107.         output = 'B';
  108.       }
  109.       else{ //DEF
  110.         currentSpeed = 0;
  111.         output = 'D';
  112.       }
  113.     }
  114.     if(abs(Xvalue)<abs(Yvalue)){ //RGT
  115.       if (Yvalue > 120){
  116.         currentSpeed = map(sqrt(Yvalue), 10, sqrt(500), 80, maxSpeed);
  117.         output = 'R';
  118.       }
  119.       else if (Yvalue < -120){ //LFT
  120.         currentSpeed = map(sqrt(abs(Yvalue)), 10, sqrt(525), 80, maxSpeed);
  121.         output = 'L';
  122.       }
  123.       else{  //DEF
  124.         currentSpeed = 0;
  125.         output = 'D';
  126.       }
  127.     }
  128.    
  129.   }
  130.   //Serial.println(output);
  131.   if(strlen(output.c_str())==0) output = 'D';
  132.   if(currentSpeed<100){
  133.     output += '0';
  134.     if(currentSpeed<10) output += '0';
  135.   }
  136.   output += String(currentSpeed);
  137.   sendMsg(output);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement