Pages: 1 ... 4 5 [6] 7 8   Go Down
Author Topic: how to create an array with both members and number of members undefined  (Read 4280 times)
0 Members and 1 Guest are viewing this topic.
Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There are some free memory functions on the site somewhere that might be of use.

Do you mean functions that take no memory and can help with the various calculations I'll need?

Functions that show you how much free memory you have left.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
unsigned int servoPos[valNumber];
declare the array with 8 bit ints iso 16bit ints, as the value of the servo is always 0..179  WIll halve the size of the array

uint8_t servoPos[valNumber];

Great!
Does that mean I can also do this for:
Code:
const unsigned int valNumber = 500;            //variable to contain number of array members

And:
Code:
const int waitForServo = 5;                            //delay time to let servo get to position

?
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 50
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rob's suggestions will get you plenty of space and freemem can warn you if you get close to the edge again.

However, you might consider modelling it in a slightly different way: store a new servo value only when it changes. In a companion array, keep the time of the change. Depending on how many movements you want to record, this may well use less memory. You can probably store the time as 10ths or 100ths of a second since recording began, in which case, unsigned int would likely be enough.

On another topic, you still have this going on:
Code:
      for (int x=3;x>6;x++)

Consider putting it in a function so you only have to correct it in one place  smiley-wink
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh man...  smiley-roll
Thanks.
After I'll jump through all the hoops I've set for myself, I need to clean this code...
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 50
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just a suggestion, but you might well find it easier to jump through all those hoops after you've cleaned the code.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, doing it right now actually...

Setting up functions where I repeat stuff or where I find it too complicated to read and figure out the different tasks...

Would appreciate an answer to this as part of my spring cleaning:
Quote
Quote from: robtillaart on Today at 06:27:14 PM
Code:
unsigned int servoPos[valNumber];
declare the array with 8 bit ints iso 16bit ints, as the value of the servo is always 0..179  WIll halve the size of the array

uint8_t servoPos[valNumber];

Great!
Does that mean I can also do this for:
Code:
const unsigned int valNumber = 500;            //variable to contain number of array members

And:
Code:
const int waitForServo = 5;                            //delay time to let servo get to position

?
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 50
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I assume you're asking about using uint8_t for these two values. For waitForServo, sure. The other one is too big to fit in a byte. Personally, I wouldn't bother - you'd be saving a byte. RAM is short, but you're not that close. Get rid of buffer instead if you're looking to penny pinch.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok - after all this going about I finally decided it would be much easier and ram efficient to record and playback in a different manner altogether:
start recording and save both time(inTime) and position (inPos)
stop recording and save both time (outTime) and position (outPos)

and then when playing back do the same thing:
start playing from IN position and get to OUT position at the delta time (inTime - outTime)

For this to work smoothly I think I need to do delta time divided by delta position, right?
That is the only way the servo will move smoothly over time from one (In) position to the other (Out).

Something is still missing here...  
« Last Edit: March 31, 2012, 05:14:13 am by Soffer » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12482
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
const unsigned int valNumber = 500; 
you can use #define VALNUMBER 500 instead ... (will not save much space.

Quote
For this to work smoothly I think I need to do delta time divided by delta position, right?
Don't know what you mean by that.  time/angle is a measure of angular speed ?

you should have something like

duration in millis(), position pairs =>
10  0      goto angle 0 for 10 mills
100 10    goto position 10 for 100 mills
250 179
100 140
etc

Advice First make the datatype to hold this (2 arrays with equal indices) and the play-mode.

something like :
Code:
unsigned int duration[VALNUMBER];
uint8_t position[VALNUMBER];

unsigned long lasttime = 0;
void play()
{
if (millis() -lastTime > duration[idx])
{
  lastTime += duration[idx];
  idx++;
  digitalWrite(PIN, position[idx]);
}
}

you should use an unsigned int to hold the duration, if time is longer than 65535 millis write another record, otherwise you must use a unsigned long for every duration which = 4 bytes and a real memory loss.

Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 0
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

robtillaart, what i'm thinking is much simpler:
I'm trying to code something that will store the value sent to the servo only at the beginning and ending if the recording, and also measure the time it took to get from "in" to "out".
Then I want in playback mode to make the arduino send values ranging from the first value (In) to the last value (Out), in the course of the exact time it took in the recording.
I thought it could ease up on the memory since I would not store array values between the two points (which are actually focus points in a lens). Makes sense, right?

I though this must be simple to code and somehow I get it all messed up!
the recording part is easy:
start record - store servo position and time
stop record - store servo position and time.

Now playing back is breaking my brains.
I though it should be:
Start play back from 1st servo position and increment value of position by one until you reach the last position - and do that with delays that equal the time the recording took divided by the last position value minus first position value...
Something like this:

Code:
  for (int i=inPos; i< outPos; i++) 
      {
        myservo.write (i);
        servoTime = ((outTime-inTime)/(outPos - inPos));
        delay (servoTime);
        Serial.println (i);   
 
And it simply does not work...

What am I (obviously) doing wrong?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12482
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

seems like one of those moments you should just go back to the drawing board and redo your project (in thought). With your current insights how would you like it to behave and how would you do that.

the only thing you know is that at a certain moment you turn the potmeter and the servo will adjust to a new position. How long will it stay at that new position? until the next new value of the potmeter. So the servo moves, then it rests, it moves, it rests, move, rest, move rest  etc

It is not so that the servo goes in one minute from pos A to pos B (that is what your formula looks like), reality (I think) is that will take 5 seconds to go to pos B and the servo will stay 55 seconds at pos B. Your playback tries to average/ smooth the movements in time...

In short you model (represented in the data model) should describe the behaviour you want, nothing more nothing less.

Rethink th behaviour you want, and decide what data is needed..


Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 0
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


the only thing you know is that at a certain moment you turn the potmeter and the servo will adjust to a new position. How long will it stay at that new position? until the next new value of the potmeter. So the servo moves, then it rests, it moves, it rests, move, rest, move rest  etc

It is not so that the servo goes in one minute from pos A to pos B (that is what your formula looks like), reality (I think) is that will take 5 seconds to go to pos B and the servo will stay 55 seconds at pos B. Your playback tries to average/ smooth the movements in time...


Actually Rob, that is exactly what I want to happen. The most simple thing. The servo moves a lens from one focus point to the other and freezes there. Permanently. Until I record a new movement. Or until I go "Free mode". The only variables that are important are the first position, the last position and the time it took to get there...
Pos A to  Pos B in the time it took to record. Stop.
If that's what my formula actually does than it should have worked...
 


Logged

New Jersey
Online Online
Faraday Member
**
Karma: 50
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This thread has been running for a while, so I apologize if I missed this earlier; what exactly are you trying to do? This latest twist is a little confusing.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This thread has been running for a while, so I apologize if I missed this earlier; what exactly are you trying to do? This latest twist is a little confusing.
smiley
I'm building a follow focus rig for a camera with the ability to record an play back the movement of the lens. Since I'm new at this, and this is my first project, I may have taken a detour, but what I've done thanks to you guys with the arrays will help me with next project.
SO - Since I'm trying to cut back on memory usage, I'm thinking of loosing the arrays and simplify the code.
Logged

Pages: 1 ... 4 5 [6] 7 8   Go Up
Jump to: