Tutorial Arduino #22

Lettore SD / microSD

Lettore schede SD / microSD - Tutorial Arduino #1
Tutorial Arduino #21: Striscia LED RGB
Tutorial Arduino #21
Striscia LED RGB
Tutorial Arduino #23: Real Time Clock RTC DS3231
Tutorial Arduino #23
Real Time Clock RTC DS3231

Descrizione

In questo video scoprirai come usare le schede SD / microSD con Arduino. Vedremo come creare il circuito e programmare lo sketch per leggere e scrivere le informazioni sulle schede SD.

In particolare ti spiego come salvare i movimenti di un servomotore per poi riprodurli in automatico.

Scarica le risorse

Progetto: MicroSD (lettura)

Schema

Componenti

Codice

#include <SD.h>#include <Servo.h> #define SD_PIN 10#define SWITCH_PIN 8#define BUTTON_PIN 7#define POTENTIOMETER_PIN A0#define RED_LED_PIN 5#define GREEN_LED_PIN 4#define SERVO_PIN 2 #define FILE_NAME "servo.txt" File file;Servo servo; bool enable = false; void setup() {  if (!SD.begin(SD_PIN)) {    while (true);  }  if (SD.exists(FILE_NAME)) {    SD.remove(FILE_NAME);  }  servo.attach(SERVO_PIN);  pinMode(SWITCH_PIN, INPUT);  pinMode(BUTTON_PIN, INPUT);  pinMode(RED_LED_PIN, OUTPUT);  pinMode(GREEN_LED_PIN, OUTPUT);} void loop() {  if (digitalRead(SWITCH_PIN) == HIGH && !enable) {    file = SD.open(FILE_NAME, FILE_WRITE);    digitalWrite(RED_LED_PIN, HIGH);    enable = true;    delay(300);  } else if (digitalRead(SWITCH_PIN) == LOW && enable) {    file.close();    digitalWrite(RED_LED_PIN, LOW);    enable = false;    delay(300);  }   int value = analogRead(POTENTIOMETER_PIN);  int position = map(value, 010230179);  servo.write(position);    if (!enable) {    return;  }   if (digitalRead(BUTTON_PIN) == HIGH) {    if (file) {      digitalWrite(GREEN_LED_PIN, HIGH);      file.println(position);      delay(300);      digitalWrite(GREEN_LED_PIN, LOW);    }  }  delay(10);}
scarica il file

Progetto: MicroSD (scrittura)

Schema

Componenti

Codice

#include <SD.h>#include <Servo.h> #define SD_PIN 10#define SWITCH_PIN 8#define BUTTON_PIN 7#define POTENTIOMETER_PIN A0#define RED_LED_PIN 5#define GREEN_LED_PIN 4#define SERVO_PIN 2 #define FILE_NAME "servo.txt" #define SPEED_MIN 1#define SPEED_MAX 5 File file;Servo servo; bool enable = false;int currentPosition = 90;int nextPosition = 90;int fileCursor = 0; void setup() {  if (!SD.begin(SD_PIN)) {    while (true);  }  servo.attach(SERVO_PIN);  servo.write(currentPosition);  pinMode(SWITCH_PIN, INPUT);  pinMode(BUTTON_PIN, INPUT);  pinMode(RED_LED_PIN, OUTPUT);  pinMode(GREEN_LED_PIN, OUTPUT);} void loop() {  if (digitalRead(SWITCH_PIN) == HIGH && !enable) {    file = SD.open(FILE_NAME, FILE_READ);    file.seek(fileCursor);    digitalWrite(GREEN_LED_PIN, HIGH);    enable = true;    delay(300);  } else if (digitalRead(SWITCH_PIN) == LOW && enable) {    file.close();    digitalWrite(GREEN_LED_PIN, LOW);    enable = false;    delay(300);  }    if (!enable) {    return;  }   if (digitalRead(BUTTON_PIN) == HIGH) {    digitalWrite(RED_LED_PIN, HIGH);    nextPosition = currentPosition;    delay(300);    digitalWrite(RED_LED_PIN, LOW);  }    if (nextPosition != currentPosition) {    int value = analogRead(POTENTIOMETER_PIN);    int speed = map(value, 01023, SPEED_MIN, SPEED_MAX);        if (nextPosition > currentPosition) {      currentPosition += min(speed, nextPosition - currentPosition);    } else if (nextPosition < currentPosition) {      currentPosition -= min(speed, currentPosition - nextPosition);    }    servo.write(currentPosition);  } else {    int n = readLine();    if (n >= 0 && n <= 179) {      nextPosition = n;    }  }   delay(10);} int readLine() {    if (!file) {      return -1;    }    if (file.available()) {      String msg = file.readStringUntil('\n');      fileCursor = file.position();      return msg.toInt();    } else {      file.seek(0);      fileCursor = 0;      return -1;    }}
scarica il file

Arduino Software Hero

Vuoi imparare a programmare Arduino, ma non sai da dove cominciare?

Iscriviti al corso online

Libri consigliati

In qualità di Affiliato Amazon riceviamo un guadagno dagli acquisti idonei


Tutorial consigliati

E-commerce di elettronica

Sul nostro negozio online trovi tutti i componenti usati nei tutorial.
Inoltre hai un'ampia scelta di prodotti tra:

  • Arduino
  • Raspberry Pi
  • Sensori
  • Robotica
  • Componenti elettronici
  • Accessori

Compra online, ricevi comodamente a casa

Visita il negozio