Basic structure, Timing and Monitor data in desktop

Basic structure

All Programming constructs are same as C++.

Mandatory functions in arduino framework

void setup() {
          // put your setup code here, to run once:

        }

        void loop() {
          // put your main code here, to run repeatedly:

        }

Here is a simple hello world program :

1) Make the built-in LED (connected to pin 13) bilnk with delay of 1s.

2) Print the status on dektop using Serial.

a) Set the data transfer speed with Serial.begin(9600).

b) Use Serial.println() for outputs

3) Connect the arduino, select the right board and port, and upload code

4) To monitor the result on desktop, click Tools -> Serial monitor or click the magnifying glass button on the top right. Set the same data transfer speed (9600) on the Serial monitor. (Otherwise, the message may be missed)

Timing functions:

1) millis() : Returns the miliseconds after the start of arduino. This counter is reset when the reset button on arduino is clicked

2) delay(ms) : Number of milliseconds to delay

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  // Built In LED is connected to PIN 13
  pinMode(LED_BUILTIN, OUTPUT);
  // pinMode(13, OUTPUT); // Equivalent

  Serial.begin(9600); //Set data speed to computer at 9600 bits per second.
  // 9600 is an optimal value. Make sure that this data speed matches with data speed in monitor window
  Serial.println("Hello"); // This data will be sent to desktop and output can be viewed via serial monitor
}

// the loop function runs over and over again forever
void loop() {
  Serial.println(millis())           // prints the millisecond since the arduino was started / restarted
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  Serial.println("ON");              // print ON in the desktops serial monitor
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  Serial.println("OFF");             // print OFF in desktops serial monitor
}

Note:

Serial plotter : If you are streaming numbers to desktop, you can use serial plotter to monitor the data in the form of graph