Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Help to finish off program

  1. Help to finish off program

    i am in process of completing my robotic eyes program and ran into a lil issue. I have one of the mini push buttons and am trying to use as both an on and off switch to power an led and servo. basically what i want is when u press it once they turn on, press it again they turn off. right now i have it to where both the led and servo power on when holding down the button.. can someone please help me make the change to the code to correct this issue im having, here is my code.. thanks

    #include <Servo.h> //include the servo libary - a bunch of code for a host of servo instructions y9ou can use and don't have to develop yourself
    Servo myservo; // create servo object to control a servo
    const int buttonPin = A0; // the number of the pushbutton pin
    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
    int buttonState = 0; // variable for reading the pushbutton status
    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
    // initialize the pushbutton pin as an input:
    pinMode(buttonPin, INPUT);
    }
    void loop()
    {

    // read the state of the pushbutton value:
    buttonState = digitalRead(buttonPin);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState == HIGH) {

    pos=random(65,115); // ramdomly set the servo arm postion between 65 & 115 degrees. "90" is "straight ahead"
    delay1=random(50, 500); // randomly set delay between servo movement between 50 and 500 milliseconds
    myservo.write(pos); // tell servo to go to position specified by variable ‘pos’ value
    delay(delay1); // waits randomly between 50ms and 500 ms between servo movements
    randOn = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
    randOff = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
    digitalWrite(ledPin, HIGH); // sets the LED on
    delay(randOn); // waits for a random time while ON
    digitalWrite(ledPin, LOW); // sets the LED off
    delay(randOff); // waits for a random time while OFF
    }

    else {

    }
    }

  2. #2
    Join Date
    Jun 2010
    Location
    Australia
    Posts
    161
    Rep Power
    33

    Re: Help to finish off program

    Without looking at the program (I am not a good programmer), but just looking at how you describe the actual switch, it could be that you have a "Momentary on" switch instead of one that 'clicks on' with one press then 'clicks off' with a second press.
    The program will not be able to sense high or low unless there is something to detect, so check with a multi-meter that you have continuity between your two button presses.
    I.E. Before the first button press = no continuity, then after the first press you should have continuity, then the second press breaks the circuit again.

  3. Re: Help to finish off program

    Hi,

    Just so you know, there is a CODE beacon you can use to present your code in the original formating, makes it easier to read.

    Concerning your question, if I understand it correctly the first thing you will need to have is another variable that hold the current state information (is it ON of OFF right now?), and each time a push on the button is detected, it toggles to the opposite state.
    It won't be enough to solve the full problem, but it's a first step.

    If you have indeed a momentary switch, as the button is checked only once per loop right now, pressing it while the servo is moving or while the led is blinking will do nothing... to go further, I think you will need to describe more precisely the behavior you want to achieve and the type of switch you have.
    ---
    Personal blog: http://xevel.org
    USB2AX documentation: http://xevelabs.com

  4. Re: Help to finish off program

    hope this makes reading the code easier. basically i made a set of robotic eyes with leds that blink randomly and a servo which controls the motion from left to right at random speeds and times. i am installing this in my vehicle for a car show because i am using the transformer theme. basically i want to press the mini button to turn the devices on and off when i am showing the vehicle. this is the mini button i currently am using, http://www.trossenrobotics.com/store...sh-Button.aspx


    Code:
    #include <Servo.h> //include the servo libary - a bunch of code for a host of servo instructions y9ou can use and don't have to develop yourself
    Servo myservo; // create servo object to control a servo
    const int buttonPin = A0; // the number of the pushbutton pin
    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
    int buttonState = 0; // variable for reading the pushbutton status
    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
    // initialize the pushbutton pin as an input:
    pinMode(buttonPin, INPUT);
    }
    void loop()
    {
    
    // read the state of the pushbutton value:
    buttonState = digitalRead(buttonPin);
    
    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState == HIGH) { 
    
    pos=random(65,115); // ramdomly set the servo arm postion between 65 & 115 degrees. "90" is "straight ahead"
    delay1=random(50, 500); // randomly set delay between servo movement between 50 and 500 milliseconds
    myservo.write(pos); // tell servo to go to position specified by variable ‘pos’ value
    delay(delay1); // waits randomly between 50ms and 500 ms between servo movements
    randOn = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
    randOff = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
    digitalWrite(ledPin, HIGH); // sets the LED on
    delay(randOn); // waits for a random time while ON
    digitalWrite(ledPin, LOW); // sets the LED off
    delay(randOff); // waits for a random time while OFF
    } 
    
    else {
    
    }
    }

  5. Re: Help to finish off program

    i have gotten this code to work with the push button, however it will only shut off now when i press the button in between blinks right as the leds turn off, is there a modification in the code i can change this so that i just press it and it shuts off

    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;
    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 loop(){
      
      currentState = digitalRead(buttonPin);
      
      //When lastState is LOW and currentState is HIGH, we have detected a rising edge transition
      //ie the input has just gone high.  This is when we toggle are active boolean
      if(lastState == LOW && currentState == HIGH){
        //toggle active state
        active = !active;
      }
      
      lastState = currentState;
      
      if(active){
        //Perform actions when active = true;
    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
    delay(delay1); // waits randomly between 200ms and 550 ms between servo movements
    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
    delay(randOn); // waits for a random time while ON
    digitalWrite(ledPin, LOW); // sets the LED off
    delay(randOff); // waits for a random time while OFF
    
        
      }
      else{
        //Perform actions when active = false;
        
      }
      
      
      
    }

  6. Re: Help to finish off program

    Ok, so now you have a bit of a concurrency problem: you want the controller to do stuff *and* check for the button press at the same time.

    There are different ways to solve this. You can have a look at external interrupts (for doing stuff when the button is pressed), or timer interrupts (for doing stuff at regular intervals)... or maybe the simplest way to kind of fake the behavior you want would be to create a modified delay function.

    This function would basically start by having a look at the timestamp (using millis() ), then loop until enough milliseconds have passed while checking in this loop if the button is pressed, and updating the "active" variable accordingly.

    Not the prettyest way to do it, but considering the controller currently spends most of its time in delay(), it should do the trick.
    ---
    Personal blog: http://xevel.org
    USB2AX documentation: http://xevelabs.com

  7. Re: Help to finish off program

    can you help me with this function code, im not a coder and this is my first time doing this kinda stuff so i have no clue how to do it, im just trying to finish this thing for an upcoming car show lol.

    id highly be greatful if you could help me with this

  8. Re: Help to finish off program

    Try something like this:

    Code:
    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...
      }
    }
    and replace all the calls to delay(...) by calls to my_delay(...).
    Also, remove the lines checking for the button state in the loop function (everything before "if(active)").

    (not tested, even for compilation, so watch out for typos!)
    ---
    Personal blog: http://xevel.org
    USB2AX documentation: http://xevelabs.com

  9. Re: Help to finish off program

    like this ? it errored out during compiling saying undefined reference to loop


    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;
    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...
      }
      if(active){
        //Perform actions when active = true;
    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
    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
     
     
      }
      else{
        //Perform actions when active = false;
     
      }
     
     
     
    }
    Last edited by nolimit44; 02-06-2011 at 01:15 PM.

  10. Re: Help to finish off program

    nope, *in the loop function*, remove stuff before if(active), does not mean removing the loop function itself.

    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;
    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(){
      if(active){
        //Perform actions when active = true;
        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
        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
      }
      else{
        //Perform actions when active = false;
      }
    }
    Hope this helps.
    ---
    Personal blog: http://xevel.org
    USB2AX documentation: http://xevelabs.com

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, 01: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, 07:18 PM
  3. C# Bioloid Motion Sequence Program
    By DresnerRobotics in forum Mech Warfare
    Replies: 4
    Last Post: 11-25-2009, 05: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, 12:39 PM
  5. Fingers Program using C #‏ Microsoft robotics studio
    By jems in forum Software and Programming
    Replies: 17
    Last Post: 09-05-2008, 01: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
  •