Advertisement
zsmate13

Arduino 433Mhz Robot Car v2.1

May 22nd, 2020
2,295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Robot Car v2.1
  3.  
  4.   This is one of my first Arduino projects.
  5.   Code for the remote control is available at:
  6.   https://pastebin.com/98dhpUns
  7.   Enjoy!
  8.  
  9.   The circuit:
  10.  
  11.    433Mhz RF receiver:
  12.  ** DAT - pin 11
  13.  
  14.    L298N H-Bridge:
  15.  ** ENA, ENB - pin 5
  16.  ** IN1 - pin 6
  17.  ** IN2 - pin 7
  18.  ** IN3 - pin 8
  19.  ** IN4 - pin 9
  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.  
  30. RH_ASK rf_driver;
  31.  
  32. //L298N control pins
  33. const int LeftMotorForward = 8; //orange wire
  34. const int LeftMotorBackward = 9; //yellow wire
  35. const int RightMotorForward = 7; //red wire
  36. const int RightMotorBackward = 6; //brown wire
  37. const int SpeedControl = 5; //white wire
  38.  
  39. int noSigCount=0;
  40. int noSigTime=0;
  41. int prevmotorspeed = 0;
  42.  
  43. //moving functions
  44. void setMotorSpeed(int spd){
  45.     analogWrite(SpeedControl, spd);
  46. }
  47.  
  48. void stopMoving(){
  49.   digitalWrite(LeftMotorForward, LOW);
  50.   digitalWrite(LeftMotorBackward, LOW);
  51.   digitalWrite(RightMotorForward, LOW);
  52.   digitalWrite(RightMotorBackward, LOW);
  53. }
  54.  
  55. void goForward(){
  56.   digitalWrite(LeftMotorForward, HIGH);
  57.   digitalWrite(LeftMotorBackward, LOW);
  58.   digitalWrite(RightMotorForward, HIGH);
  59.   digitalWrite(RightMotorBackward, LOW);
  60. }
  61.  
  62. void goBackward(){
  63.   digitalWrite(LeftMotorForward, LOW);
  64.   digitalWrite(LeftMotorBackward, HIGH);
  65.   digitalWrite(RightMotorForward, LOW);
  66.   digitalWrite(RightMotorBackward, HIGH);
  67. }
  68.  
  69. void turnRight(){
  70.   digitalWrite(LeftMotorForward, HIGH);
  71.   digitalWrite(LeftMotorBackward, LOW);
  72.   digitalWrite(RightMotorForward, LOW);
  73.   digitalWrite(RightMotorBackward, HIGH);
  74. }
  75.  
  76. void turnLeft(){
  77.   digitalWrite(LeftMotorForward, LOW);
  78.   digitalWrite(LeftMotorBackward, HIGH);
  79.   digitalWrite(RightMotorForward, HIGH);
  80.   digitalWrite(RightMotorBackward, LOW);
  81. }
  82.  
  83. void setup() {
  84.   // put your setup code here, to run once:
  85.   pinMode(LED_BUILTIN, OUTPUT);
  86.   pinMode(LeftMotorForward, OUTPUT);
  87.   pinMode(LeftMotorBackward, OUTPUT);
  88.   pinMode(RightMotorForward, OUTPUT);
  89.   pinMode(RightMotorBackward, OUTPUT);
  90.  
  91.   rf_driver.init();
  92.  
  93.   Serial.begin(9600);
  94.   Serial.println("Arduino booted up");
  95.  
  96.   for(int i=0; i<5; ++i){
  97.     delay(300);
  98.     digitalWrite(LED_BUILTIN, HIGH);
  99.     delay(300);
  100.     digitalWrite(LED_BUILTIN, LOW);
  101.   }
  102.  
  103.   setMotorSpeed(50);
  104.  
  105. }
  106.  
  107. void loop() {
  108.   // put your main code here, to run repeatedly:
  109.  
  110.   uint8_t buf[4];
  111.   uint8_t buflen = sizeof(buf);
  112.  
  113.  
  114.   if (rf_driver.recv(buf, &buflen)){
  115.    digitalWrite(LED_BUILTIN, HIGH);
  116.    Serial.print("Incoming transmission: '");
  117.    for(int i = 0; i < 4; i++)
  118.    Serial.print(((char*)buf)[i]);
  119.    Serial.println("'");
  120.    
  121.    int currentSpeed;
  122.    sscanf(((char*)buf)+1, "%d", &currentSpeed);
  123.    //Serial.print(currentSpeed);
  124.    if(abs(prevmotorspeed-currentSpeed)>2){
  125.     setMotorSpeed(currentSpeed);
  126.     prevmotorspeed=currentSpeed;
  127.    }
  128.    
  129.    if (*(char*)buf == 'F') goForward();
  130.    if (*(char*)buf == 'B') goBackward();
  131.    if (*(char*)buf == 'L') turnLeft();
  132.    if (*(char*)buf == 'R') turnRight();
  133.    if (*(char*)buf == 'D') stopMoving();
  134.    
  135.  
  136.    //delay(10);
  137.    noSigCount=0;
  138.    digitalWrite(LED_BUILTIN, LOW);
  139.   }
  140.   else{
  141.    // Serial.println("NO SIGNAL");
  142.    if(noSigCount == 0) noSigTime = millis();
  143.    noSigCount = millis()-noSigTime;
  144.     if(noSigCount > 1500){
  145.       if(micros()%100000<2) Serial.println("No signal.");
  146.       stopMoving();
  147.     }
  148.   }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement