Pages: 1 [2]   Go Down
Author Topic: record data from potentiometer & playback to servo  (Read 2086 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK - things are looking good and I've even written code for using my LEDs as a countdown till record and so forth...  smiley-grin
Now here's a question I can't seem to find an answer to, probably since it's too simple, and I would very much appreciate an insight...

I'm rewriting the code so that instead of key strokes, switches will operate the different functions, and I'm constantly reading about the Debounce thing - BUT - if I simply with to use my switches not as toggles, just when I press one a function goes on, do I still need this debouncing trick? just to make it clear I'm attaching my updated code, still with keystrokes. Except for the "free mode" all other function are self terminating and for them I need to use the switch simply as a trigger.
Thank you.


Code:
// Controlling a servo position using a potentiometer (variable resistor)
// with the option of recording the movement, playing it in reverse and in original order.
// Adi Soffer 19.3.12

#include <Servo.h>
 
   Servo myservo;                                         // create servo object to control a servo
   char buffer [5];                                         //variable to hold keystrokes
   int potpin = 0;                                          // analog pin used to connect the potentiometer
   int val;                                                       // variable to read the value from the analog pin
   const int valNumber = 500;                   //variable to contain number of array members
   int incomingByte;                                  //declare variable to hold incoming letter
   int servoPos[valNumber];                    //create array of values for servo position
   const int waitForServo = 15;               //delay time to let servo get to position
   int recordModeLed = 3;                              // defining pins for ui leds
   int homeModeLed = 4;
   int playModeLed = 5;
   int freeModeLed = 6;
   int freeSwitch = 7;                                 //defining pins for switches
   int recSwitch = 8;
   int homeSwitch = 9;
   int 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);
     
 }


  void loop ()
{
   
  digitalWrite (freeModeLed, LOW);
  digitalWrite (recordModeLed, LOW);
  digitalWrite (homeModeLed, LOW);
  digitalWrite (playModeLed, LOW);
 
  if (Serial.available( ) > 0)
 {
       incomingByte = Serial.read ( );
       if (incomingByte == 'f')
  {
          Serial.println ("Free mode");
          while (incomingByte == 'f')
    {
          digitalWrite (freeModeLed, HIGH);
          val = analogRead (potpin);
          val = map(val, 0, 1023, 0, 179);
          myservo.write (val);
          delay (waitForServo);
          if (Serial.available ( ) > 0)
      {
            incomingByte = Serial.read ( );
            if (incomingByte == 'r')
         {
             recordFunction ( );   
         }
           else if (incomingByte == 'h')
       {
           reverseFunction ( );
        }   
           else if (incomingByte = 'p')
        {
             playBackFunction ( );
         }   
       } 
     }
   } 
          else if (incomingByte == 'r')                   // checks if input was r
        {                       
           recordFunction ( );
        }
           else if (incomingByte == 'h')
       {
           reverseFunction ( );
        }
                 else if (incomingByte = 'p')
        {
           playBackFunction ( );
           }
        }
      }

void playBackFunction ( )
       {
         digitalWrite (freeModeLed, LOW);
          digitalWrite (playModeLed, HIGH);
          Serial.println ("Playback Function");
            for (int i=0;i<(valNumber - 1);i++)
          {
             myservo.write (servoPos[i]);                    //reads values from array in original order to servo
             delay (waitForServo);
             Serial.println (servoPos[i]);
          }
           digitalWrite (playModeLed, LOW);   
       }

 void reverseFunction ( )
      {
            digitalWrite (freeModeLed, LOW);
            digitalWrite (homeModeLed, HIGH);
            Serial.println ("Home function");
            for (int i=(valNumber - 1);i>0;i--)
          {
             myservo.write (servoPos[i]);                   //reads values from array in reverse to servo
             delay(waitForServo);
             Serial.println (servoPos[i]);
          }
          digitalWrite (homeModeLed, LOW);
       }
     
void recordFunction ( )
{
           for (int c=6;c>3;c --)                                     // three sec countdown to recording
           {
             digitalWrite (recordModeLed, HIGH);
             digitalWrite (c, HIGH);
             delay (700);
             digitalWrite (recordModeLed, LOW);
             digitalWrite (c, LOW);
             delay(300);
           }
           digitalWrite (recordModeLed, HIGH);
           Serial.println ("Record function");            // if so - prints record
           for (int i=0;i<(valNumber - 1) ;i++)                //for loop to store declared values of pot 
        {
              val = analogRead(potpin);                             // reads the value of the potentiometer (value between 0 and 1023)
              val = map(val, 0, 1023, 0, 179);                             // scale it to use it with the servo (value between 0 and 180)
              myservo.write(val);                                         // sets the servo position according to the scaled value
              servoPos [i]=val;                                              // stores val in array "servoPos
              delay(waitForServo);                                      // waits for the servo to get there
              Serial.println(val);                                       // print values for checking
         } 
         digitalWrite (recordModeLed, LOW);
}


Logged

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

@ sbright33 - I'm afraid you misunderstood me... I'm simply a director building my own camera rigs for fun, not a motion control specialist... smiley-wink
What I can say is that from what I'm reading people are coding the arduino to smooth the arrays of data - thus smoothing the motion. HOp that helps  smiley-grin
Logged

Offline Offline
Edison Member
*
Karma: 8
Posts: 1341
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My goal is to make canned predefined movements where the motion is already smoothed or eased on the PC before it is loaded unto Arduino.  There are many PC apps that do this.  I have written a Sketch to make the steppers/servos move.  It's the in between part that I'm struggling with.  How to export the data into an array for the Sketch to use.  So far Excel is my friend.  Looking for an improvement...
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

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

Aha! Now I got your meaning. you're looking for an interfacing "something" to grab you data and spit it into the arduino... interesting.
Logged

Offline Offline
Edison Member
*
Karma: 8
Posts: 1341
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Each PC app is different.  First they should export.  Then I can use Processing to translate that text file to array syntax.  Then cut and paste into IDE, compile, run.

Alternatively I could write a Sketch to take data from serial port and put it into array.  Still  need Processing to translate exported file to Serial data.  I haven't found an app that exports to serial port yet.  Then I could skip the middle man?
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Pages: 1 [2]   Go Up
Jump to: