Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The Arduino Software (IDE) allows you to write programs and upload them to your
board.
*Code in the Arduino IDE is written manily with C++ along with custom libraries for the
various functions for the Arduino.
//blink sketch – turns an led on and off Variable name “led” is initialized to the
value 13, everytime “led” is called in the sketch it
int led{13}; //sets the variable name led to 13 is read as the value it was initialized to; in this case
13.
Explanation:
// Reading Sensor Values Pin A0 is initialized as an input pin. Note:
any pin can be used for analog input not just the
int input_pin{A0};
analog pins, digital pins e.g. pin 1, pin 2, pin 3, pin
void setup() { 4, … can be used as analog pins as well and vice
versa.
pinMode(input_pin, INPUT);
When reading sensor values two functions can be
} used analogRead(pinNumber) and
digitalRead(pinNumber). With analogRead() the
pin number to be read from is declared and a value
void loop() { from 0 -1023 will be returned
analogRead(input_pin); When reading sensor values using digitalRead()
the pin number to be read from is declared and the
digitalRead(input_pin);
pin will be read if it is in HIGH or LOW state
}
// UltraSonic Sensor – simplified | cm reading Explanation:
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
int distance{get_object_distance()};
Serial.println(distance);
delay(250);
}
void get_object_distance() {
int duration{};
double distance{};
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
return distance;
}