Pages: 1 ... 5 6 [7] 8   Go Down
Author Topic: how to create an array with both members and number of members undefined  (Read 5254 times)
0 Members and 1 Guest are viewing this topic.
Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can add 8k bytes of SPI RAM pretty cheap.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And it simply does not work...

So what does it do? Nothing? I assume you're not getting any debug output from the serial print.

One issue, at least as far as I can see from that snippet is that you assume that inpos is less than outpos. Have you ensured that it is?
Logged

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

Hey.
For the test I'm making sure outPos>inPos.
What I get back is delayTime = 0
and no playback - naturally, if the delayTime is 0...deltaPos=0...

I'm sure I'm doing something extremely stupid here...  smiley-confuse

Code:
//             Simple servo position recoder                                    

//    FILE: servoRecorder.pde
//  AUTHOR: Adi Soffer
//    DATE: 1-4-2012 B
//
// PUPROSE: servo recorder
//


#define FREE 1
#define RECORD 2
#define PLAYBACK 3
#define STOP 4
#define REWIND 5
#define RECORDCONT 6

int mode;
unsigned int maxidx = 500;

#include <Servo.h>

Servo myservo;                                                      // create servo object to control a servo
char buffer [2];                                                      //variable to hold keystrokes
unsigned int potpin = 0;                                   // analog pin used to connect the potentiometer
unsigned int val;                                                // variable to read the value from the analog pin
int incomingByte;                                               //variable to hold incoming letter
uint8_t waitForServo = 5;                               //delay time to let servo get to position
uint8_t lastPosition;

unsigned long inTime;                                                          //variable to hold time when recording starts          
unsigned long outTime;                                                        //variable to hold time when recording ends        
unsigned long deltaTime;             //var holding actual recording time
uint8_t inPos;                                                                          //var to hols "in" value
uint8_t outPos;                                                                        //var to hols "out" value
uint8_t deltaPos;                                //var to claculate num of steps
unsigned long servoTime;   //var to claculate time for step in playback


const byte recordModeLed = 3;                  // defining pins for ui leds
const byte homeModeLed = 4;
const byte playModeLed = 5;
const byte freeModeLed = 6;
const byte freeSwitch = 7;                         //defining pins for switches
const byte recSwitch = 8;
const byte homeSwitch = 9;
const byte playSwitch = 10;

void setup()
{
  Serial.begin(9600);
  Serial.flush();
  myservo.attach(2);                                                   // attaches the servo on pin 2 to the servo object
  pinMode (freeModeLed, OUTPUT);                         //defining LED pins as output
  pinMode (recordModeLed, OUTPUT);
  pinMode (homeModeLed, OUTPUT);
  pinMode (playModeLed, OUTPUT);
  pinMode (freeSwitch, INPUT);                              //defining switch pins as input
  pinMode (recSwitch, INPUT);
  pinMode (homeSwitch, INPUT);
  pinMode (playSwitch, INPUT);
  deltaPos = (outPos - inPos);
  servoTime = (deltaTime/deltaPos);
  deltaTime = (outTime - inTime);

}


void loop ()
{
  digitalWrite (freeModeLed, LOW);
  digitalWrite (recordModeLed, LOW);
  digitalWrite (homeModeLed, LOW);
  digitalWrite (playModeLed, LOW);

  // HANDLE IO

  if (Serial.available( ) > 0)
  {
    incomingByte = Serial.read ( );

    switch(incomingByte)
    {
    case 'f':
      mode = FREE;
      Serial.println ("Free mode");  
      break;

    case 'r':
      mode = RECORD;
      for (int c=6;c>3;c --)                                                             // three sec LED countdown to recording
      {
        digitalWrite (recordModeLed, HIGH);
        digitalWrite (c, HIGH);
        delay (700);
        digitalWrite (recordModeLed, LOW);
        digitalWrite (c, LOW);
        delay(300);
      }
      Serial.println ("Record");
      break;

    case 's' :
      mode = STOP;
      Serial.println ("STOP");
      break;

    case 'w' :
      mode = REWIND;
      digitalWrite (homeModeLed, HIGH);                    //turn on rewind LED
      Serial.println ("Rewind");
      break;

    case 'p':
      mode = PLAYBACK;
      digitalWrite (playModeLed, HIGH);                     //turn on playback LED
      Serial.println ("Playback");
      break;

    }
  }

  switch(mode)
  {

  case STOP:
    {
      Serial.print ("inPos:  ");                  //check change in var
      Serial.println (inPos);
      Serial.print ("outPos:");
      Serial.println (outPos);
      Serial.print ("Steps: ");
      Serial.println (deltaPos);
      Serial.print ("in time: ");
      Serial.println (inTime);
      Serial.print ("out Time: ");
      Serial.println (outTime);
      Serial.print ("time: ");
      Serial.println (deltaTime);
      stopFunction ( );
    }
    break;

  case FREE:
    digitalWrite (freeModeLed, HIGH);
    val = analogRead (potpin);
    val = map(val, 0, 1023, 0, 179);                           // val = val * 45/256; // <==> val * 180/1024
    myservo.write (val);
    Serial.println (val);                                    //print val for checking
    lastPosition = val;                                      //save last position for REWIND fixing
    delay (waitForServo);                                   // should be refactored away -> like blink without delay
    break;

  case RECORD:

    digitalWrite (recordModeLed, HIGH);        // keeps LED on
    Serial.println ("Record function");         // if so - prints record
    val = analogRead(potpin);                              // reads the value of the potentiometer  
    val = map(val, 0, 1023, 0, 179);                              // scale it to use it with the servo
    myservo.write(val);                                          // sets the servo position according to the scaled value
    inPos = val;                                                         //save in pos
    inTime = millis ( );                                            //save in time
    Serial.println (val);                                       // print values for checking

    mode = RECORDCONT;

    break;

  case RECORDCONT:

    digitalWrite (recordModeLed, HIGH);       // keeps LED on
    val = analogRead(potpin);                             // reads the value of the potentiometer  
    val = map(val, 0, 1023, 0, 179);                             // scale it to use it with the servo  
    myservo.write(val);                                         // sets the servo position according to the scaled value
    outPos = val;                                                      //save out pos
    outTime = millis ( );                                        //save out time
    lastPosition = val;
    delay(waitForServo);                                      // waits for the servo to get there
    Serial.println(val);                                       // print values for checking

    break;

  case REWIND:                                                                          //need to build rewind from lastPosition in free mode
    if (outPos > inPos)
    {  
      for (int i=outPos;i>inPos;i--)
      {
        myservo.write (i);
        delay (waitForServo);
        Serial.println (i);                                                           // print values for checking
      }
      mode = STOP;
      break;
    }
    else if (outPos<inPos)
    {
      for (int i=outPos;i>inPos;i++)
      {
        myservo.write (i);
        delay (waitForServo);
        Serial.println (i);                                                           // print values for checking
      }
      mode = STOP;
      break;
    }
  case PLAYBACK:

    if (outPos>inPos)
    {
      for (int i=inPos; i< outPos; i++)  
      {
        myservo.write (i);
        //lastPosition = i;
        delay (servoTime);
        Serial.println (i);                                                           //print val for checking
      }
      mode = STOP;
      break;
    }
    else if (outPos<inPos)
    {
      for (int i=inPos; i<outPos; i--)  
      {
        myservo.write (i);
        //lastPosition = i;
        delay (servoTime);
        Serial.println (i);                                                           //print val for checking
      }
    }
  }


}



void stopFunction ( )
{
  for (int x=3;x<6;x++)
  {
    digitalWrite (x, LOW);                                 //turn off all LEDs
  }
}










« Last Edit: April 01, 2012, 11:41:07 am by Soffer » Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
For the test I'm making sure outPos>inPos.

For rewind, sure. Playback, not so much. Also, rewind and playback are so similar that you should be able to use a common function to do their work.
Logged

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

I'm making sure with the values I put in...
As for the same function for Rewind and Playback: you can see rewind uses a different delay (), playback is supposed to use a delay that will create the same timing as in the recording.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You only set servoTime in setup, when the values of the things you use to calculate it are all zero.

As to using a function for rewind and playback, I'd expect you to pass the delay to it as a parameter. InPos & outPos too of course.
Logged

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

You only set servoTime in setup, when the values of the things you use to calculate it are all zero.

As to using a function for rewind and playback, I'd expect you to pass the delay to it as a parameter. InPos & outPos too of course.

Told you I was doing something stupid...
So I'll put the calculations into the loop. Thanks.
Not sure about you 2nd comment though. I do not need the rewind to move at the same timing as playback

EDIT: Did and it works. Simple and great.
Thanks a lot!
« Last Edit: April 01, 2012, 11:42:19 am by Soffer » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4801
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Shouldn't the end position for one item in queue be the start position for the next?
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

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

Shouldn't the end position for one item in queue be the start position for the next?


Since this is meant to pull focus, no. Each "focus pulling" is a stand alone process.
Logged

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

Hello again!

I'm in the process of adapting my code to work with switches.
I'm using the Bounce library and building on its "change" sketch to help me work out the different "cases".
If you'll be kind enough to check out my code, you'll notice that BOTH REWIND sections commented out.
That is because the code works great until I add the "else if" section referring to the rewind mode (hbouncer).
Even with the REWIND case commented out and just with the "else if" section active (to check the flow), things get erratic.
I push the fButton and things go weird.
I'd be thankful for someone's practiced eye.

Code:


// Servo Recording - Switch Version//

#include <Servo.h>
#include <Bounce.h>


Servo myservo;

unsigned int potpin = 0;
unsigned int val;
uint8_t waitForServo = 5;                       //delay time to let servo get to position
uint8_t lastPosition;

//Defining Modes
int mode;
#define FREE 1
#define RECORD 2
#define PLAYBACK 3
#define STOP 4
#define REWIND 5
#define RECORDCONT 6

//Variables for Record and Playback
unsigned long inTime;                       //variable to hold time when recording starts           
unsigned long outTime;                     //variable to hold time when recording ends       
unsigned long deltaTime;                //var holding actual recording time
uint8_t inPos;                                       //var to hols "in" value
uint8_t outPos;                                     //var to hols "out" value
uint8_t deltaPos;                                //var to claculate num of steps
unsigned long servoTime;                //var to claculate time for step in playback

//Defining Switches
#define fBUTTON 7      //Free Mode Button
#define rBUTTON 8      //Record Button
#define hBUTTON 9      //Home Button
#define pBUTTON 10    //Play Button


//Defining LEDs
#define rLED 3     //Record LED
#define hLED 4     // Home LED
#define pLED 5     //Play LED
#define fLED 6     // Free Mode LED


int fledValue = LOW;  //Free led Value
int rledValue = LOW;  //Record led value
int hledValue = LOW;  //Home led value
int pledValue = LOW;  //Play led value

//Defining Bounce objects
Bounce fbouncer = Bounce( fBUTTON, 10 );  //Free Mode Bouncer
Bounce rbouncer = Bounce( rBUTTON, 10 );  //Record Bouncer
Bounce hbouncer = Bounce (hBUTTON, 10);   //Home Bouncer
Bounce pbouncer = Bounce (pBUTTON, 10);   //Play Bouncer

void setup() {
  Serial.begin(9600);
  Serial.flush();
  myservo.attach (2);
  pinMode(fBUTTON,INPUT);
  pinMode(rBUTTON,INPUT);
  pinMode (hBUTTON, INPUT);
  pinMode (pBUTTON, INPUT);
  pinMode (pLED, OUTPUT);
  pinMode (hLED, OUTPUT);
  pinMode(fLED,OUTPUT);
  pinMode(rLED,OUTPUT);
}

