Hello, i am tying to save the analog output from a potentiometer and the signal PWM (servo) in an SD card like ".txt" or ".csv", i have my code but it doesn't working, i hope you can find my error. Below i attached a photo of my work.
#include <SD.h> // SD card library
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
///int pin= 9;
//SPI SD Card pins
// MOSI = Pin 11
//MISO = Pin 12
//SCLK = Pin 13
int CS_pin=10; //ChipSelect
////int pow_pin = 8;//Pin de poder
// Entradas al Arduino desde el servo y el potenciometro
int inservo = 4 ; // entrada digital desde el servo a arduino/ input from servo to arduino
int outservo = 0 ;// entrada analoga desde potenciometro a arduino/input from potentiometer to arduino
float refresh_rate = 0.0; //Dataloger Refresh Rate
long id = 1; // Use this to store id # of our reading.
void setup ()
{
Serial.begin(9600);
Serial.println("Initializing Card");
//CS is an output
pinMode(CS_pin, OUTPUT);
////digitalWrite(pow_pin,HIGH);
//Initialize card
if (!SD.begin(CS_pin))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
/////Write log File Header
File logFile = SD.open("LOG.csv",FILE_WRITE);
if (logFile)
{
logFile.println(", "); // Just a leading blank line, incase tghere weas previuos data
String header = " Inservo , Outservo ";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
}
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(outservo,INPUT);
pinMode(inservo,INPUT);
}
void my_loop(void)
{
while( 1 )
{
//Read analog inputs and digital outputs
int inservo_val = digitalRead(inservo);
int outservo_val = analogRead(outservo);
//Create Data string for storing to SD card
// we will use the CSV Format
String dataString= String(id) + " , " + String(inservo_val) + " , " + String(outservo_val);
//Open a file to write to
//Only one file can be open at time
File logFile = SD.open("LOG.csv",FILE_WRITE);
if (logFile)
{
logFile.println(dataString);
logFile.close();
Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
//Incremet ID number
id++;
delay(refresh_rate);
}
}
void loop()
{
for(pos = 0; pos < 45; pos += 1) // goes from 0 degrees to 45 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 45; pos>=0; pos-=1) // goes from 45 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 0; pos <45; pos+=1)
{
myservo.write(pos);
delay(25);
}
for(pos = 45; pos <90; pos+=1)
{
myservo.write(pos);
delay(15);
}
for(pos = 90; pos >=45; pos-=1)
{
myservo.write(pos);
delay(15);
}
for(pos = 45; pos >=0; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
Thank you very much.
