1

I am trying to make a program for my Arduino that will run 4 motors with random actions (forward,backward,left,right) and execution is determined by a random number generator. When I compile there is no errors, and The COM port is connected correctly to my Arduino Mega, but when it uploads the log says this:

Sketch uses 668 bytes (0%) of program storage space. Maximum is 253,952 bytes.

Global variables use 9 bytes (0%) of dynamic memory, leaving 8,183 bytes for local variables. Maximum is 8,192 bytes.

That means only 677 B is being used, but the Arduino file is 6 KB. I know that the program gets somewhat smaller when it is compiled, but not ten times smaller.

What's happening here?

EDIT

// Arduino pins for the shift register
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8

// 8 bit bus

#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR3_A 5
#define MOTOR3_B 7
#define MOTOR4_A 0
#define MOTOR4_B 6

// Motor pins:

#define MOTOR1_PWM 11
#define MOTOR2_PWM 3
#define MOTOR3_PWM 6
#define MOTOR4_PWM 5

// LED Pins

#define led_r 22
#define led_l 23

// Actions

#define RIGHT 0
#define LEFT 1
#define FORWARD 2
#define STOP 3

// Codes for the motor function.
#define GO 1
#define REVERSE 2
#define BRAKE 3
#define RELEASE 4

// Probability

#define rProb 5
#define lProb 5
#define fProb 88
#define stopProb 2

// other globals

#define defSpd 255

void setup() {}

void execute(int action) {

 if (action == FORWARD) {

  motor(1,FORWARD,defSpd);
  motor(2,FORWARD,defSpd);
  motor(3,FORWARD,defSpd);
  motor(4,FORWARD,defSpd);

  digitalWrite(led_r, LOW);
  digitalWrite(led_l, LOW);
 } else if (action == RIGHT) {

  motor(1,REVERSE,defSpd);
  motor(2,REVERSE,defSpd);
  motor(3,FORWARD,defSpd);
  motor(4,FORWARD,defSpd);

  digitalWrite(led_l, LOW);
  digitalWrite(led_r, HIGH);
  delay(150);

 } else if (action == LEFT) {

  motor(1,FORWARD,defSpd);
  motor(2,FORWARD,defSpd);
  motor(3,REVERSE,defSpd);
  motor(4,REVERSE,defSpd);

  digitalWrite(led_l, HIGH);
  digitalWrite(led_r, LOW);

  delay(150);

 } else if (action == STOP) {

  motor(1,BRAKE,defSpd);
  motor(2,BRAKE,defSpd);
  motor(3,BRAKE,defSpd);
  motor(4,BRAKE,defSpd);

  digitalWrite(led_r, HIGH);
  digitalWrite(led_l, HIGH);

  delay(1000);

 } 

}

void loop() {
  // put your main code here, to run repeatedly:

  // Random action
  int i = (1, 101);

  if (i > 0 && i <= rProb) {

    execute(RIGHT);

  } else if (i > rProb && i <= rProb + lProb) {

    execute(LEFT);

  } else if (i > rProb + lProb && i <= rProb + lProb + fProb) {

    execute(FORWARD);

  } else if (i > rProb + lProb + fProb && i <= rProb + lProb + fProb + stopProb) {

    execute(STOP);

  }

}

void motor(int nMotor, int command, int speed)
{
  int motorA, motorB;

  if (nMotor >= 1 && nMotor <= 4)
  {  
    switch (nMotor)
    {
    case 1:
      motorA   = MOTOR1_A;
      motorB   = MOTOR1_B;
      break;
    case 2:
      motorA   = MOTOR2_A;
      motorB   = MOTOR2_B;
      break;
    case 3:
      motorA   = MOTOR3_A;
      motorB   = MOTOR3_B;
      break;
    case 4:
      motorA   = MOTOR4_A;
      motorB   = MOTOR4_B;
      break;
    default:
      break;
    }

    switch (command)
    {
    case GO:
      motor_output (motorA, HIGH, speed);
      motor_output (motorB, LOW, -1);     // -1: no PWM set
      break;
    case REVERSE:
      motor_output (motorA, LOW, speed);
      motor_output (motorB, HIGH, -1);    // -1: no PWM set
      break;
    case BRAKE:
      // The AdaFruit library didn't implement a brake.
      // The L293D motor driver ic doesn't have a good
      // brake anyway.
      // It uses transistors inside, and not mosfets.
      // Some use a software break, by using a short
      // reverse voltage.
      // This brake will try to brake, by enabling 
      // the output and by pulling both outputs to ground.
      // But it isn't a good break.
      motor_output (motorA, LOW, 255); // 255: fully on.
      motor_output (motorB, LOW, -1);  // -1: no PWM set
      break;
    case RELEASE:
      motor_output (motorA, LOW, 0);  // 0: output floating.
      motor_output (motorB, LOW, -1); // -1: no PWM set
      break;
    default:
      break;
    }
  }
}

void motor_output (int output, int high_low, int speed)
{
  int motorPWM;

  switch (output)
  {
  case MOTOR1_A:
  case MOTOR1_B:
    motorPWM = MOTOR1_PWM;
    break;
  case MOTOR2_A:
  case MOTOR2_B:
    motorPWM = MOTOR2_PWM;
    break;
  case MOTOR3_A:
  case MOTOR3_B:
    motorPWM = MOTOR3_PWM;
    break;
  case MOTOR4_A:
  case MOTOR4_B:
    motorPWM = MOTOR4_PWM;
    break;
  default:
    // Use speed as error flag, -3333 = invalid output.
    speed = -3333;
    break;
  }

  if (speed != -3333)
  {
    // Set the direction with the shift register 
    // on the MotorShield, even if the speed = -1.
    // In that case the direction will be set, but
    // not the PWM.
    shiftWrite(output, high_low);

    // set PWM only if it is valid
    if (speed >= 0 && speed <= 255)    
    {
      analogWrite(motorPWM, speed);
    }
  }
}

void shiftWrite(int output, int high_low)
{
  static int latch_copy;
  static int shift_register_initialized = false;

  // Do the initialization on the fly, 
  // at the first time it is used.
  if (!shift_register_initialized)
  {
    // Set pins for shift register to output
    pinMode(MOTORLATCH, OUTPUT);
    pinMode(MOTORENABLE, OUTPUT);
    pinMode(MOTORDATA, OUTPUT);
    pinMode(MOTORCLK, OUTPUT);

    // Set pins for shift register to default value (low);
    digitalWrite(MOTORDATA, LOW);
    digitalWrite(MOTORLATCH, LOW);
    digitalWrite(MOTORCLK, LOW);
    // Enable the shift register, set Enable pin Low.
    digitalWrite(MOTORENABLE, LOW);

    // start with all outputs (of the shift register) low
    latch_copy = 0;

    shift_register_initialized = true;
  }

  // The defines HIGH and LOW are 1 and 0.
  // So this is valid.
  bitWrite(latch_copy, output, high_low);

  // Use the default Arduino 'shiftOut()' function to
  // shift the bits with the MOTORCLK as clock pulse.
  // The 74HC595 shiftregister wants the MSB first.
  // After that, generate a latch pulse with MOTORLATCH.
  shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
  delayMicroseconds(5);    // For safety, not really needed.
  digitalWrite(MOTORLATCH, HIGH);
  delayMicroseconds(5);    // For safety, not really needed.
  digitalWrite(MOTORLATCH, LOW);
}
  • 1
    Let me look in my crystal ball.... Ah yes, bits are being optimized out because you haven't written your software right. Post your code, the others here aren't psychic! – Majenko Aug 26 '15 at 16:52
  • @Majenko Code added. – Galen Nare Aug 26 '15 at 18:20
3

What is:

// Random action
int i = (1, 101);

supposed to achieve?

/tmp/untitled1/untitled1.ino: In function 'void loop()':
/tmp/untitled1/untitled1.ino:110:13: warning: left operand of comma operator has no effect [-Wunused-value]

Ergo, i is always going to be 101. So all the if constructs will be false, so they get optimized out (they are all constants). Because of that the other functions are never getting called, so they are optimized out.

So you are left with next to nothing.

I think you may have missed the word random out from there...

  • Program size: 2798 bytes
  • Memory size: 17 bytes
  • Compilation took 0.227 seconds
  • I believe I saw in some docs that (min,max) could be used to generate random numbers. Obviously not. Thanks! – Galen Nare Aug 26 '15 at 18:32
  • @GalenNare Only as parameters to the random() function. – Majenko Aug 26 '15 at 18:32

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.