void loop() {
  //Record and Play Calculated
  deltaPos = abs (outPos - inPos);       
  deltaTime = (outTime - inTime);
  servoTime = (deltaTime/deltaPos);



  if ( fbouncer.update() ) {                              //if FREE Button pressed
    if ( fbouncer.read() == HIGH) {
      if ( fledValue == LOW ) {
        fledValue = HIGH;
        Serial.println ("Free In");
        mode = FREE;
      }
      else {
        fledValue = LOW;
        Serial.println ("Free Out");
        mode = STOP;
      }
      digitalWrite(fLED,fledValue);


    }
  }

  else if ( rbouncer.update() ) {                    //if REC Button pressed
    if ( rbouncer.read() == HIGH) {
      if ( rledValue == LOW ) {
        rledValue = HIGH;
        Serial.println ("Record In");
        mode = RECORD;
      }
      else {
        rledValue = LOW;
        Serial.println ("Record Out");
        mode = STOP;
      }
      digitalWrite(rLED,rledValue);
    }
  }

  /*
<<<<<<<<<<<<<<<<<<<<<This is the part that messes everything up>>>>>>>>>>>>>>>

else if ( hbouncer.update() ) {                    //if Reverse Button pressed
   if ( hbouncer.read() == HIGH) {
   if ( hledValue == LOW ) {
   hledValue = HIGH;
   Serial.println ("Rewinding");
   mode = REWIND;
   }
   else {
   hledValue = LOW;
   Serial.println ("Rewinding done");
   mode = STOP;
   }
   digitalWrite(hLED,hledValue);
   }
   }
   */




  switch (mode){

  case FREE:
    val = analogRead (potpin);
    val = map(val, 0, 1023, 0, 179);                           // val = val * 45/256; // <==> val * 180/1024
    myservo.write (val);
    Serial.println (val);                                    //print val for checking
    lastPosition = val;                                      //save last position for REWIND fixing
    delay (waitForServo);                                   // should be refactored away -> like blink without delay
    break;


  case RECORD:
    val = analogRead(potpin);                              // reads the value of the potentiometer 
    val = map(val, 0, 1023, 0, 179);                              // scale it to use it with the servo
    myservo.write(val);                                          // sets the servo position according to the scaled value
    inPos = val;                                                         //save IN pos
    inTime = millis ( );                                            //save IN time
    Serial.println (val);                                       // print values for checking

    mode = RECORDCONT;

    break;

  case RECORDCONT:
    val = analogRead(potpin);                             // reads the value of the potentiometer 
    val = map(val, 0, 1023, 0, 179);                             // scale it to use it with the servo 
    myservo.write(val);                                         // sets the servo position according to the scaled value
    outPos = val;                                                      //save OUT pos
    outTime = millis ( );                                        //save OUT time
    lastPosition = val;
    delay(waitForServo);                                      // waits for the servo to get there
    Serial.println(val);                                       // print values for checking

    break;

  case STOP:
    Serial.print ("IN POSITION: ");
    Serial.println (inPos);
    Serial.print ("OUT POSITION: ");
    Serial.println (outPos);
    Serial.print ("STEPS: ");
    Serial.println (deltaPos);
    Serial.print ("IN TIME: ");
    Serial.println (inTime);
    Serial.print ("OUT TIME: ");
    Serial.println (outTime);
    Serial.print ("TOTAL REC TIME: ");
    Serial.println (deltaTime);

    break;
    /*
  case REWIND:                             //need to build rewind from lastPosition in free mode
     digitalWrite (hLED, HIGH);
     if (outPos > inPos)
     { 
     for (int i=outPos;i>inPos;i--)
     {
     myservo.write (i);
     delay (15);
     Serial.println (i);                                                           // print values for checking
     }
     hledValue = LOW;
     digitalWrite (hLED, LOW);
     mode = STOP;
     break;
     }
     else if (outPos<inPos)
     {
     for (int i=outPos;i<inPos;i++)
     {
     myservo.write (i);
     delay (15);
     Serial.println (i);                                                           // print values for checking
     }
     hledValue = LOW;
     digitalWrite (hLED, LOW);
     mode = STOP;
     break;
     }
     */

  case PLAYBACK:
    if (outPos>inPos)
    {
      for (int i=inPos; i< outPos; i++) 
      {
        myservo.write (i);
        //lastPosition = i;
        delay (servoTime);
        Serial.println (i);                                                           //print val for checking
      }
      mode = STOP;
      break;
    }
    else if (outPos<inPos)
    {
      for (int i=inPos; i>outPos; i--) 
      {
        myservo.write (i);
        //lastPosition = i;
        delay (servoTime);
        Serial.println (i);                                                           //print val for checking
      }
      mode = STOP;
      break;
    }
  }
}


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:
Think you should only set the mode after the press of a button and move the LED-code in the right case. Because the only thing a button or a serial key does is changing the state/mode of the device. If the device is in free mode that "state"knows what to do ;)

e.g.
 [code]

switch (mode){

  case FREE:
    setLedOn(FREE);
    val = analogRead (potpin);
    val = map(val, 0, 1023, 0, 179);                           // val = val * 45/256; // <==> val * 180/1024
    myservo.write (val);
    Serial.println (val);                                    //print val for checking
    lastPosition = val;                                      //save last position for REWIND fixing
    delay (waitForServo);                                   // should be refactored away -> like blink without delay
    break;
...

setLedOn(int ledpin)
{
  for (int i=0; i< #LEDS; i++) digitalWrite(LED[i], LOW);  //set all LEDS off
  digitalWrite( LED[ledpin], HIGH);  <<< the enum of mode used as index of an array of ledpinnumbers ;)
}

think you get the idea


[/code]

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 - thank you for your swift reply!

You mean that the fact that the LED on/off codes are in the "if...else" ladder makes the rewind part mess things up?
The whole thing behaves weird...
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


not necesarily, but you kept 4 flags indicating somewhat the same : mode == state and a 3 flags for ledstate. Ledstates which implicit follows from mode!
Is some sort of redundancy, removing it makes code more straightforward.
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


not necesarily, but you kept 4 flags indicating somewhat the same : mode == state and a 3 flags for ledstate. Ledstates which implicit follows from mode!
Is some sort of redundancy, removing it makes code more straightforward.

Rob, I apologize for being such a noob, but I do not understand what you're referring to...
Actually I was just posting my frustration when I saw your reply...
What am I doing wrong here? Please point me to my errors.
I'm very thankful for your patience here!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18722
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Rob is saying you are setting two variables:

Code:
       
        fledValue = HIGH;
...
        mode = FREE;

When the mode variable implies the other one.
Logged


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