output:

Code:

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

void loop() {
  digitalWrite(LEDPIN,HIGH);
  delay(500);
  digitalWrite(LEDPIN,LOW);
  delay(500);
}


print to serial monitor

Code:

void setup()
{
  Serial.begin(9600);
  // print to serial monitor
  Serial.println("hello world");
}


input:

Code:

int buttonState = 0;
void setup()
{
  pinMode(2, INPUT);
  Serial.begin(9600);
}

void loop()
{
  // read the input pin
  buttonState = digitalRead(2);
  // print out the state of the button
  Serial.println(buttonState);
  delay(10); // Delay a little bit to improve simulation performance
}

analog output:

Code:

# define led1 11
void setup()
{
  pinMode(led1, OUTPUT);
  // can be used to set LED brightness (but only PWM functionality pins
  // such as 9, 10, 11):
  analogWrite(led1,128);// 0..255 range (low..high)
}

read serial from PC on Arduino
(input on serial monitor window on PC)

Code:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    int msg = Serial.parseInt();
    Serial.println(msg);
  }
}


EEPROM save, load data:
the data is saved even after Arduino shut down
EEPROM data can save values of 0->255 (1 byte)
EEPROM has a lifetime of bout 100k times so try not to use it in the loop function

Code:

#include <EEPROM.h>
void setup() {
  Serial.begin(9600);
  // save 7 at address 0
  // available addresses: 0->512
  EEPROM.write(0,7);
  EEPROM.write(3,50);
  int v1 = EEPROM.read(3);
  Serial.println(v1);
}

analog pins

analog pins can also be used as digital read pins
pinMode(A0, INPUT);
digitalWrite(a0,High);

get analog pin analog input:
pinMode(A0, INPUT);
sensorValue = analogRead(A0); // reads between 0 -> 1023
// a potentiometer connected to 5v, GRD and a0
// a0 = (analog pin 0 to middle connector of potentiometer)

get time after boot:
unsigned long t = millis();
unsigned long t2 = micros(); // 1k more precise than millis
delayMicroseconds(1000000); // delays program for 1 second

//this can be used to replace the use of the delay(); function.

Code:

unsigned long prevTime = millis();
# define timeInterval  1000
bool ledState1 = false;

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

void loop()
{
  unsigned long currentTime = millis();
  if(currentTime - prevTime > timeInterval){
    prevTime = currentTime;
    if(ledState1){
       digitalWrite(LED_BUILTIN, HIGH);
    }
    else{
       digitalWrite(LED_BUILTIN, LOW);
    }
    ledState1 = not ledState1;
  }
}
blush