Remote control car with raspberry pi

So I’m trying to make a remote controlled car. It will consist of a raspberry pi connected via serial to the arduino. You control it by VNC into the raspberry pi and then sending the commands to arduino. This is the code I have for recognizing commands and acting on the ones that affect the servo. I am using the servo to turn the wheels to change directions, however the servo doesn’t turn a uniform distance when a add 45 to its position. Here’s the code:

#include <Servo.h>
Servo myservo; 
int servoPin = 3;
int potVal = 500;
void setup() {
void loop(){  
  if (Serial.available()) {
      String data = Serial.readString();
      if (data == "Hello!!") {
        Serial.println ("Hi, I'm ready!");
      if (data == "Left") {
        potVal = potVal - 45;
        Serial.println (potVal);
      if (data == "Right") {
        potVal = potVal + 45;
        Serial.println (potVal);
      if (data == "TakePic") {
        Serial.println ("Here's your picture!");
      if (data == "IncSpeed") {
        Serial.println ("Increasing Speed!");
      if (data == "DecSpeed") {
        Serial.println ("Decreasing Speed!");
   // potVal = map(potVal, 0, 1023, 0, 45);                

Is there some sort of fixed offset?

What does 'some sort of fixed offset' mean?

Well, you add 75, but subtract 45, so an offset of 30 (or minus 30, depending on POV)

Sorry, that is an error in the code. I have corrected it but the result is still the same.

I would keep more servo smarts in the rPi code(Python?) and have the Arduino just setting servo positions directly. No left or right in the Arduino, just checks for valid commands positions like 0 to 1500 or something like that. Then you'd calibrate for what straight ahead would be, maybe 700 and you store that in the rPi python or better yet store in a file and read the on startup and set your variables. I can see storing things like:
STEERING_LEFT_PWM = 1220 #pwm value for full left steering
STEERING_RIGHT_PWM = 290 #pwm value for full right steering
Your zero position would be calculated to be halfway between those values.

This way your Arduino code is robust and simple and your application logic(driving) is in the higher level Python code on the rPi and doesn't require constant compile/upload cycles.


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