system
November 22, 2014, 12:10pm
1
Hi, I would like to sum all the value that I save from text.txt file.
example: Save/text.txt
inside the text is:
1 2 3 3 2 3 2 1...3 2 1 = depending on the number of values i've input ?
#include <SPI.h>
#include <SD.h>
File myFile;
void setup()
{
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop()
{
char c=Serial.read()
if(c=='H') //input value
{
if(!SD.exists(text.txt))
{
myFile = SD.open(text.txt); //Write .txt file in SD, done.
if (myFile) {
myFile.println("2");
myFile.close();
}
}
}
if(c=='L') //input value
{
if(!SD.exists(text.txt))
{
myFile = SD.open(text.txt); //Write .txt file in SD, done.
if (myFile) {
myFile.println("3");
myFile.close();
}
}
}
if(c=='S')
{
myFile = SD.open(text.txt);
//[i]then sum-up all the values that is input in text.txt[/i]
}
}
system
November 23, 2014, 12:34am
3
thank you sir mrburnette solved
system
November 24, 2014, 3:35am
4
but then using I2C, i got problem.
seems i can't pass the bufs value of from loop() to void requestEvent() to pass it to master using Wire.h.
void loop()
{
if(s=='H')
{
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) {
S1 = myFile.parseInt();
a=a+S1-0;
}
Serial.print(a); //it can display the total sum of test.txt value.
myFile.close();
}
sprintf(bufs,"sum:%lu",a); //the value of bufs will be pass for I2C com. but i can't get it.
}
a='\0';
delay(5000);
}
void requestEvent()
{
Wire.write(bufs);
}
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
int S1;
char i;
int a=0;
void setup()
{
Wire.begin(5);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
randomSeed(analogRead(0));
lcd.print("Initialization done");
delay(2000);
}
void loop()
{
if(s=='H') //from void receiveEvent(int howMany)
{
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) {
S1 = myFile.parseInt();
a=a+S1-0;
Serial.print(S1);
}
Serial.print(a);
myFile.close();
}
sprintf(bufs,"sum:%lu",a);
}
a='\0';
delay(5000);
}
void receiveEvent(int howMany)
{
while(Wire.available() > 0)
{
char c = Wire.read();
numbers[i++] = c;
numbers*='\0';*
}*
if (strcmp (numbers,"A")==0) {
{*
s='H'; //pass to void loop()*
}*
}
void requestEvent()
{
Wire.write(bufs); // i can't get the bufs value from void loop().*
}
it can display the total sum from test.txt file after summing up to Serial monitor but i need to get the value and display to master.
i can't pass the value from slave to master.
system
November 24, 2014, 4:25am
5
here's my first code, which is not in void loop(). but when i send data "A" from master to trigger,
my problem is it hang and nothing happen. help.
char bufs[70];
int S1;
int a=0;
void setup()
{
// SD initialization
}
void loop()
{
//no code here, all the codes are in void receiveEvent(int howMany)
}
void receiveEvent(int howMany)
{
while(Wire.available() > 0)
{
char c = Wire.read();
numbers[i++] = c;
numbers='\0';
}
if (strcmp (numbers,"A")==0) {
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) {
S1 = myFile.parseInt();
a=a+S1-0;
Serial.print(S1);
}
Serial.print(a);
myFile.close();
}
sprintf(bufs,"sum:%lu",a);
}
}
void requestEvent()
{
Wire.write(bufs);
a='\0';
}