Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: dynamic array? or varrying the size of the array based on something? on: December 13, 2012, 11:05:47 pm
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;
}
2  Using Arduino / Programming Questions / Re: dynamic array? or varrying the size of the array based on something? on: December 13, 2012, 12:40:23 am
i still dont think its quite right;  SnumRead is set to 5; which is 6 spots in an array.

for that bit of code in the loop i should have a sample size of 6 samples but then to figure the avage its dividing by SnumRead which is set to 5. but the frequency is right (output matches my function gen)?

hmmmmmmmmm  anyone have some insight?
3  Using Arduino / Programming Questions / Re: dynamic array? or varrying the size of the array based on something? on: December 13, 2012, 12:29:59 am
i think i fixed it;  i removed the = sign in my for loop


Code:
average0 = 0;
  sum0= 0;
  for (i0 = 0; i0 <= (SnumRead); i0++)
  {
    sum0 = sum0 + pulse0[i0];
  }
  average0 = sum0 / SnumRead;
  rate0 =  (float)1000000 / average0;
4  Using Arduino / Programming Questions / Re: dynamic array? or varrying the size of the array based on something? on: December 13, 2012, 12:20:35 am
anyone have any bright ideas why my math isn't working?   if i go over 30hz the rate0 jumps.  i know its adding another value in there but if i fix the math in the loop (SnumRread + 1) then its wrong if i go under 30hz.....


here is the code thats not working:
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;

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

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

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

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() {
  average0 = 0;
  sum0= 0;
  for (i0 = 0; i0 <= (SnumRead); i0++)
  {
    sum0 = sum0 + pulse0[i0];
  }
  average0 = sum0 / (SnumRead + 1);
  rate0 =  (float)1000000 / average0;
 
 
  average1 = 0;
  sum1= 0;
  for (i1 = 0; i1 <= (SnumRead + 1); i1++)
  {
    sum1 +=pulse1[i1];
  }
  average1 = sum1 / (SnumRead + 1);
  rate1 =  (float)1000000 / average1;
 
 
 
  average2 = 0;
  sum2= 0;
  for (i2 = 0; i2 <= (SnumRead + 1); i2++)
  {
    sum2 +=pulse2[i2];
  }
  average2 = sum2 / (SnumRead + 1);
  rate2 =  (float)1000000 / average2;
 
 
  average3 = 0;
  sum3= 0;
  for (i3 = 0; i3 <= (SnumRead + 1); i3++)
  {
    sum3 +=pulse3[i3];
  }
  average3 = sum3 / (SnumRead + 1);
  rate3 =  (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 (rate0 <= 30)
  {
  SnumRead = 1;
  }
  else if (rate0 > 30)
  {
  SnumRead = 5;
  }

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

void count0()
{
  nowPulse0 = micros();
  pulse0[pIndex0] = nowPulse0 - lastPulse0;
  pIndex0++;
  if (pIndex0 > (SnumRead))
    pIndex0 = 0;
  nxtLastPulse0 = lastPulse0;
  lastPulse0 = nowPulse0;
  }
void count1()
{
  nowPulse1 = micros();
  pulse1[pIndex1] = nowPulse1 - lastPulse1;
  pIndex1++;
  if (pIndex1 > (SnumRead + 1))
    pIndex1 = 0;
  lastPulse1 = nowPulse1;
  nowPulse1 = 0;
}
void count2()
{
  nowPulse2 = micros();
  pulse2[pIndex2] = nowPulse2 - lastPulse2;
  pIndex2++;
  if (pIndex2 > (SnumRead + 1))
    pIndex2 = 0;
  lastPulse2 = nowPulse2;
  nowPulse2 = 0;
}
void count3()
{
  nowPulse3 = micros();
  pulse3[pIndex3] = nowPulse3 - lastPulse3;
  pIndex3++;
  if (pIndex3 > (SnumRead + 1))
    pIndex3 = 0;
  lastPulse3 = nowPulse3;
  nowPulse3 = 0;
}
5  Using Arduino / Programming Questions / Re: dynamic array? or varrying the size of the array based on something? on: December 12, 2012, 11:45:30 pm
Quote
i meant the latter;
Us engineers are a pedantic lot who tend to take things literally smiley Part of the job description I suppose.

______
Rob

understandable lol

i'm still very new to all this so all the proper nomenclature eludes me :-/
6  Using Arduino / Programming Questions / Re: dynamic array? or varrying the size of the array based on something? on: December 12, 2012, 10:39:47 pm
My project measures wheel speed by measuring time between interrupts.

This might help:

http://gammon.com.au/forum/?id=11504

Two techniques are described near the start of that thread. Counting (getting the frequency directly) and measuring the period and inverting it. One is better for low frequencies and one is better for high frequencies.

the frequencies i'm dealing with are much slower; from about 10hz-2000hz so i'd have to have a huge window for it to count in which would make the response of the output unuseable.  the method i have works and with enough smoothing seems to work well.

i played with the variable sized index for a 8 space array and got it to work but i think my math is broke somewhere. i'll have to spend more time on it.

PaulS commented on this
Quote
the ISR changes the position of itself in the array

You are now saying this
Quote
populating the various slots in the array.  if i change the sample size it makes a difference.

Two totally different things.

Quote
eave the array with 8 spaces but at low speed only use the first 2 then at higher speed maybe 4 then highest speed use all 8.  i'll have to try it but i think it'll work better than trying to reallocate memory
Sounds like a better plan.

_____
Rob

i meant the latter; the isr changes the index position for the array or resets it to 0 if +1 is too many
7  Using Arduino / Programming Questions / Re: dynamic array? or varrying the size of the array based on something? on: December 11, 2012, 11:51:21 pm
Quote
right now the ISR changes the position of itself in the array and i want to keep that code small so i dont possibly miss any other interupts.
No, it doesn't. The ISR is a function. It is not an array element, so it can't move itself in the array.

There are ways to dynamically define arrays. You are advised not to use them. The Arduino (328-based, anyway) has little memory. Fragmenting that doing dynamic memory allocation is not advised. Clearly, you are not using a 328 based Arduino, since it does not have 4 interrupt pins. Still, small, static arrays are best.

what do you mean it doesnt?

Code:
 nowPulse2 = micros();
  pulse2[pIndex2] = nowPulse2 - lastPulse2;
  pIndex2++;
  if (pIndex2 > SnumRead)
    pIndex2 = 0;
  lastPulse2 = nowPulse2;
  nowPulse2 = 0;

it seems like its populating the various slots in the array.  if i change the sample size it makes a difference.


Quote
is it possible?

Yes.

Quote
My project measures wheel speed by measuring time between interrupts.  At low speed (<20hz) its fairly accurate and i can use a small array of 1 or 2 readings for fast response; but at 800hz and up i need a bigger array (8-10) to smooth out the readings.

You do not need such (large) arrays to measure wheel speed. Maybe you want to rethink your approach instead.


care to elaborate on this?  i need fairly high resolution (measure slip in 1/10 of a percent) so i'm measruring time between pulses.  the way i have it setup works great but at high speed the small size of the array leads to some big jumps; upping the size makes it happier.

/edit:  my first attempt was just counting pulses but at low speed the reaction was way too slow; i need to update the output based on slip at least every 10ms (100hz). even at 20hz there wasn't enough pulses to have enough resolution to have useful data.  this timing of pullses works much better.



i think i have an idea though;  leave the array with 8 spaces but at low speed only use the first 2 then at higher speed maybe 4 then highest speed use all 8.  i'll have to try it but i think it'll work better than trying to reallocate memory
8  Using Arduino / Programming Questions / dynamic array? or varrying the size of the array based on something? on: December 11, 2012, 05:21:36 am
is it possible?

My project measures wheel speed by measuring time between interrupts.  At low speed (<20hz) its fairly accurate and i can use a small array of 1 or 2 readings for fast response; but at 800hz and up i need a bigger array (8-10) to smooth out the readings.

is it possible to have an array that changes size dynamically?    if i tru declaring the array as anything but constant i get errors.

right now the ISR changes the position of itself in the array and i want to keep that code small so i dont possibly miss any other interupts.

anyway here is my in progress code:

Code:
const int SnumRead = 8;
const int pot = A0;

volatile unsigned long pulse0[SnumRead];
volatile unsigned long pIndex0 = 0;
volatile unsigned long nowPulse0 = 0;
volatile unsigned long lastPulse0 = 0;
volatile unsigned long i0, sum0, average0 = 0;
float rate0;

volatile unsigned long pulse1[SnumRead];
volatile unsigned long pIndex1 = 0;
volatile unsigned long nowPulse1 = 0;
volatile unsigned long lastPulse1 = 0;
volatile unsigned long i1, sum1, average1 = 0;
float rate1;

volatile unsigned long pulse2[SnumRead];
volatile unsigned long pIndex2 = 0;
volatile unsigned long nowPulse2 = 0;
volatile unsigned long lastPulse2 = 0;
volatile unsigned long i2, sum2, average2 = 0;
float rate2;

volatile unsigned long pulse3[SnumRead];
volatile unsigned long pIndex3 = 0;
volatile unsigned long nowPulse3 = 0;
volatile unsigned long lastPulse3 = 0;
volatile unsigned long i3, sum3, average3 = 0;
float rate3;

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() {
  average0 = 0;
  sum0= 0;
  for (i0 = 0; i0 <= SnumRead; i0++)
  {
    sum0 +=pulse0[i0];
  }
  average0 = sum0 / SnumRead;
  rate0 =  (float)1000000 / average0;
 
 
  average1 = 0;
  sum1= 0;
  for (i1 = 0; i1 <= SnumRead; i1++)
  {
    sum1 +=pulse1[i1];
  }
  average1 = sum1 / SnumRead;
  rate1 =  (float)1000000 / average1;
 
 
 
  average2 = 0;
  sum2= 0;
  for (i2 = 0; i2 <= SnumRead; i2++)
  {
    sum2 +=pulse2[i2];
  }
  average2 = sum2 / SnumRead;
  rate2 =  (float)1000000 / average2;
 
 
  average3 = 0;
  sum3= 0;
  for (i3 = 0; i3 <= SnumRead; i3++)
  {
    sum3 +=pulse3[i3];
  }
  average3 = sum3 / SnumRead;
  rate3 =  (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);
  }
 
 
  delay(5);
}

void count0()
{
  nowPulse0 = micros();
  pulse0[pIndex0] = nowPulse0 - lastPulse0;
  pIndex0++;
  if (pIndex0 > SnumRead)
    pIndex0 = 0;
  lastPulse0 = nowPulse0;
  }
void count1()
{
  nowPulse1 = micros();
  pulse1[pIndex1] = nowPulse1 - lastPulse1;
  pIndex1++;
  if (pIndex1 > SnumRead)
    pIndex1 = 0;
  lastPulse1 = nowPulse1;
  nowPulse1 = 0;
}
void count2()
{
  nowPulse2 = micros();
  pulse2[pIndex2] = nowPulse2 - lastPulse2;
  pIndex2++;
  if (pIndex2 > SnumRead)
    pIndex2 = 0;
  lastPulse2 = nowPulse2;
  nowPulse2 = 0;
}
void count3()
{
  nowPulse3 = micros();
  pulse3[pIndex3] = nowPulse3 - lastPulse3;
  pIndex3++;
  if (pIndex3 > SnumRead)
    pIndex3 = 0;
  lastPulse3 = nowPulse3;
  nowPulse3 = 0;
}
9  Using Arduino / Programming Questions / Re: Calculating the difference in a set of variables in percent on: December 05, 2012, 02:51:13 am
Thanks peter,

for the sake of having more numbers to play with i stopped dividing the 2 sets of pulses for drive and non drive and just add them.  This is working better but I want to speed it up and implement some kind of smoothing.  i also thought about instead of counting pulses i should somehow time the pulses for better resolution;  the shorter the time between pulses the faster the wheel turns. so i could have the program react even faster than ever 100ms.  i could take 5-10 samples and use that for smoothing and still have it react faster than this at 200ms. 
or am i going about this all wrong and should i just calculate the slip on the fly and get rid of the delay function?   if i do that though then i'll really have a problem with resolution;  right now i have to wait 200ms to get enough pulses to do decent math with any shorter and the error goes through the roof

here is the code as it sits:
Code:
  const int TxPin = 6;
  const int output = 11;
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
  int pot = A0;
  volatile int pulse0 = 0;
  volatile int pulse1 = 0;
  volatile int pulse2 = 0;
  volatile int pulse3 = 0;
  volatile int drive = 0;
  volatile int nondrive = 0;
  volatile int diff = 0;
  volatile int slip = 0;
  volatile int threshold = 0;
  volatile int overslip = 0;
 
void setup() {
  mySerial.begin(9600);
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
  mySerial.write(22);
  mySerial.write(17);
  mySerial.write(212);                  // set Quarter note tone
  mySerial.write(220);                  // A note tone
  mySerial.write(12);                   // blank disp
  mySerial.print("CompJLT INC");        // First line
  mySerial.write(13);                   // Form feed
  mySerial.print("Traction Control");   // Second line
  delay(4000);
  mySerial.write(12);
  attachInterrupt(0, count0, RISING);
  attachInterrupt(1, count1, RISING);
  attachInterrupt(2, count2, RISING);
  attachInterrupt(3, count3, RISING);
 
}
void loop() {
  mySerial.write(12);
  delay(5);
  noInterrupts();
  drive = pulse0 + pulse1;
  nondrive = pulse3 + pulse3;
  diff = (drive - nondrive);
  slip = (100 * diff) / nondrive;
  interrupts(); 
  mySerial.print(drive);
  mySerial.print("/");
  mySerial.print(nondrive);
  mySerial.print(" ");
  mySerial.write(13); 
  mySerial.print("S:");
  mySerial.print(slip);
  threshold = analogRead(pot);
  threshold = map(threshold, 0, 1023, 0, 100);
  mySerial.print(" T:");
  mySerial.print(threshold);
 
  overslip = slip - threshold;
  if (overslip > 0)
  {
  if (overslip > 100)
  {
  analogWrite(output, 255);
  }
  else
  {   
  overslip = map(overslip, 0, 100, 0, 255);
  analogWrite(output, overslip);
  }
  }
  else
  {
  analogWrite(output, 0);
  }
 
  mySerial.print(" O:");
  mySerial.print(overslip);
   
  pulse0 = 0;
  pulse1 = 0;
  pulse2 = 0;
  pulse3 = 0;
  overslip = 0;
  delay(200);
}
void count0()
{
  pulse0++;
}
void count1()
{
  pulse1++;
}
void count2()
{
  pulse2++;
}
void count3()
{
  pulse3++;
}
10  Using Arduino / Programming Questions / Re: Calculating the difference in a set of variables in percent on: December 04, 2012, 05:47:21 am
I think so. Try it.

holy sheet that worked lol!

i'm so happy smiley-grin

thank you so much!!

i can go to bed now at 5am lol
11  Using Arduino / Programming Questions / Re: Calculating the difference in a set of variables in percent on: December 04, 2012, 05:41:02 am
If you use integer Mathis you end up with zero.int is ok for the counters, but you need to have floats for the calculated vars.

so i need to change the variables for drive and nondrive to float vs. int?
12  Using Arduino / Programming Questions / Calculating the difference in a set of variables in percent on: December 04, 2012, 04:47:14 am
ok guys i'm stuck, lol... 

I've made really good progress on my project but i'm stuck.  I really have to give it up for how easy this audrino is to program on, i went from knowing almost nothing about code to within 2 days coming up with this.

I'm using an audrino leonardo and i'm sampling 4 ABS wheel speed sensors on an automobile, averaging the front and the rear separately and trying to calculate slip % based on the difference.  Right now i'm just using an agilent function generator and an audrino uno as a 2nd function generator to simulate 2 different +5vpp square wave inputs (split to the 4 inputs).  I'll be using 2 max9926 chips to condition the vr sensors (or hall effect) to give me +5vpp signals, once they're actually in a car. i've already got that setup working so the hardware i done with i'm just stuck on the software.

I have the 4 inputs working using interrupts, i have a cycle that repeats every 250ms or so (i need fairly fast response) so it counts the pulses, averages them and displays it all on an lcd (lcd is just for debugging right now) then clears the pulse count.

I can get the pulses for each input, i can average and i can subtract the difference and get that to display. the problem is is when i try to get a slip %. I need it to take the speed of the non drive wheels, subtract that from the drive wheels, get the difference then divide the non drive speed by the difference.  For example drive wheels are spinning at 90mph, non drive wheels are at 60mph, difference is 30mph; 30/60= .5 or 50%. 

All I can get to display is 0, or 0.00 when i set that variable as float or try to use floating point math coming up with the "slip" variable.  Eventually I need to get the slip amount to output a "voltage" using the pwm trick mapped out by the threshold set by a potentiometer.

Anyway here is my code, hopefully someone can point out what i'm doing wrong:

Code:
volatile int pulse0 = 0;
volatile int pulse1 = 0;
volatile int pulse2 = 0;
volatile int pulse3 = 0;

int pot = A0;
int threshold = 0;

const int TxPin = 6;
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);


void setup() {
  mySerial.begin(9600);
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
  mySerial.write(22);
  mySerial.write(17);
  attachInterrupt(0, count0, RISING);
  attachInterrupt(1, count1, RISING);
  attachInterrupt(2, count2, RISING);
  attachInterrupt(3, count3, RISING);
  }

void loop() {
  mySerial.write(12);
  delay(5);
  noInterrupts();
  mySerial.print(pulse0);
  mySerial.print("/");
  mySerial.print(pulse1);
  mySerial.print(" ");
  mySerial.print(pulse2);
  mySerial.print("/");
  mySerial.print(pulse3);
  mySerial.write(13);
  int drive = (pulse0 + pulse1) / 2;
  mySerial.print(drive);
  mySerial.print("/");
  int nondrive = (pulse3 + pulse3) / 2;
  mySerial.print(nondrive);
  mySerial.print(" SLP:");
  int diff = (drive - nondrive);
  int slip = diff / nondrive;
  mySerial.print(slip);
 
 
 // threshold = analogRead(pot);
 // threshold = map(threshold, 0, 1023, 0, 100);
 // mySerial.print("THR:");
 // mySerial.print(threshold);
   
  pulse0 = 0;
  pulse1 = 0;
  pulse2 = 0;
  pulse3 = 0;
  interrupts();
  delay(243);
  }
void count0()
{
  pulse0++;
}
void count1()
{
  pulse1++;
}
void count2()
{
  pulse2++;
}
void count3()
{
  pulse3++;
}

Pages: [1]