Pages: [1]   Go Down
 Author Topic: help with math  (Read 355 times) 0 Members and 1 Guest are viewing this topic.
Croatia
Offline
Newbie
Karma: 0
Posts: 17
 « on: January 17, 2013, 11:02:31 am » Bigger Smaller Reset

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 ?
 Logged

Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35566
Seattle, WA USA
 « Reply #1 on: January 17, 2013, 11:04:37 am » Bigger Smaller Reset

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.

 Logged

Johannesburg UTC+2
Offline
Edison Member
Karma: 34
Posts: 1705
 « Reply #2 on: January 17, 2013, 11:10:59 am » Bigger Smaller Reset

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?
 Logged

IT Crowd:
Roy... "Have you tried turning it off and on again?"
Moss.. "Have you tried forcing an unexpected reboot?"

Austin, TX
Offline
Full Member
Karma: 1
Posts: 170
 « Reply #3 on: January 17, 2013, 11:29:19 am » Bigger Smaller Reset

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:
delay = 1000UL * (m * 60UL)) + s) / (passes * 49);
 Logged

Chris J. Kiick
Robot builder and all around geek.

Croatia
Offline
Newbie
Karma: 0
Posts: 17
 « Reply #4 on: January 17, 2013, 02:03:39 pm » Bigger Smaller Reset

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
 Logged

Croatia
Offline
Newbie
Karma: 0
Posts: 17
 « Reply #5 on: January 17, 2013, 02:15:27 pm » Bigger Smaller Reset

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:
delay = 1000UL * (m * 60UL)) + s) / (passes * 49);

what is UL?

here is part od code:
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

 Logged

Netherlands
Online
Tesla Member
Karma: 90
Posts: 9436
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #6 on: January 17, 2013, 02:19:12 pm » Bigger Smaller Reset

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

 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Netherlands
Online
Tesla Member
Karma: 90
Posts: 9436
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #7 on: January 17, 2013, 02:31:55 pm » Bigger Smaller Reset

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:

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

 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Croatia
Offline
Newbie
Karma: 0
Posts: 17
 « Reply #8 on: January 17, 2013, 03:22:02 pm » Bigger Smaller Reset

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
 Logged

Croatia
Offline
Newbie
Karma: 0
Posts: 17
 « Reply #9 on: January 17, 2013, 03:49:52 pm » Bigger Smaller Reset

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:
delay = 1000UL * (m * 60UL)) + s) / (passes * 49);

it works! thank a lot!!
 Logged

Johannesburg UTC+2
Offline
Edison Member
Karma: 34
Posts: 1705
 « Reply #10 on: January 17, 2013, 09:17:34 pm » Bigger Smaller Reset

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"
 Logged

IT Crowd:
Roy... "Have you tried turning it off and on again?"
Moss.. "Have you tried forcing an unexpected reboot?"

 Pages: [1]   Go Up