help on Electric Vehicle (SO)

hello,

I'm trying to get my project working by tomorrow evening so a timely response would be greatly appreciated (fingers crossed). Anyway, the goal of this project is to have a car drive down a track as fast as possible, then slow down at 8.5m and stop at 12m. I wrote up my code and so far it will only get as far as spinning up the motor and counting wheel turns using the optical shaft encoder. Can anyone please explain where the code is getting "stuck" and possibly offer some solutions?

This is my first time coding anything and I am here to learn so dont hold back, drop some knowledge on me!!

any reply would be greatly helpful.

thanks in advance,
Patrick

heres my sketch:

/* pins:
 *  optical shaft encoder = 3,5
 *  button = 11
 *  esc = 9
 *  led = 13
 */


#include <Servo.h>

Servo motor;

//IMPORTANT PART HERE
const int trackDist = 12000;       //in MILLIMETERS   ex. (11500cm = 11.50m)
const int wheelDi = 725;           //in MILLIMETERS
const int countTurn = 30;          //check that (shaft encoder count per turn)
//IMPORTANT PART HERE


int val; 
const int encoderPinA = 3;
const int encoderPinB = 5;
int encoderPos = 0;
int encoderPinALast = LOW;
int n = LOW;
int final = 0;

int wheelTurn;
int distTrav;
unsigned long startMillis = 0;

const int buttonPin = 11;
int buttonState;
int start = LOW;
const int ledPin = 13;

void setup() { 
  Serial.begin (9600);
  pinMode (encoderPinA,INPUT);
  pinMode (encoderPinB,INPUT);
  pinMode (buttonPin, INPUT);
  pinMode (ledPin, OUTPUT);

  motor.attach(9);
  motor.writeMicroseconds(1500);
} 

void loop() { 

  wheelTurn = encoderPos / countTurn;
  distTrav = wheelTurn * wheelDi * 314 / 1000;
 
     n = digitalRead(encoderPinA);
  if ((encoderPinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoderPinB) == LOW) {
      encoderPos--;
    } else {
      encoderPos++;
    }
    Serial.print (encoderPos);
    Serial.print ("/");
  }
  encoderPinALast = n;
 
  buttonState = digitalRead(buttonPin);
  unsigned long currentMillis = millis();
  digitalWrite(ledPin, HIGH);

  if (buttonState == HIGH) {
    encoderPos = 0;
    start = HIGH;
  }

  if (start == HIGH && distTrav < 8500) {
    startMillis = currentMillis;
    motor.writeMicroseconds(2000);
  }


  if (distTrav >= 8500 && distTrav < trackDist) {        //optional braking before cruise stage 
    motor.writeMicroseconds(1400);
    delay(200);
    motor.writeMicroseconds(1600);
  }

  if (distTrav >= trackDist) {          //optional relax on motor after stop stage
    motor.writeMicroseconds(1400);
    delay(2000);
    motor.writeMicroseconds(1500);
  }
  
 /* if (currentMillis - startMillis >= 8000) {             //failsafe
    motor.writeMicroseconds(1500);
    digitalWrite(ledPin, HIGH);
  } */

}

You've got some large numbers there - I'd be inclined to make them at least "long", instead of "int".

distTrav = wheelTurn * wheelDi * 314 / 1000;

is "wheelTurn * 725 * 314", so you've exceeded the range of an "int" right there.

When you're faced with problems like this, it's usually best to add some debug prints, to get the program to tell you what it is doing, rather than trying to guess.

Great thanks! ill give that a try and post back later.

Hey, I am working on this project as well, and I am looking for ways to approach it. I am very new to arduino so I was wondering if you had a relatively basic approach to this. I was thinking of setting up 4 press buttons of different colors and creating different combinations with the buttons. Each combo would have it a specific distance assigned to it. I feel like this is a very basic thing to do but I am hoping that you might have an even better approach to this.
Sagi Ravid.

sagi_ravid:
Hey, I am working on this project as well, and I am looking for ways to approach it. I am very new to arduino so I was wondering if you had a relatively basic approach to this. I was thinking of setting up 4 press buttons of different colors and creating different combinations with the buttons. Each combo would have it a specific distance assigned to it. I feel like this is a very basic thing to do but I am hoping that you might have an even better approach to this.
Sagi Ravid.

I think it's easiest to just have a laptop with you and adjust it that way. Different buttons would be really cool though

I bet you could run the same code each time if you had each button assigned to change the value of a variable controlling the stopping point