Pages: [1]   Go Down
Author Topic: help with math  (Read 675 times)
0 Members and 1 Guest are viewing this topic.
Croatia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Online Online
Faraday Member
**
Karma: 91
Posts: 4249
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Austin, TX
Offline Offline
Full Member
***
Karma: 2
Posts: 182
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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?

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

Croatia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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++)
  {
    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);
}


Logged

Rob Tillaart

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

Croatia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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  smiley-roll
Logged

Croatia
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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
Online Online
Faraday Member
**
Karma: 91
Posts: 4249
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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"  smiley-cool
Logged

Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Pages: [1]   Go Up
Jump to: