Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Help to finish off program

  1. Re: Help to finish off program

    ok i compiled the code and uploaded it , however the unit wont even power on at the push of the button anymore

  2. Re: Help to finish off program

    Oops, my bad.
    Now the code only checks the state of the button when waiting using my_delay(), and it only calls my_delay() when active is true...

    Add a my_delay(10) in the "else" branch of the "if(active)" conditional...
    ---
    Personal blog: http://xevel.org
    USB2AX documentation: http://xevelabs.com

  3. Re: Help to finish off program

    thank you very much for taking the time to help me, everything worked great. if i have any other questions ill post again

  4. Re: Help to finish off program

    ok after testing the robot eyes for a few days, i noticed that the led will not blink or be on on during servo movement.

    basically the led stays on, blinks, the servo moves to new location, and then led comes back on after the servo shift.

    how can i make the led seperate from the servo movement so that it doesnt matter if the servo is moving or not to be on or blink?

  5. Re: Help to finish off program

    What you are asking now is a little more complex, and you will need some sort of (faked) concurrency.
    Again, their is multiple ways to get to the behavior you want, some simpler than others.

    If you want to stay with only arduino functions (this implies that it will work no matter what on any arduino board), you will have to do something similar to what you already have, but with more levels.

    An other way would be to use interrupts, or to create a sort of scheduler...

    I think you should check this library http://www.arduino.cc/playground/Code/Metro
    With it, you can make a separate "Metro" for each event (led, moving servo) and eliminate all the delays in your code.

    Basically the idea would be something along these lines:

    Code:
    Metro ledMetro = Metro(10);
    Metro servoMetro = Metro(10);
    
    char ledState = LOW;
    bool servoDeployed  = false;  
    
    void loop(){
       //check_button and use it to set 'active'
       ...
       
       if(ledMetro.check() ==1){
          //change the led state and set the metro interval with a random number depending on the led state
          
          if(ledState==HIGH){
            ... change led and ledState to LOW, change metro interval to a random number 
          }else {
            ... if not active, change led state and update metro interval
          }
       }
    
       if(servoMetro.check() == 1){
          // move the servo and set set the metro interval...
          if(servoDeployed){
            ... return to home position
          } else {
            ... if not active, then deploy and change metro interval
          }
       }
    }
    The "active" flag is used only in the inner conditions so that you have a code that does not allow the animation to begin when "active" is false, yet allows it to terminate once it has started.

    You should not have any delay() left

    Hope this helps.
    ---
    Personal blog: http://xevel.org
    USB2AX documentation: http://xevelabs.com

  6. Re: Help to finish off program

    ok so am i adding this to what i already have and just getting rid of the delays or am i removing everything i have in the loop function. i ask this because i have all the motion range of servos and led blink timeframes and not sure how to combine the new code with the old

  7. Re: Help to finish off program

    I would start from scratch, in a new sketch. You can copy all the stuff that you are sure you still need (the top, up to setup()), but check for stuff not needed anymore.

    Keep the first version opened beside this one so that you have all the data available.

    Servo range of motion will still be used the exact same way it was, all significant delays will translate to intervals for the Metros.
    ---
    Personal blog: http://xevel.org
    USB2AX documentation: http://xevelabs.com

  8. Re: Help to finish off program

    anything like this ? lol i tried but am prob not even close

    Code:
    #include <Servo.h> //include the servo libary - a bunch of code for a host of servo instructions you can use and don't have to develop yourself
    Servo myservo; // create servo object to control a servo
    boolean active = false;
    boolean lastState = LOW;
    boolean currentState = LOW;
    byte buttonPin = A0;
    Metro ledMetro = Metro(10);
    Metro servoMetro = Metro(10);
    char ledState = LOW;
    bool servoDeployed  = false; 
    int pos = 0; // variable to store the servo position
    int delay1 = 0; // variable to store the delay between servo arm's movement info
    int ledPin = 13; // LED connected to digital pin 13
    long randOn = 0; // Initialize a variable for the ON time
    long randOff = 0; // Initialize a variable for the OFF time
    void setup(){
     
    myservo.attach(9); // attaches pin 9 of the micro controller to the servo object - the servo's "control wire"
    randomSeed (analogRead (0)); // randomize
    pinMode(ledPin, OUTPUT); // sets the digital pin as output
     
    }
    void my_delay(int delay_ms){
      unsigned long end_time = millis() + delay_ms; // time at which to stop = now + delay
      while(millis() < end_time){ // wait until enough time has past
        currentState = digitalRead(buttonPin); // check the button
     
        if(lastState == LOW && currentState == HIGH){ // check for rising edge
          active = !active;  //toggle active state
        }
        lastState = currentState; // remember last state
        delayMicroseconds(200); // wait a little, not really usefull...
      }
    }
    void loop(){
       //check_button and use it to set 'active'
       ...
     
       if(ledMetro.check() ==1){
          //change the led state and set the metro interval with a random number depending on the led state
     
          if(ledState==HIGH){
     
       randOn = random (100, 10000); // generate ON time between .1 and 10 seconds
        randOff = random (200, 400); // generate OFF time between 0.2 and 0.4 seconds
        digitalWrite(ledPin, HIGH); // sets the LED on
        my_delay(randOn); // waits for a random time while ON
        digitalWrite(ledPin, LOW); // sets the LED off
        my_delay(randOff); // waits for a random time while OFF
            //... change led and ledState to LOW, change metro interval to a random number 
          }else {
     
            Metro(10);
            //...if not active, change led state and update metro interval
          }
       }
       if(servoMetro.check() == 1){
     
         pos=random(30,160); // ramdomly set the servo arm postion between 30 & 165 degrees. "90" is "straight ahead"
        delay1=random(200, 550); // randomly set delay between servo movement between 200 and 550 milliseconds
        myservo.write(pos); // tell servo to go to position specified by variable ‘pos’ value
        my_delay(delay1); // waits randomly between 200ms and 550 ms between servo movements
          // move the servo and set set the metro interval...
     
          if(servoDeployed){
            ... return to home position
          } else {
                     Metro(10);
            ... if not active, then deploy and change metro interval
          }
       }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Who uses what cad program?
    By rebel in forum Software and Programming
    Replies: 20
    Last Post: 02-14-2011, 12:13 AM
  2. Question(s) How to program a bioloid with C
    By Hamed in forum Software and Programming
    Replies: 4
    Last Post: 12-02-2010, 06:18 PM
  3. C# Bioloid Motion Sequence Program
    By DresnerRobotics in forum Mech Warfare
    Replies: 4
    Last Post: 11-25-2009, 04:55 AM
  4. Cnc cad program and hardware opinions requested
    By science_geek in forum Software and Programming
    Replies: 4
    Last Post: 06-14-2009, 11:39 AM
  5. Fingers Program using C #‏ Microsoft robotics studio
    By jems in forum Software and Programming
    Replies: 17
    Last Post: 09-05-2008, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •