Pages: [1]   Go Down
Author Topic: newb help request.  How best to construct this?  (Read 339 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I'm currently working on my first Arduino project, an adaptation of The Magic Clock project (I can't yep post a link, but you can google themagicclock).  My version is going to use stepper motors instead of servos, which makes for a situation that I'm having trouble figuring out how to program for.  I am very new to programming, but determined to figure this out.  I have a fairly technical mind, and have so far enjoyed figuring most of the code I'm adapting.  I've learned quite a lot in the past week, and I have deciphered a lot of what I need to. I've even constructed a few smaller test setups to get the hardware moving and figure out some of the programming basics.  However, as a newb there are still some things I'm not sure how to handle.  

In a nutshell, I'd like a condition (X != Y) to cause a stepper to rotate until a sensor has a LOW>HIGH transition, and from this point continue to rotate X amount of steps.  The sensor essentially makes for a zero position mark from which X amount of steps can be taken to arrive at a desired place.  

Here is a mockup of what I'd like to occur. Hopefully I do not embarass myself too much with this pseudocde!

Code:
int Position
// a position determined elseware in the program

int OldPosition
// after the stepper has landed at a desired location...
// the location is saved here to compare new settings to

void SetHands ()
// SetHands is a function called...
// from another place in the program
{                                                                                    
      IF (Position != OldPosition)
      // If a new position has been set which...
      // does not match the previous one
      {
            IF (digitalRead (SensePin[var])==HIGH)
            // switch is HIGH, stepper is at zero position!
            {
                  //move stepper X steps
            }
            ELSE
            {
                  //keep moving stepper until SensePin[var]==HIGH
                  //Then move stepper X steps
            }
      }
}
I'm not looking for help (yet!) on the actual stepper commands, but rather how see if a condition is met, if not then make one thing occur until condition IS met, and then immediately perform another action.

Please keep in mind that I am VERY new to this, so please be gentle.  I could not find an example that would accomplish this.  I having a feeling that using a While, or a Do..While, or a Switch Case, would be a smarter way of accomplishing this, but I haven't been able to create something that didn't lead me to a dead end or a loop I wasn't sure how to get out of.  Thanks in advance for any help or direction, or even some reading suggestions!

-Ryan
Logged

NJ
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
ARDUINO BEEP BEEP
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How powerful is the servo, I assume not very?

Try the motor shield, it makes simple work of controlling a servo. It might cost a couple bucks but it's nothing too significant.
Logged

NJ
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
ARDUINO BEEP BEEP
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

how about this for the loop?

you have an external trigger to set X!=Y (like a momentary spst or something)

Quote
if (X!=Y){
  if (digitalRead(SensePin)==HIGH){
    motor.step(5, FORWARD, SINGLE);
  }else if(digitalRead(SensePin)==LOW){
    X=Y;
  }
}


Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bleedscarlet, thanks a lot for taking a look.  I really appreciate you taking the time to think about this a little but. I think maybe my original post was too lengthy, which lead to a bit of skimming-over, so I'll try to clarify a couple of things.

Quote
How powerful is the servo, I assume not very?

Try the motor shield, it makes simple work of controlling a servo. It might cost a couple bucks but it's nothing too significant.

I'll be using steppers not servos (which I think you realize, maybe you mistyped smiley ), and I'll be using 3 of them, which is one more than the motorshield can accommodate.  I will be using EasyDriver boards to drive the steppers.  From the bit I've played aruond with them already, they seem easy to control, so I'm not too worried about the coding for that part of the project.  

I believe you are on to something with the bit of code you've suggested.  I'm hoping you can clarify something for me though.  Do you think the first IF statement should be made into a While statement in order to make the stepper continue to move forward until the requirement is met?  Looking at your code, my uneducated and untrained eye (read: I could be very wrong about this) makes me think that the code as you've written it will only make the stepper act once, rather than continuing to step until SensePin==LOW.  Here is what I'm thinking of doing...

Code:
int Position;
// a position determined elseware in the program

int OldPosition;
// after the stepper has landed at a desired location...
// the location is saved here to compare new settings to

int x;
int y;

void SetHands()
// SetHands is a function called...
// from another place in the program
{
  if (Position != OldPosition)
  {
    if (digitalRead (SensePin)==HIGH)
    //stepper is already at zero position
    {
      MoveHand();
      //MoveHand function makes stepper move X steps
    }
    else
    {
      Calibrate();
      //causes stepper to rotate until zero position is reached
      MoveHand();
    }
  }
}

void Calibrate()
{
  x = 1;
  y = 0;
  
  while (X!=Y)
  {
    if (digitalRead(SensePin)==LOW                
    //SensePin LOW = sensor not tripped
    {
      move forward 1 step;
    }
    else if(digitalRead(SensePin)==HIGH)
    {
      X=Y;
    }
  }
}

void MoveHands()
{
  //move stepper x steps
  Position = OldPosition;
}

I'm not even sure I can put two functions in that else statement (calibrate() movehands()), I may have to move that movehands() to the 'else if'  statement of the calibrate() function, before the x=y.  Anyway, please let me know what you think.  And if anyone else is checking this out, please feel free to jump in.  The more the merrier, and hopefully I'll learn something smiley
Logged

NYC
Offline Offline
Full Member
***
Karma: 0
Posts: 129
The singularity is near!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dude, Just try to wirite it. First look at a real sketch like the blink example: http://www.arduino.cc/en/Tutorial/Blink

Make sure you can run the sketch. Trust me, it will be fun. Play with the sketch and run again (like change how fast it blnks.)

Add some print statements like when you turn the led on or off, Serial.println("led on"); The print statements allow you to see easilly what the program is doing. Look up: http://arduino.cc/en/Serial/Print

Now that you have learned a little Arduino programming, Add a few lines at a time to the blink example (and save under a new name) to get it to do what you want.
Logged

Pages: [1]   Go Up
Jump to: