Arduino (C++)

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

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

Variables

const int sensorPin = A0;
const float baselineTemp = 19;

float voltage = (sensorVal / 1024.0) * 5.0;
float temperature = (voltage - 0.5) * 100;

Delays

delay(1000); // in milliseconds

Digital I/O Pins

The digital pins on an Arduino can act as both inputs and outputs.

The Arduino’s digital pins can only read two states: when there is voltage on an input pin, and when there’s not. This kind of input is normally called digital (or binary, for two states). These states are commonly referred to as HIGH and LOW.

  • HIGH means there’s voltage here. When you turn an OUTPUT pin HIGH, you’re turning it on.
  • LOW means there’s no voltage here. When you turn an OUTPUT pin LOW, you’re turning it off.
// Goes in setup()

pinMode(LED_BUILTIN, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH); // 5V
digitalWrite(2, LOW); // 0V
digitalRead(2); // Needs an argument for which pin to read from

Analog I/O Pins

Analog pins can report a value between 0-1023, which maps to a range from 0 to 5 volts.

Serial & Logging

Make sure you open the serial monitor to see the output: Tools > Serial Monitor

void setup() {
  Serial.begin(9600); // open a serial port, 9600 bits/s
}
void loop() {
  int sensorVal = analogRead(sensorPin);

  Serial.print("Sensor Value: ");
  Serial.print(sensorVal);

  Serial.print("index=");
  Serial.print(index);

  Serial.println(); // moves cursor to a new line
}

Conditionals

if (true) {
  // if true code here
}
if (true) {
  // if true code here
} else {
  // if false code here
}
if (true) {
  // if true code here
} else if (true) {
  // if false code here
} else {
  // if false code here
}

Loops

for(int pinNumber = 2; pinNumber<5; pinNumber++){
  // Loop code goes here
}