0

Hi i am currently working on a school project , my only problem is that i want my RC car to return to middle direction (which i have set to 66 in the servo motor) when the left or right button is not pressed . i used if statements but it doesn't work. Are there any function that i can use ? Here is my code for the transmitter i used the RC-Switch Library (https://www.arduinolibraries.info/libraries/rc-switch)

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

const int IN1 = 3;
const int IN2 = 4;
const int trigPin = 5;
const int echoPin = 6;
const int Buzzer = 8;
int distance = 0;
int value = 0;
long duration;
RCSwitch mySwitch = RCSwitch();
Servo myservo;

void setup () {
  mySwitch.enableReceive(0);
  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
  pinMode(Buzzer,OUTPUT);
  Serial.begin (9600);    // Debugging only
  Serial.println ("Receiver Ready");
}

void loop () {

  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,

 if (mySwitch.available()) {

    value = mySwitch.getReceivedValue();
    Serial.println (value);

      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(" ");
    }
  mySwitch.resetAvailable();
  //myservo.write(45);
}

void forward () {
  digitalWrite (IN1, LOW);
  digitalWrite (IN2, HIGH);
}

//value = mySwitch.getReceivedValue();
void backward () {

  if ((distance < 50)&& (value == '2'))
  {
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, LOW);
    digitalWrite (Buzzer, HIGH);
  }
  else if ((distance > 50)&& (value == '2'))
  {
    digitalWrite (IN1, HIGH);
    digitalWrite (IN2, LOW);
    digitalWrite (Buzzer, LOW);
  }
}
// 66 is middle


void right () {
  if (value == 4){
    myservo.write(110);
  } else
  myservo.write(66);
}

void left () {
  if (value == 3){
    myservo.write(50);
  }else
 myservo.write(66);
}

And Here is The transmitter code

// TRANSMITTER CODE

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

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

void loop () {
  if (digitalRead(forward) == LOW) {
    mySwitch.send(1, 24);
    Serial.println ("Forward");
  }

  if (digitalRead(reverse) == LOW) {
    mySwitch.send(2, 24);
    Serial.println ("Reverse");
  }

  if (digitalRead(leftTurn) == LOW) {
    mySwitch.send(3, 24);
    Serial.println ("Left Turn");
  }

  if (digitalRead(rightTurn) == LOW) {
    mySwitch.send(4, 24);
    Serial.println ("Right Turn");
  }
}
  • do not use statements such as but it doesn't work .... there is no useful information contained in such statements ...... statements such as it keeps turning left, actually describe what is happening – jsotola Aug 10 '18 at 16:43
1

You send values from your transmitter when you press a button but not when you release them. The simplest solution would be sending the state of all buttons in every iteration if the transmitter has enough capacity. (I think it has for just these four values)

Add an enum to make the code more readable:

enum {
    FORWARD_ON = 0,
    FORWARD_OFF, // It will be 1.
    BACKWARD_ON, // It will be 2 and so on...
    BACKWARD_OFF,
    RIGHT_ON,
    RIGHT_OFF,
    LEFT_ON,
    LEFT_OFF
};

So a sending function will be like this:

if (digitalRead(forward) == LOW)
    mySwitch.send(FORWARD_ON, 24);
else
    mySwitch.send(FORWARD_OFF, 24);
// And so on for every button...

Then by reception you should add some state variables and store the received values:

int steer_right = 0;
int steer_left = 0;

// ...

loop() {

// ...

value = mySwitch.getReceivedValue();

switch (value) {
    case RIGHT_ON:
        steer_right = 1;
        break;
    case RIGHT_OFF:
        steer_right = 0;
        break;
    case LEFT_ON:
        steer_left = 1;
        break;
    case LEFT_OFF:
        steer_left = 0;
        break;

    // ... (throttle)

    default:
        break;
}

// ...

}

Then you evaluate what you got:

if (steer_right && !steer_left)
    right();
else if (!steer_right && steer_left)
    left();
else
    straight();

// ... (throttle)
  • Hi it's me again, I'm a bit confused at the switch (value) part , should i declare the RIGHT_ON and RIGHT_OFF as variables or enum them ? – Underhanded CODE Aug 10 '18 at 15:32
  • and also is there anyway i can speed up the sending speed? – Underhanded CODE Aug 10 '18 at 15:44
  • another question is it keeps printing when the button is off which i think is the cause why it's slow to send and receive , is there any way i can prevent this ? – Underhanded CODE Aug 10 '18 at 15:57
  • keeps printing what? ..... do not print when the button is not pressed (use if statement or case statement ) – jsotola Aug 10 '18 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.