Skip to main content
added 270 characters in body
Source Link

schematic

simulate this circuit – Schematic created using CircuitLab

schematic

simulate this circuit – Schematic created using CircuitLab

Source Link

Rc car controller using the RC-switch library

(Newbie in arduino) Hi so i have a project which involves creating an rc car which uses the 433Mhz rf module and an arduino nano with a servo motor and an ultrasonic parking sensor.

I'm having problems with the code both on the transmitter and receiver, Here is the code for the transmitter.

 #include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
// Assigning controller buttons to Digital Pins
 const int forward = 8;
 const int reverse = 9;
 const int rightTurn = 10;
 const int leftTurn = 11;
 
 int Fwd = 0
 int Bwd = 0
 int Lt  = 0
 int Rt  = 0

void setup () {
  pinMode (forward, INPUT);
  pinMode (reverse, INPUT);
  pinMode (rightTurn, INPUT);
  pinMode (leftTurn, INPUT);
  mySwitch.enableTransmit(7);
  Serial.begin (9600);         // Debugging only
  Serial.println ("Transmitting");
}

void loop () {
  Fwd = digitalRead(forward);
  Bwd = digitalRead(reverse);
  Rt  = digitalRead(rightTurn);
  Lt  = digitalRead(leftTurn);
  
  if (Fwd == HIGH) {
    mySwitch.send('1');
    Serial.println ("Forward");
    delay(100);
  }

  if (Bwd == HIGH) {
    mySwitch.send('2');
    Serial.println ("Reverse");
    delay(100);
  }
  
  if (Rt == HIGH) {
    mySwitch.send('3');
    Serial.println ("Left Turn");
    delay(100);
  }
  
  if (Lt == HIGH) {
    mySwitch.send('4');
    Serial.println ("Right Turn");
    delay(100);
  }
}

And here's the code for the receiver

#include <RCSwitch.h>
#include <Servo.h>

const int IN1 = 3;
const int IN2 = 4;
const int trigPin = 5;
const int echoPin = 6;
int distance = 0;

RCSwitch mySwitch = RCSwitch();
Servo myservo;

void setup () {
  mySwitch.enableReceive(2);
  myservo.attach(7);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin (9600);    // Debugging only
  Serial.println ("Receiver Ready");
}

void loop () {
 if (mySwitch.available()) {
    
    int value = mySwitch.getReceivedValue();
      
      if(value == '1') {
        forward ();
        Serial.println (" = forward");
      }
      
      if(value == '2'){
        backward ();
        Serial.println (" = backward");
      }
      
      if(value == '3') {
        left ();
        Serial.println (" = left");
      }
      
      if(value == '4') {
        right ();
        Serial.println (" = right");
      }
      Serial.print(" ");
    }
    
}

void forward () {
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
}
long duration;
int value = mySwitch.getReceivedValue();
void backward () {
  if ((distance > 10)&& (value == '2'))
  {
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, LOW);
  }
  else if ((distance < 10)&& (value == '2'))
  {
  digitalWrite (IN1, HIGH);
  digitalWrite (IN2, LOW);
  }
  delayMicroseconds(10);
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance= duration*0.034/2;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance); //distance is the returned integer,
}

void right () {
  myservo.write(0);
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
}

void left () {
  myservo.write(90);
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
}

The problem is when i run the code the buttons start getting pressed even if I'm not pressing them and even when i disconnect them from the arduino. and also the receiver cant receive any data. Please help