problem with loops and voids and saving outputs in a SD card
hello, tying save analog output potentiometer , signal pwm (servo) in sd card ".txt" or ".csv", have code doesn't working, hope can find error. below attached photo of work.
thank much.
code: [select]
#include <sd.h> // sd card library
#include <servo.h>
servo myservo; // create servo object control servo
// maximum of 8 servo objects can created
int pos = 0; // variable store 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 arduino/ input servo arduino
int outservo = 0 ;// entrada analoga desde potenciometro arduino/input potentiometer arduino
float refresh_rate = 0.0; //dataloger refresh rate
long id = 1; // use store id # of our reading.
void setup ()
{
serial.begin(9600);
serial.println("initializing card");
//cs 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(", "); // 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 servo on pin 9 servo object
pinmode(outservo,input);
pinmode(inservo,input);
}
void my_loop(void)
{
while( 1 )
{
//read analog inputs , digital outputs
int inservo_val = digitalread(inservo);
int outservo_val = analogread(outservo);
//create data string storing sd card
// use csv format
string datastring= string(id) + " , " + string(inservo_val) + " , " + string(outservo_val);
//open file write to
//only 1 file can open @ 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 0 degrees 45 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo go position in variable 'pos'
delay(15); // waits 15ms servo reach position
}
for(pos = 45; pos>=0; pos-=1) // goes 45 degrees 0 degrees
{
myservo.write(pos); // tell servo go position in variable 'pos'
delay(15); // waits 15ms servo reach 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 much.
what my_loop() function ? never called. why there anyway ?
Arduino Forum > Using Arduino > Programming Questions > problem with loops and voids and saving outputs in a SD card
arduino
Comments
Post a Comment