hello guys, currently i'm working on with the project that control the steps of stepper motor by reading value in text file stored in sd card. i'm using stepper motor, easy driver and microSD shield in this project
here is my code so far
#include <SD.h>
#define DIR_PIN 2
#define STEP_PIN 3
File myFile;
void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
Serial.begin(9600);
while(!Serial){
}
Serial.print("Initializing SD card...");
pinMode(53, OUTPUT);
if (!SD.begin(53)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
}
}
void loop(){
if (myFile.available()){
float val=0;
//myFile.read();
val = myFile.parseFloat();
//rotate a specific number of degrees
rotateDeg(val, 1);
delay(1000);
}
}
void rotateDeg(float deg, float speed){
//rotate a specific number of degrees (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (deg > 0)? HIGH:LOW;
digitalWrite(DIR_PIN,dir);
int steps = abs(deg)*(1/0.225);
float usDelay = (1/speed) * 70;
for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
}
}
This code indicates that the motor rotation is influenced by reading data in a text file.
90
180
260
80
This is the sample data stored in a text file.
I want to control the rotation of the motor like this:
- number 90 is read and motor rotation of 0 until 90 degree which is 90 degree is the new position of rotation point.
- number 180 is read but motor rotation will rotate by taking into account the previous point rotation which is 90 (the new origin) and a steps of rotation is only done by 90 where 180-90 = 90 and so on until the data is finish to read out in a text file.
i also have attach the image of data stored in text file together with this post. please kindly help me to construct the code. thank you.
