Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 31
Posts: 2947
I only know some basic electricity....
|
 |
« Reply #90 on: April 01, 2012, 07:18:27 am » |
You can add 8k bytes of SPI RAM pretty cheap.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2354
|
 |
« Reply #91 on: April 01, 2012, 07:34:46 am » |
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
Full Member
Karma: 0
Posts: 161
|
 |
« Reply #92 on: April 01, 2012, 09:56:02 am » |
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...  // 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
Edison Member
Karma: 24
Posts: 2354
|
 |
« Reply #93 on: April 01, 2012, 10:11:56 am » |
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
Full Member
Karma: 0
Posts: 161
|
 |
« Reply #94 on: April 01, 2012, 10:14:39 am » |
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
Edison Member
Karma: 24
Posts: 2354
|
 |
« Reply #95 on: April 01, 2012, 11:27:20 am » |
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
Full Member
Karma: 0
Posts: 161
|
 |
« Reply #96 on: April 01, 2012, 11:34:13 am » |
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
Faraday Member
Karma: 31
Posts: 2947
I only know some basic electricity....
|
 |
« Reply #97 on: April 01, 2012, 03:38:20 pm » |
Shouldn't the end position for one item in queue be the start position for the next?
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 161
|
 |
« Reply #98 on: April 01, 2012, 11:08:23 pm » |
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
Full Member
Karma: 0
Posts: 161
|
 |
« Reply #99 on: April 03, 2012, 01:34:24 pm » |
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.
// 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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9429
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #100 on: April 03, 2012, 01:51:02 pm » |
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
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 161
|
 |
« Reply #101 on: April 03, 2012, 01:59:00 pm » |
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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9429
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #102 on: April 04, 2012, 12:56:02 am » |
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
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 161
|
 |
« Reply #103 on: April 04, 2012, 01:09:52 am » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #104 on: April 04, 2012, 01:32:45 am » |
Rob is saying you are setting two variables: fledValue = HIGH; ... mode = FREE;
When the mode variable implies the other one.
|
|
|
|
|
Logged
|
|
|
|
|
|