Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Robot Car v2.1
- This is one of my first Arduino projects.
- Code for the remote control is available at:
- https://pastebin.com/98dhpUns
- Enjoy!
- The circuit:
- 433Mhz RF receiver:
- ** DAT - pin 11
- L298N H-Bridge:
- ** ENA, ENB - pin 5
- ** IN1 - pin 6
- ** IN2 - pin 7
- ** IN3 - pin 8
- ** IN4 - pin 9
- created 1 Febr 2020
- modified 23 May 2020
- by Mate Z.
- */
- #include <RH_ASK.h> //Radio Head RF library
- #include <SPI.h> //previous one needs it
- RH_ASK rf_driver;
- //L298N control pins
- const int LeftMotorForward = 8; //orange wire
- const int LeftMotorBackward = 9; //yellow wire
- const int RightMotorForward = 7; //red wire
- const int RightMotorBackward = 6; //brown wire
- const int SpeedControl = 5; //white wire
- int noSigCount=0;
- int noSigTime=0;
- int prevmotorspeed = 0;
- //moving functions
- void setMotorSpeed(int spd){
- analogWrite(SpeedControl, spd);
- }
- void stopMoving(){
- digitalWrite(LeftMotorForward, LOW);
- digitalWrite(LeftMotorBackward, LOW);
- digitalWrite(RightMotorForward, LOW);
- digitalWrite(RightMotorBackward, LOW);
- }
- void goForward(){
- digitalWrite(LeftMotorForward, HIGH);
- digitalWrite(LeftMotorBackward, LOW);
- digitalWrite(RightMotorForward, HIGH);
- digitalWrite(RightMotorBackward, LOW);
- }
- void goBackward(){
- digitalWrite(LeftMotorForward, LOW);
- digitalWrite(LeftMotorBackward, HIGH);
- digitalWrite(RightMotorForward, LOW);
- digitalWrite(RightMotorBackward, HIGH);
- }
- void turnRight(){
- digitalWrite(LeftMotorForward, HIGH);
- digitalWrite(LeftMotorBackward, LOW);
- digitalWrite(RightMotorForward, LOW);
- digitalWrite(RightMotorBackward, HIGH);
- }
- void turnLeft(){
- digitalWrite(LeftMotorForward, LOW);
- digitalWrite(LeftMotorBackward, HIGH);
- digitalWrite(RightMotorForward, HIGH);
- digitalWrite(RightMotorBackward, LOW);
- }
- void setup() {
- // put your setup code here, to run once:
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(LeftMotorForward, OUTPUT);
- pinMode(LeftMotorBackward, OUTPUT);
- pinMode(RightMotorForward, OUTPUT);
- pinMode(RightMotorBackward, OUTPUT);
- rf_driver.init();
- Serial.begin(9600);
- Serial.println("Arduino booted up");
- for(int i=0; i<5; ++i){
- delay(300);
- digitalWrite(LED_BUILTIN, HIGH);
- delay(300);
- digitalWrite(LED_BUILTIN, LOW);
- }
- setMotorSpeed(50);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- uint8_t buf[4];
- uint8_t buflen = sizeof(buf);
- if (rf_driver.recv(buf, &buflen)){
- digitalWrite(LED_BUILTIN, HIGH);
- Serial.print("Incoming transmission: '");
- for(int i = 0; i < 4; i++)
- Serial.print(((char*)buf)[i]);
- Serial.println("'");
- int currentSpeed;
- sscanf(((char*)buf)+1, "%d", ¤tSpeed);
- //Serial.print(currentSpeed);
- if(abs(prevmotorspeed-currentSpeed)>2){
- setMotorSpeed(currentSpeed);
- prevmotorspeed=currentSpeed;
- }
- if (*(char*)buf == 'F') goForward();
- if (*(char*)buf == 'B') goBackward();
- if (*(char*)buf == 'L') turnLeft();
- if (*(char*)buf == 'R') turnRight();
- if (*(char*)buf == 'D') stopMoving();
- //delay(10);
- noSigCount=0;
- digitalWrite(LED_BUILTIN, LOW);
- }
- else{
- // Serial.println("NO SIGNAL");
- if(noSigCount == 0) noSigTime = millis();
- noSigCount = millis()-noSigTime;
- if(noSigCount > 1500){
- if(micros()%100000<2) Serial.println("No signal.");
- stopMoving();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement