a[i] = b * c[i]; Comes With Errors Inside For & While Loops

int basicThrust;
int motor[6];
const float ax[6] = {@&#^%&};
const float ay[6] = {^%$#&
%};
const float az[6] = {%$#^%&*};
int axScaler;
int ayScaler;
int azScaler;

I’m using a nano board, tying to write a hexacopter flight-controller myself.
Above are variables’ types, and the following code inside void loop comes with error:

test_fly_004.ino: In function ‘void loop()’:
test_fly_004.ino:127:1: error: unable to find a register to spill in class ‘POINTER_REGS’

  for(int k=0; i<=5; i++){
  motor[i] = basicThrust;
  motor[i] += (axScaler) * (ax[i]);
  motor[i] += (ayScaler) * (ay[i]);
  motor[i] += (azScaler) * (az[i]);
  }

Changed to while loop won’t help either, IDE detects the same error.

  int k=0;
  while(i<=5){
  motor[i] = basicThrust;
  motor[i] += (axScaler) * (ax[i]);
  motor[i] += (ayScaler) * (ay[i]);
  motor[i] += (azScaler) * (az[i]);
  i++;
  }

I also checked IDE doesn’t mixed " * multiplication " with " * dereference operator" by changing * into +.

But when I remove one of the variables:

  for(int k=0; i<=5; i++){
  motor[i] = basicThrust;
  motor[i] += (ax[i]);
  motor[i] += (ay[i]);
  motor[i] += (az[i]);
  }
  for(int k=0; i<=5; i++){
  motor[i] = basicThrust;
  motor[i] += (axScaler);
  motor[i] += (ayScaler);
  motor[i] += (azScaler);
  }

No errors now.

Does it mean I have to create another variable to temporarily store (axScaler) * (ax*) , then add it on?Besides, I’d like to know why it happens.*
Thanks.

Post all your code, using code tags.

Is this supposed to represent foul language? Either way, it won't compile.

const float ax[6] = {@*&#^%&};

There are a number of things that are, at best, fuzzy. First, what's this about:

const float ax[6] = {@*&#^%&};
const float ay[6] = {^%$#&*%};
const float az[6] = {%$#^%&*};

Those don't look like floating point constants. In the following expression:

 motor[i] += (axScaler) * (ax[i]);

You should post all of your code, too.

for(int k=0; i<=5; i++){

Why are you initializing ‘k’ but incrementing ‘i’ and also using ‘i’ as the array indices?

Pete

For some reason, part of my post was lost. The expression:

 motor[i] += (axScaler) * (ax[i]);

needs a cast. If you are truncating the float, then use:

 motor[i] += axScaler * (int) ax[i];

If you need the fractional component in the calculation, use these casts:

 motor[i] += (int) ( (float) axScaler * ax[i]);

Your original code is trying to pour four bytes of float information into a 2 byte int bucket, which runs the risk of losing data.

Sorry for stupid mistakes while posting the question.

“#^%$$#” looks like this:

const float ax[6] = {-0.166666, 0.166666,-0.333333, 0.333333,-0.166666, 0.166666};
const float ay[6] = {-0.288675,-0.288675, 0.0, 0.0, 0.288675, 0.288675};
const float az[6] = { 1.0,-1.0,-1.0, 1.0, 1.0,-1.0};

And only “i”, no “k”

Thank you econjack. I tried both, same error popped up.

and here’s my code:

#include<adxl345.h>
#include<Wire.h>
#include<Servo.h>

int PPMcounter = 0;
unsigned long Stopwatch;
unsigned long Laststopwatch = 0;
float axScaler;
float ayScaler;
float azScaler;

const float ax[6] = {-0.166666, 0.166666,-0.333333, 0.333333,-0.166666, 0.166666};
const float ay[6] = {-0.288675,-0.288675, 0.0, 0.0, 0.288675, 0.288675};
const float az[6] = { 1.0,-1.0,-1.0, 1.0, 1.0,-1.0};

unsigned int Pulsewidth;
int Ch1pw;
int Ch2pw;
int Ch3pw;
int Ch4pw;
int Ch5pw;

int motor[6] = {900,900,900,900,900,900};

int Vacc[3];
adxl345 ACC;

unsigned long basicThrust = 1070;


Servo ESC1, ESC2, ESC3, ESC4, ESC5, ESC6;

void setup() {
  ACC.StartACC();
  ESC1.attach(11);
  ESC2.attach(10);
  ESC3.attach(9);
  ESC4.attach(6);
  ESC5.attach(5);
  ESC6.attach(3);
  
  ESC1.writeMicroseconds(motor[0]);
  ESC2.writeMicroseconds(motor[1]);
  ESC3.writeMicroseconds(motor[2]);
  ESC4.writeMicroseconds(motor[3]);
  ESC5.writeMicroseconds(motor[4]);
  ESC6.writeMicroseconds(motor[5]);
  
  
  delay(4000);
  
  attachInterrupt(0, PPM, CHANGE);
}

void loop() {
  ACC.GetVacc(Vacc);
  
  basicThrust = basicThrust+((Ch3pw - 1468)/100);
  basicThrust = constrain(basicThrust, 1000, 2000);
  
  axScaler = ((Ch1pw-1488)*15)/50;
  //axScaler += (Vacc[1])*(-10);
  
  ayScaler = ((1488-Ch2pw)*15)/50;
  //ayScaler += (Vacc[0])*(10);
  
  azScaler = ((Ch4pw-1484)*15)/120;
  
  int k=0;
  while(k<=5){
  motor[k] = basicThrust;// + axScaler*(-1/6) + ayScaler*(-1/34640) + azScaler*( 1);
  motor[k] += (axScaler) * ((int)ax[k]);
  motor[k] += (ayScaler) * ((int)ay[k]);
  motor[k] += (azScaler) * ((int)az[k]);
  k++;
  }
  if(Ch5pw > 1500)
  {
    motor[0] = 1000;
    motor[1] = 1000;
    motor[2] = 1000;
    motor[3] = 1000;
    motor[4] = 1000;
    motor[5] = 1000;
    basicThrust = 1000;
  }
  
  
  delay(50);
  noInterrupts();
  ESC1.writeMicroseconds(motor[0]);
  ESC2.writeMicroseconds(motor[1]);
  ESC3.writeMicroseconds(motor[2]);
  ESC4.writeMicroseconds(motor[3]);
  ESC5.writeMicroseconds(motor[4]);
  ESC6.writeMicroseconds(motor[5]);
  interrupts();
}
void PPM()
{
  Stopwatch = micros();
  Pulsewidth = Stopwatch - Laststopwatch;
  Laststopwatch = Stopwatch;
  
  
  if(Pulsewidth > 4000 )
    { 
      PPMcounter = 0;
    }
    
   else if( PPMcounter == 1)
      {
        Ch1pw = Pulsewidth;
      }
   else if( PPMcounter == 2)
      {
        Ch2pw = Pulsewidth;
      }
   else if( PPMcounter == 3)
      {
        Ch3pw = Pulsewidth;
      }
   else if( PPMcounter == 4)
      {
        Ch4pw = Pulsewidth;
      }
   else
      {
        Ch5pw = Pulsewidth;
      }
   PPMcounter++;   

}

test_fly_004.ino (3.44 KB)

In expressions like these:

  basicThrust = constrain(basicThrust, 1000, 2000);
  
  axScaler = ((Ch1pw-1488)*15)/50;

all of the numeric constants are treated as int, which screws up the math. Use a type modifier to make it known to the compiler what the data type is, or use a decimal value for *float*s:

  basicThrust = constrain(basicThrust, 1000L, 2000L);   // Notice the 'L' so the compiler knows its a long
  
  axScaler = (( (float) Ch1pw-1488.0)*15.0)/50.0;

See if that helps.

Thank you econjack. That will make my code more decent, since there will be more and more math.
"a = b * c_"inside for/while still won’t work. I’ll forget about simplicity and use "d = b; d *= c; a += d "_

Guess what? [i] renders your code in italics, and also makes it gibberish.