Go Down

### Topic: help with math (Read 1 time)previous topic - next topic

#### flega

##### Jan 17, 2013, 05:02 pm
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= ((minutes*60)+seconds)/(number of pass*49)*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 ?

#### PaulS

#1
##### Jan 17, 2013, 05:04 pm
Quote
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.

#### JimboZA

#2
##### Jan 17, 2013, 05:10 pm
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?
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

#### ckiick

#3
##### Jan 17, 2013, 05:29 pm
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);`
Chris J. Kiick
Robot builder and all around geek.

#### flega

#4
##### Jan 17, 2013, 08:03 pm

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?

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

#### flega

#5
##### Jan 17, 2013, 08:15 pm

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 74HC595const int latchPin = 8;//Pin connected to clock pin (SH_CP) of 74HC595const int clockPin = 12;////Pin connected to Data in (DS) of 74HC595const int dataPin = 11;//Pin connected to data of third 595const 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 beepser.print('\f');  ser.println("50m :"); //number of passresponse=get_int(); // Get an integer number from phi-panelint pass=response;ser.print('\f');ser.println("Metars:");ser.print(pass*50);ser.print(" m");delay(1000);ser.print('\f');  ser.println("Enter minutes:"); //minutesresponse=get_int(); // Get an integer number from phi-panelint minutes=response;ser.print('\f');ser.print(minutes);ser.print(" minutes");delay(1000);ser.print('\f'); ser.println("Enter seconds:"); // secondsresponse=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

#6
##### Jan 17, 2013, 08:19 pm
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

#7
##### Jan 17, 2013, 08:31 pm
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

#8
##### Jan 17, 2013, 09:22 pm

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

#### flega

#9
##### Jan 17, 2013, 09:49 pm

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!!

#### JimboZA

#10
##### Jan 18, 2013, 03:17 am
Quote
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"
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

Go Up

Please enter a valid email to subscribe