Go Down

Topic: Project needs laptop (Read 110 times) previous topic - next topic

Pimmeke

Mar 07, 2017, 11:37 am Last Edit: Mar 07, 2017, 11:46 am by Pimmeke
I am making a project for school. I am making a car that follows a line. When the arduino is attached to my laptop the program somethimes works, but when it needs to run on the 4 1,5V bateries only the lights on the arduino and the motorshield work. How do I fix this?

Code: [Select]
#include <AFMotor.h>
#include <QTRSensors.h>
  
AF_DCMotor motor1(1, MOTOR12_8KHZ );
AF_DCMotor motor2(2, MOTOR12_8KHZ );
  
#define KP .2
#define KD 5
#define M1_minumum_speed 150
#define M2_minumum_speed 150
#define M1_maksimum_speed 200
#define M2_maksimum_speed 200
#define MIDDLE_SENSOR 4
#define NUM_SENSORS 5
#define TIMEOUT 2500
#define EMITTER_PIN 2
#define DEBUG 0
  
QTRSensorsRC qtrrc((unsigned char[]) { A4,A3,A2,A1,A0} ,NUM_SENSORS, TIMEOUT, EMITTER_PIN);
  
unsigned int sensorValues[NUM_SENSORS];
  
void setup()
{
  delay(1500);
  manual_calibration();
  set_motors(0,0);
}
  
int lastError = 0;
int last_proportional = 0;
int integral = 0;
  
void loop()
{
  
  unsigned int sensors[5];
  int position = qtrrc.readLine(sensors);
  int error = position - 2000;
  
  int motorSpeed = KP * error + KD * (error - lastError);
  lastError = error;
  
  int leftMotorSpeed = M1_minumum_speed + motorSpeed;
  int rightMotorSpeed = M2_minumum_speed - motorSpeed;
  
  // set motor speeds using the two motor speed variables above
  set_motors(leftMotorSpeed, rightMotorSpeed);
}
  
void set_motors(int motor1speed, int motor2speed)
{
  if (motor1speed > M1_maksimum_speed ) motor1speed = M1_maksimum_speed;
  if (motor2speed > M2_maksimum_speed ) motor2speed = M2_maksimum_speed;
  if (motor1speed < 0) motor1speed = 0;
  if (motor2speed < 0) motor2speed = 0;
  motor1.setSpeed(motor1speed);
  motor2.setSpeed(motor2speed);
  motor1.run(FORWARD);
  motor2.run(FORWARD);
}
  
void manual_calibration() {
  
  int i;
  for (i = 0; i < 250; i++){
    
    qtrrc.calibrate(QTR_EMITTERS_ON);
    delay(20);
}
  
  if (DEBUG) {
    
    Serial.begin(9600);
    
    for (int i = 0; i < NUM_SENSORS; i++){
      
      Serial.print(qtrrc.calibratedMinimumOn[i]);
      Serial.print(' ');
}
    Serial.println();
  
    for (int i = 0; i < NUM_SENSORS; i++){
      
      Serial.print(qtrrc.calibratedMaximumOn[i]);
      Serial.print(' ');
}
    Serial.println();
    Serial.println();
}
}

Robin2

You need to post your program.

And when posting code please use the code button </>
Code: [Select]
so your code looks like thisand is easy to copy to a text editor See How to use the Forum

Have you written the program so it is waiting for some input from your PC?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy