Thread: How do I run this code only once?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    28

    How do I run this code only once?

    I'd like to run this code only once instead of a loop. Is there a simple way to do this?

    Code:
    void loop()  // Main Program 
    {
    
    //// Two Sweeps
    for (q = 0; q <=1; q += 1) 
    {
      for(pos = 180; pos >= 0; pos -= 1)  
      {                                  
        myservo.write(pos);             
        delay(slowdelay);                       
      } 
      
      for(pos = 0; pos < 180; pos+=1)  
      {                                
        myservo.write(pos);          
        delay(slowdelay);                      
      }  
    }
           pos = (90);    
        myservo.write(pos);          
        delay(23);     
    
    }
    Last edited by 777funk; 12-21-2015 at 06:30 PM.

  2. #2
    Guest
    Guest
    Remove the loop you don't want? I assume you want to run the inner loops as they appear to do useful work. So remove the outer loop. What part confuses you?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Well the two sweeps (incr and decr q number of times) is what's needed. But after that (q number of times) I'd like it to stop and not continue forever. But I need the bracket at the end and at the beginning of the main program no?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What device is this code intended for?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    It drives a stepper motor run by an arduino MCU chip.

    I'd like it to cycle from 0 to 180 a couple times then land at 90. But it does it indefinitely.

  6. #6
    Guest
    Guest
    Hmm, looking at the code as it currently is, it would appear that the outer loop runs twice, then the servo is reset to 90° and the function returns normally. What behavior are you experiencing counter to your expectation?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I don't know anything about the arduino, but it's possible that it automatically loops a program when it "ends" (a quick search seems to indicate this may be the case). The devices I work with actually stop if not explicitly looped infinitely.

    Is there a push-button switch you could utilize, so that the program will wait indefinitely until it is pressed - and when it is, it runs your code only once and then waits again?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Quote Originally Posted by Guest View Post
    Hmm, looking at the code as it currently is, it would appear that the outer loop runs twice, then the servo is reset to 90° and the function returns normally. What behavior are you experiencing counter to your expectation?
    I'd think it does just what you said (runs the outer loop twice then goes to 90° and STOPS). Instead it loops this sequence indefinitely.

    Maybe as Matticus says it is indeed an Arduino thing and not a code thing.

  9. #9
    Guest
    Guest
    Yes, I think Matticus may be onto something there (the thread is informative). I didn't even consider that possibility as I only code on/for my x86 Desktop. I hope you figure it out!

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    The Arduino system requires you to implement a setup() function and a loop() function. The setup() function is called once when the MCU resets, and the loop() function is called repeatedly until the MCU is powered off or resets (or until you stop it some other way).

    The simplest fix is to move your code into your setup() function so that it runs only once.

    By the way, you can get more help with Arduino in the Arduino forum.

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    13
    Here is a great piece of code I have used in my Morse code beeping clock, it might be my 4th or 5th generation code for sending nibbles of data to a LCD unit.

    I thought when I came up with it , it was a great piece of code, I can not remember how much small bit of memory it saved in the program


    You might be able to modify it to your own need, that is if I read your problem correctly.



    I have deleted a bit of code to make it easier to read.

    Code:
    void  sendLcd2(int highB, int lowB)
    {
     Boolean twice=false;
    
      byte nibble;
    
      do{
    
    nibble=(twice == false)?highB:lowB;
    
    twice = !twice;
    
    /*
    the first time this code runs nibble == high byte, and twice == true
    so this do while function repeats the second time nibble == low byte.
    This time twice == false so the do while function come to end.
    */
      }
    
    
      while (twice == true); 
      return;
    }
    
    of course if you do a "do - while" function that will only run once.
    Last edited by 4eric; 12-29-2015 at 04:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM