RC car project - joystick code?

We are trying to run a RC solar car project and instead of using a bluetooth module we are trying to incorporate a joystick module. Here is the code for the project with bluetooth.Code

How would we change the code to include a joystick instead. We found this code online for coding an analog joystick to an arduino, will this work?

It's hard to edit a picture of code.

Remember to use code tags.

Here is the joystick code

Please post your code, in code tags

Please also explain how you think this is an installation issue.

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}

void loop() {
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
}

Will this work? I don't think it's an installation issue, but I categorized it under troubleshooting, I thought.

1 Like

You are right, so I moved it here.

ok thanks. Just still trying to figure out if the joystick code will work with the rest of the code for the solar/RC car project?

So why not let us in on the rest of the code as well? If it was in that Google docs link then pleas post it here in the correct way, which you now know.

Is there anything wrong with the code you did post? Is it working correctly?

Oh sorry. So here is the code from the project on the instructables.com website - RC Robot Car With Solar Panels. This original project uses a bluetooth module, but we want to use a joystick instead and we just want to take out any bluetooth module code (that would control the car with an android/app) and put in the joystick code. But we are sure if 1) the joystick code is correct for this project? and 2) where to place the code in the existing code for the project?

char blueReading;

void setup() {

pinMode(13,OUTPUT);

pinMode(12,OUTPUT);

pinMode(11,OUTPUT);

pinMode(10,OUTPUT);

Serial.begin(9600);

}

void loop() {

if(Serial.available()){ blueReading = Serial.read();

Serial.println(blueReading);

}

if(blueReading == 'F'){

digitalWrite(13,HIGH);

digitalWrite(11,HIGH);

digitalWrite(12,LOW);

digitalWrite(10,LOW);

}

else if(blueReading == 'B'){

digitalWrite(12,HIGH);

digitalWrite(10,HIGH);

digitalWrite(13,LOW)

; digitalWrite(11,LOW);

}

else if(blueReading == 'I'){

digitalWrite(11,HIGH);

digitalWrite(13,LOW);

digitalWrite(12,LOW);

digitalWrite(10,LOW);

}

else if(blueReading == 'G'){

digitalWrite(13,HIGH);

}

else if(blueReading == 'H'){

digitalWrite(12,HIGH);

digitalWrite(13,LOW);

digitalWrite(11,LOW);

digitalWrite(10,LOW);

}

else if(blueReading == 'J'){

digitalWrite(10,HIGH);

digitalWrite(13,LOW);

digitalWrite(11,LOW);

digitalWrite(12,LOW);

}

else if(blueReading == 'S'){

digitalWrite(13,LOW);

digitalWrite(12,LOW);

digitalWrite(11,LOW);

digitalWrite(10,LOW);

}

}

joystick code is correct for this project?

Can you explain more about how you want to use the joystick with the robot?

Do you want to send x-y joystick values, instead of commands, to the robot by bluetooth?

How do you want the robot to respond to those values? Do you want to map 7 different x-y joystick outputs into the 7 individual character commands, or do you want to use the joystick in a different manner.

I am not sure. We want something simple, preferably not bluetooth. Can we add an IR receiver? Whatever you think will require the least steps and troubleshooting. We just simply want to control the car with one joystick. Here are the components we are using so far:

At least 2x solar panels

1x battery 9v - rechargeable

1x lithium battery 9v

2x battery clips

1x Arduino Uno Rev3 or Mega 2560

1x bluetooth module - PREFER NOT TO USE

1joystick module - THIS IS THE NEW COMPONENT WE ARE ADDING, NOT PART OF THE ORIGINAL PROJECT. Do we also need a IR Receiver?

1x L298N motor controller board

4x DC gearbox motor

4x wheels

Many jumper cables

We also have these modules to connect the solar panels to the 9V batteries for charging them.
TP4056 Lithium Battery Charging Board

Why do you not want to use bluetooth?

Here's a link to a tutorial using an IR transmitter and IR receiver for joystick output.

https://aaravpatel.com/2020/06/17/joystick-controlled-car/

Yes but why?

I sense that you are trying to cobble together a project without any real idea of what you are doing. Just searching for stuff that sounds like part of your project and then not knowing how to incorporate two ideas together.

The coding involved in doing this is almost trivial.

Rule 1 - Never use Instructables to learn anything. They are almost universally poor, written by people with an inflated view of their own skills. Only use that site if you can spot the errors that will inevitably be present.

Rule 2 - Never use code that you don't understand.

From what I gather you want to use analogue pots to produce digital control, that is just a left / right / or nothing (centre), is this correct?

If so start off by defining, on pencil and paper, what you want the motor to do for each three states of each of the two pots.

Then work out what motor control pins need to be set at what level to achieve this. How are you going to control the stop / go of each motor?

When you have this sorted only then will you be able to begin coding. When you have had a try at this coding and get stuck, that is the time to post on a forum for help.

Make sure you post the code you are having trouble with and include a schematic of your hardware. You will need one of these before you can even consider building it. Note a schematic is not a Frtizing layout diagram, a hand drawn sketch will be fine.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.