Indice
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.
#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, 0, 1023, 0, 179);
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);
}
#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, 0, 1023, 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;
}
}