Pages: 1 [2]   Go Down
Author Topic: dynamic array? or varrying the size of the array based on something?  (Read 1502 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  for (i0 = 0; i0 <= (SnumRead); i0++)
Why are there parentheses around SnumRead?

Code:
  for (i1 = 0; i1 <= (SnumRead + 1); i1++)
Why does this loop loop a different number of times?

You really need to start using arrays and functions. Nothing about those 4 blocks of code (expect the bizarre discrepancy in the number of times each loops) is different, except which array is read from. There is, for instance, absolutely no reason to use four different loop index variables or 4 different accumulators.

Casting 1000000 to a float may not be achieving the result you expect. Using 1000000.0 instead would eliminate the need for the cast. Also, the compiler knows that a literal with a decimal place in it is not an int. Without that decimal point, it assumes that the value IS an int (which it isn't). What happens when that value is cast to a float is anyone's guess.

I wouldn't write code that requires guessing. Your mileage may vary.

Your ISRs are theoretically all firing at different times. Yet, SnumRead is the same for all 4 situations. Why?

i figured out the problem it was the = sign in the isr.

the reason for the different indexes is because each isr in theory will be firing at a different time at slightly different frequencies.  i'll try removing the float cast and see what happens

snumread is the number of spaces in the array to use; each block of code takes the same number of samples.   the parentheses were an error; that was some in between code i was working on.  i fixed it here:

Code:
const int SnumReadCC = 5;
volatile int SnumRead = 1;
const int pot = A0;

volatile unsigned long pulse0[SnumReadCC];
volatile unsigned long pIndex0 = 0;
volatile unsigned long nowPulse0 = 0;
volatile unsigned long lastPulse0 = 0;
volatile unsigned long nxtLastPulse0 = 0;
volatile unsigned long i0, sum0, average0 = 0;
float rate0;
float correction0 = 1;

volatile unsigned long pulse1[SnumReadCC];
volatile unsigned long pIndex1 = 0;
volatile unsigned long nowPulse1 = 0;
volatile unsigned long lastPulse1 = 0;
volatile unsigned long nxtLastPulse1 = 0;
volatile unsigned long i1, sum1, average1 = 0;
float rate1;
float correction1 = 1;

volatile unsigned long pulse2[SnumReadCC];
volatile unsigned long pIndex2 = 0;
volatile unsigned long nowPulse2 = 0;
volatile unsigned long lastPulse2 = 0;
volatile unsigned long nxtLastPulse2 = 0;
volatile unsigned long i2, sum2, average2 = 0;
float rate2;
float correction2 = 1;

volatile unsigned long pulse3[SnumReadCC];
volatile unsigned long pIndex3 = 0;
volatile unsigned long nowPulse3 = 0;
volatile unsigned long lastPulse3 = 0;
volatile unsigned long nxtLastPulse3 = 0;
volatile unsigned long i3, sum3, average3 = 0;
float rate3;
float correction3 = 1;

float slip;
float threshold;
float overslip;
float overslipscale = 25;


void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(115200);
  attachInterrupt(0, count0, FALLING);
  attachInterrupt(1, count1, FALLING);
  attachInterrupt(2, count2, FALLING);
  attachInterrupt(3, count3, FALLING );
}

void loop() {
 
  if((micros() - nxtLastPulse0) > 400099)
  {
  rate0 = 0;
  }
  else
  {
  average0 = 0;
  sum0= 0;
  for (i0 = 0; i0 < SnumRead; i0++)
  {
    sum0 = sum0 + pulse0[i0];
  }
  average0 = sum0 / SnumRead;
  rate0 =  correction1 * ((float)1000000 / average0);
  }
 
  if((micros() - nxtLastPulse1) > 400099)
  {
  rate1 = 0;
  }
  else
  {
  average1 = 0;
  sum1= 0;
  for (i1 = 0; i1 < SnumRead; i1++)
  {
    sum1 +=pulse1[i1];
  }
  average1 = sum1 / SnumRead;
  rate1 =  correction1 * ((float)1000000 / average1);
  }
 
 
  if((micros() - nxtLastPulse2) > 400099)
  {
  rate2 = 0;
  }
  else
  {
  average2 = 0;
  sum2= 0;
  for (i2 = 0; i2 < SnumRead; i2++)
  {
    sum2 +=pulse2[i2];
  }
  average2 = sum2 / SnumRead;
  rate2 =  correction2 * ((float)1000000 / average2);
  }
 
 
  if((micros() - nxtLastPulse3) > 400099)
  {
  rate3 = 0;
  }
  else
  {
  average3 = 0;
  sum3= 0;
  for (i3 = 0; i3 < SnumRead; i3++)
  {
    sum3 +=pulse3[i3];
  }
  average3 = sum3 / SnumRead;
  rate3 =  correction3 * ((float)1000000 / average3);
  }
 


  threshold = analogRead(pot);
  threshold = threshold / 48.71;
  slip = max(rate0, rate1) / max(rate2, rate3);
  slip = 100 * (slip - 1);
  overslip = slip - threshold;
  if (overslip < 0)
  {
  overslip = 0;
  }
  else if (overslip > overslipscale)
  {
  overslip = overslipscale;
  }
 
  Serial.println("");
  Serial.print(rate0,2);
  Serial.print(" / ");
  Serial.print(rate1,2);
  Serial.print(" / ");
  Serial.print(rate2,2);
  Serial.print(" / ");
  Serial.print(rate3),2;
  Serial.print(" --- ");
  Serial.print(threshold),2;
  Serial.print(" / ");
  Serial.print(slip),2;
  Serial.print(" / ");
  Serial.print(overslip),2;
 
  if (threshold < 21)
  {
  overslip = map(overslip, 0, overslipscale, 0, 255);
  analogWrite(11, overslip);
  digitalWrite(13, HIGH);
  }
  else
  {
  analogWrite(11, 0);
  digitalWrite(13, LOW);
  }

  if (max(max(rate0, rate1), max(rate2, rate3)) <= 30)
  {
  SnumRead = 1;
  }
  if (max(max(rate0, rate1), max(rate2, rate3)) > 30)
  {
  SnumRead = 5;
  }

  Serial.print(" / ");
  Serial.print(SnumRead); 
  Serial.print(" / ");
 
 
  delay(5);
}

void count0()
{
  nowPulse0 = micros();
  nxtLastPulse0 = nowPulse0;
  pulse0[pIndex0] = nowPulse0 - lastPulse0;
  pIndex0++;
  if (pIndex0 >= SnumRead)
    pIndex0 = 0;
  lastPulse0 = nowPulse0;
  }
void count1()
{
  nowPulse1 = micros();
  nxtLastPulse1 = nowPulse1;
  pulse1[pIndex1] = nowPulse1 - lastPulse1;
  pIndex1++;
  if (pIndex1 >= SnumRead)
    pIndex1 = 0;
  lastPulse1 = nowPulse1;
  nowPulse1 = 0;
}
void count2()
{
  nowPulse2 = micros();
  nxtLastPulse2 = nowPulse2;
  pulse2[pIndex2] = nowPulse2 - lastPulse2;
  pIndex2++;
  if (pIndex2 >= SnumRead)
    pIndex2 = 0;
  lastPulse2 = nowPulse2;
  nowPulse2 = 0;
}
void count3()
{
  nowPulse3 = micros();
  nxtLastPulse3 = nowPulse3;
  pulse3[pIndex3] = nowPulse3 - lastPulse3;
  pIndex3++;
  if (pIndex3 >= SnumRead)
    pIndex3 = 0;
  lastPulse3 = nowPulse3;
  nowPulse3 = 0;
}
Logged

Pages: 1 [2]   Go Up
Jump to: