help with math

Hi everyone,
I have developed project for help swimmers to train better. It consist of 25 led's in pool bottom(each meter is one led) and they are lit in programmed time. I have developed input via liudr and led output via 74hc595. It works nice, but I can't make arduino to calculate right time for delay between led's.
There is a few variables that operator enter via keyboard: number of 50m passes of pool (integer, can be byte), pass time in minutes (it is easy to enter separately minutes then seconds) and seconds. Goal is to calculate time for delay. I think formula be like this: delay= ((minutes60)+seconds)/(number of pass49)*1000. why 49 and not 50? because it will be used in 25m long swimming pool and after 25 led is lit then 24...1 led is lit. So one led must be subtracted from every 50m pass.
When I enter formula like that in my program (delay is long unsigned integer) I don't have proper calculation. I have tried with float but this also don't work. Can somebody tell me how to calculate that delay ?

Can somebody tell me how to calculate that delay ?

You are probably having issues with integer arithmetic truncating values. Hard to be sure without seeing your code.

You say you have 25 LEDs.... Surely in a 25m pool you would have either 26 LEDs (if there are LEDs at the very ends) or 24 LEDs?

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.

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

JimboZA:
You say you have 25 LEDs.... Surely in a 25m pool you would have either 26 LEDs (if there are LEDs at the very ends) or 24 LEDs?

:astonished: upps, you're right. I must have 26 led's!! So, passes must be multiplied with 51 :grin:
thanks for suggestion, I will notice my mistake after build 25 meters of led strip :frowning:

ckiick:
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.

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

what is UL?

here is part od code:

#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
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.

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

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);
}

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.

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 :roll_eyes:

ckiick:
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.

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

it works! thank a lot!!

thanks for suggestion, I will notice my mistake after build 25 meters of led strip

Known historically as a "fence-post error", and now known as the "pool LED error" 8)