Go Down

Topic: help with math (Read 720 times) previous topic - next topic

flega


It would be much easier to help you if you posted the code, or at least gave some sample inputs and outputs.  Otherwise we are just guessing at what the problem is.

One thing is that to preserve accuracy, you need to re-arrange the calculation a little.  Also, if you want unsigned long results, you should use unsigned long constants and variables in the calculation as much as possible.

Code: [Select]

delay = 1000UL * (m * 60UL)) + s) / (passes * 49);




what is UL?

here is part od code:
Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial ser(2,3);

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
//Pin connected to data of third 595
const int dataPin2 =6;
byte pass=1;
unsigned long int wait= 500;

void setup() {
  ser.begin(19200);
  delay(100);
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin2, OUTPUT);
}

void loop() {
 
 
  //unos podataka
  ser.print("\eO770;1000~");//start beep



ser.print('\f'); 
ser.println("50m :"); //number of pass
response=get_int(); // Get an integer number from phi-panel
int pass=response;
ser.print('\f');
ser.println("Metars:");
ser.print(pass*50);
ser.print(" m");
delay(1000);

ser.print('\f'); 
ser.println("Enter minutes:"); //minutes
response=get_int(); // Get an integer number from phi-panel
int minutes=response;
ser.print('\f');
ser.print(minutes);
ser.print(" minutes");
delay(1000);

ser.print('\f');
ser.println("Enter seconds:"); // seconds
response=get_int();
int sec=response;
ser.print('\f');
ser.print(sec);
ser.println(" seconds");
delay(1000);


 
wait=(((minutes*60)+sec)*1000)/(49*pass); //calculation of delay
 

robtillaart

Code: [Select]
wait=(((minutes*60)+sec)*1000)/(49*pass); //calculation of delay
this does not fit in an integer, that is why you need the UL to make the math unsigned long.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

furthermore the speed of a swimmer is not constant near the turning points and when it starts. Also the speed tends to slow down as the swimmer gets more acid in his /her legs and arms.
==> one delay will not work.

If I was the trainer I would just use a potmeter on an analog input (averaged to get a stable value)  and map that value to the speed of the leds.
easier to make and easier to adjust during the training ...

something like this
Code: [Select]


int led = 0;
int direction = 1;

void setup()
{
  // set pinmodes and initial values right
}

void loop()
{
  int sum = 0;
  for (int i=0; i<16; I++)
  {
    sum += analogRead(A0); // 0..1023
  }
  long d = map(a, 0, 16*1023, 250, 250000); // to be adjusted minimum and maximum interled time
  delay(d);
  nextLED();
}

nextLED()
{
   digitalWrite(led, OFF);
   led = led + direction;
   if (led == 25 || led == 0) direction = -direction;
   digitalWrite(led, ON);
}



Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

flega


furthermore the speed of a swimmer is not constant near the turning points and when it starts. Also the speed tends to slow down as the swimmer gets more acid in his /her legs and arms.
==> one delay will not work.



this project is to swimmer learn to keep constant speed all the time. I'm not a trainer, I'm just a one who want to help kids in local swimming club. They ask me to do it and I'm trying to  :smiley-roll:

flega


It would be much easier to help you if you posted the code, or at least gave some sample inputs and outputs.  Otherwise we are just guessing at what the problem is.

One thing is that to preserve accuracy, you need to re-arrange the calculation a little.  Also, if you want unsigned long results, you should use unsigned long constants and variables in the calculation as much as possible.

Code: [Select]

delay = 1000UL * (m * 60UL)) + s) / (passes * 49);




it works! thank a lot!!

Go Up