PinMode (a simple question)

Hello guys
I am trying to use a led without using setup function. But i don't know how to define pinMode without using setup function. I will be very happy if you can help me :slight_smile:
Here is the code

void setup() {
pinMode(3, OUTPUT);
}
void loop() {
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3, LOW);
delay(1000);
}

Why don't you want to use a setup function?

An even simpler question: What has this got to do with the website or forum?

pert:
Why don't you want to use a setup function?

Unfortunately it's mine exam question. My teacher can be very pointless sometimes :slight_smile:

Google "AVR data direction register"

Seymanurozad:
Unfortunately it's mine exam question. My teacher can be very pointless sometimes :slight_smile:

Are you going to tell your teacher that we passed your exam?

...R

Seymanurozad:
My teacher can be very pointless sometimes :slight_smile:

Indeed.

Seymanurozad:
I am trying to use a led without using setup function.

pinMode can be called at any time. (But it is difficult to tell if that's what the teacher has in mind.)

What’s the exam question ?
You could run an led from an analog port without setup

well, setup function is a specific section of the code

void setup() {
}

so you could just do something like this:

bool runSetup = true;

void setup(){} //this has to be here, or IDE will give you a compiler error

void loop(){
  if (runSetup){
    //put setup code here
    runSetup = false;
  }
  //put rest of your code here
}

but I doubt that is what your teacher really intends.

If you actually do want to compile a sketch with no setup() function references, you will have to get a lot deeper into the IDE than I can to research.

But to be a bit less sarcastic, you can have a look at the page on direct port manipulation:
Port Manipulation

i don't know how to define pinMode without using setup function

Without a setup() function you are going to struggle as a normal Arduino sketch simply will not compile. There are ways to do it but you will find it complicated.

Are you sure that it is the setup() function that you are not allowed to use or just pinMode() ?

You are allowed to use your own main().
You are allowed to use your own direct access to the DDR registers.

Now, the tricky bit is the delay().

Maybe it is time to look at what the Arduino's init function does.

Does this sketch make sense?

void loop()
{
  pinMode(13, OUTPUT);
  do
  {
    digitalWrite(13, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    delay(1000);
  }
  while (1);
}

void setup()
{
  
}

GolamMostafa:
Does this sketch make sense?

Yes, but it has a setup function

An Arduino sketch can be created and executed without an explicit loop() function. Can it be done without a setup() function? (Arduino users, usually, do not like to see main() function in an Arduino