Newbie here, Can you program multiple ways to accomplish the same task ?

Can you program multiple ways to accomplish the same task such as make making a servo go from 0 to 180 and then from 180 to zero? Any example of using different ways to accomplish this task would be great.

Thanks

Of course there are several ways to program a computer to do a particular thing. What kind of daft question is that?

What you're asking us to show you is literally one of the examples that comes with the IDE. Why don't you at least put in a little bit of effort before you come ask us to do it all for you.

Or is this a homework question you were hoping we'd answer for you?

blink

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

blink without delay

const int ledPin =  13;
int ledState = LOW;
unsigned long previousMillis = 0;
unsigned long interval = 1000;

void setup() {
  pinMode(ledPin, OUTPUT);
} 

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ledPin, ledState);
  }
}

yet another way

// Example 43.1
// tronixstuff.com/tutorials > chapter 43
// John Boxall - October 2011
// Digital 0~7 set to outputs, then on/off using port manipulation

void setup()
{
  DDRD = B11111111; // set PORTD (digital 7~0) to outputs
}

void loop()
{
  PORTD = B11110000; // digital 4~7 HIGH, digital 3~0 LOW
  delay(1000);
  PORTD = B00001111; // digital 4~7 LOW, digital 3~0 HIGH
  delay(1000);
}

Delta_G: Or is this a homework question you were hoping we'd answer for you?

Ding, Ding. Yup.

epsilon: Can you program multiple ways to accomplish the same task such as make making a servo go from 0 to 180 and then from 180 to zero?

Yes I can, thanks for asking. Now over to you to write your own example.

Steve

Look at that , a bunch of people increasing their comment rankings....

Unfortunately i tried to search school that teach Arduino, there are none, all i'm relying on are bits and pieces on YouTube, independent sites and so on, so since Arduino learning has been bits and pieces i ask questions like this and will to make sure my sources on YouTube are not taking me in the wrong direction, its not like i have a teacher teaching me this, or even a paid source.

Don't take the fun out of learning Arduino with your non helping comments please.

epsilon: Look at that , a bunch of people increasing their comment rankings....

Don't take the fun out of learning Arduino with your non helping comments please.

karma++

epsilon: Don't take the fun out of learning Arduino with your non helping comments please.

Try explaining why you want answers like that then because I'm not clear. The answer to your question is yes. There are always many ways of doing something. Some will be ugly, some long-winded, some fast, some slow, some easy to extend, some almost impossible to understand etc.

So if you already know a working way of doing some task (like the Sweep example) what is it that you want to know about other ways of doing the same thing? If you have some specific reason for doing things differently why not say what it is ?

Learning to program usually involves having a number of tasks and wanting to find ways to do them. Trying to find many ways to do the same thing for no particular reason sounds like a test set by a bad teacher to fill the time up.

So is there actually anything specific you want to know? About Sweep in particular? Or some other examples?

Steve