How to code Arduino blink code without setup() and loop() functions.

Spread the love

Setup and loop are two main pre-define functions of Arduino but they occupy lots of memory. A simple blink code occupies 925bytes of memory. The question is How to code Arduino without these functions and save memory. Also is it possible to do?

Coding Arduino without void setup() & void loop() is possible. In this article we are going to modify the blink example code without void loop() and void setup() function. Also optimize the code and reduce the space of Arduino blink sketch.

Material Required

Hardware

Part NameQuantity
Arduino UNO1
LED1
220ohm Resistor1
Jumper WiresAPR
Breadboard1

Software

Arduino IDELink

Connection

This is a part of Arduino code basics. The Connection is very simple for Arduino blink. Connect everything as show in digram

how to code arduino

Arduino blink tutorial Old Code

This is the Arduino blink program which toggles a led between HIGH and LOW with the delay of 1 sec. Arduino code language is C++ and there are predifine functions to make ower life easier for coding.

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

void loop() 
{
digitalWrite(LED_BUILTIN, HIGH); 
delay(1000); 
digitalWrite(LED_BUILTIN, LOW); 
delay(1000); 
}

How to write Arduino code without Setup and loop explanation.

  • Instead of using pinMode we can directly assign resistor DDRB and the hexadecimal value. As i did in this step.
void setup()
{
DDRB = 0xff;
}
  • Similarly Remove digitalWrite with PORTB and assign it hexadecimal high and low.
void loop()
{
PORTB = 0xff;
delay(1000);
PORTB = 0x0;
delay(1000);
}
  • Arduino has its main function which call the loop, setup, and other function. so here we will write main function and try to toggle between high and low. Also we put this in infinite while loop.
int main()
{
  DDRB = 0xff;
  while (true)
      {
       PORTB = 0xff;
       delay(1000);
       PORTB = 0x0;
        delay(1000);
       }
}
  • Delay is also a per define function. we are not using any libraries so we can’t use delay directly. So here we try to make ower own delay. For that I make a loop to simulate the delay. During testing, I observe that only one loop executeds and stops because the loop returns nothing. so I solve this problem bt passing an empty string.

This Arduino Code works fine it successfully toggles the pin between high and low.

int main()
{
DDRB = 0xff;
while (true)
{
PORTB = 0xff;
for (long i = 0; i<1000000; i++)
{
  asn("");
}
PORTB = 0x0;
for (long i = 0; i<1000000; i++)
{
asn("");
}
}
}

Final Code

There is a PIN method which allow toggle the pin state depends on previews state so i use that method which make my code more short and less in size.

int main() 
{
  DDRB = 0xff;
  while (true) 
{
    PINB = 0xff;
    for (long i = 0; i < 500000; i++) 
{
      asm("");
    }
  }
}

Everything about ports

PORTD maps to Arduino digital pins 0 to 7

  • DDRD – The Port D Data Direction Register – read/write
  • PORTD – The Port D Data Register – read/write
  • PIND – The Port D Input Pins Register – read-only

PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable

  • DDRB – The Port B Data Direction Register – read/write
  • PORTB – The Port B Data Register – read/write
  • PINB – The Port B Input Pins Register – read-only

PORTC maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on the Arduino Mini

  • DDRC – The Port C Data Direction Register – read/write
  • PORTC – The Port C Data Register – read/write
  • PINC – The Port C Input Pins Register – read-only

Each bit in these registers corresponds to a single pin; Such as DDRB, PORTB, and PINB refers to the low bit pin PB0 (digital pin 8). For a complete mapping of the Arduino pin number to ports and bits, see the diagram for your chip: ATmega8, ATmega168. (Note that some bits of a port can be used for things other than I / O; be careful not to change the values ​​of register bits corresponding to them.)

Add a Comment

Your email address will not be published. Required fields are marked *