Hello I'am beginner in arduino
Sorry for my english
But I have a project to control two servo motor with different speeds (speed in rpm), the speed data in SD card (data in csv). Can you tell me how to read the data in SD card then use that data to control speed of servo motors?
Reply #6here (just as an example, there are probably others) shows you how to use parseInt() to parse the integers out of the SD card data. Then you would use those integer variables in your servo.write()s to set the speed.
(Assuming of course that when you use the words "servo" and "rpm" in the same sentence you mean continuous rotation servos?)
Before you do any of that though, get your servo working by itself just with some values hard-coded. Also use the examples with the SD card library to get your head round using the SD card by itself.
What sort of "servo motor"? It is not possible to control the speed of most standard servos. Even if you have continuous or "360 degree" servos the speed control is usually very poor, certainly not good enough to get to any specific rpm.
Anyhoo, the code below reads integers out of an SD card file and writes them to 2x servos.
The code's based on the dumpfile sketch that comes with the SD library, but uses parseInt() to read the numbers into 2x integer variables which are then written to the servos.
This is the data, which I just created in a text editor:
1000,2000
1100,1900
1500,1500
1200,1800
This is the serial output:
.... parse integers out of sd file ....
Compiler: 4.9.2, Arduino IDE: 10805
Created: 11:26:19, Aug 4 2019
C:\Users\meltDown\Documents\Arduino\sd_parse_integers_for_servo\sd_parse_integers_for_servo.ino
Initializing SD card...card initialized.
Servo 1: 1000, Servo 2: 2000
Servo 1: 1100, Servo 2: 1900
Servo 1: 1500, Servo 2: 1500
Servo 1: 1200, Servo 2: 1800
.... no more data
This is the sketch:
// parse integers out of an SD card file and control servo
// 4 august 2019
/*
BASED ON SD card file dump
This example shows how to read a file from the SD card using the
SD library and send it over the serial port.
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created 22 December 2010
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
#include <Servo.h>
Servo servo1;
Servo servo2;
const int chipSelect = 4;
int servo1Pos;
int servo2Pos;
void setup()
{
servo1.attach(9);
servo2.attach(10);
Serial.begin(9600);
Serial.println(".... parse integers out of sd file ....");
Serial.print("Compiler: ");
Serial.print(__VERSION__);
Serial.print(", Arduino IDE: ");
Serial.println(ARDUINO);
Serial.print("Created: ");
Serial.print(__TIME__);
Serial.print(", ");
Serial.println(__DATE__);
Serial.println(__FILE__);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("servodat.txt");
// if the file is available, read from it:
if (dataFile) {
while (dataFile.available())
{
//parse the next 2 values out of the file:
servo1Pos = dataFile.parseInt();
servo2Pos = dataFile.parseInt();
//print them to the monitor:
Serial.print("Servo 1: ");
Serial.print(servo1Pos);
Serial.print(", Servo 2: ");
Serial.println(servo2Pos);
//and write to the servos:
servo1.writeMicroseconds(servo1Pos);
servo2.writeMicroseconds(servo2Pos);
//let the servos run at that speed a while
delay(1000);
//go back to the top for the next line of data...
//or ....
}
Serial.println(".... no more data");
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening the data file");
}
}
void loop()
{
}