Go Down

Topic: Help with combining 2 sketches together. (Read 1 time) previous topic - next topic

Dec 06, 2012, 08:12 am Last Edit: Dec 18, 2012, 09:34 am by AWOL Reason: 1
Well first of all let me tell you what I want the whole thing to do. I have it connected to a matrix keypad and when any digits are entered, followed by a pound, it saves the string of numbers on to the sd card. That part I have it down. It is the first sketch. Second thing I want it to do is when I connect to it via serial and send a command I want it to print the saved data to the serial. I have a sketch that does that by just reading the sd every time it starts up. So for it to read on my command I just reset the Arduino with sending an R and it just reads the SD card again. I made sketch for that. I just want to combine both sketches to run together. I greatly appreciate any help I can get.
Im using a Adruino Pro with the SparkFun SD shield. I've tried to just combine them, it compiles without any errors but when I upload it nothing happens. Just bunch of hash symbols. And Ive narrowed it down to the line     "ArduinoOutStream cout(Serial);" from the second sketch. When I add this line to the Keypad sketch it compiles and uploads but nothing works.

HERE IS THE FIRST SKETCH:


Code: [Select]
#include <SdFat.h>
#include <Keypad.h>

const int chipSelect = 8;
SdFat sd;
SdFile myFile;

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] =
{
 {'1','2','3'},
 {'4','5','6'},
 {'7','8','9'},
 {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 7, 6, 5, 4 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 2, 3, 9 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char code[1024];
int index;

#define ledpin 13




void setup()
{
 index = 0;

 Serial.begin(9600);
 if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();

}

void writeToFile()
{
 // open the file for write at end like the Native SD library
 if (!myFile.open("codes.txt", O_RDWR | O_CREAT | O_AT_END)) {
   sd.errorHalt("opening codes.txt for write failed");
 }

 code[index] = '\0';
 myFile.println(code);

 // close the file:
 myFile.close();
 Serial.println("done.");
}

void printDigits(byte digits){

}

void loop()
{

 char key = kpd.getKey();
 if(key)  // Check for a valid key.
 {
   Serial.println(key);
   switch (key)
   {
     case '#':
       writeToFile();
       index = 0;
       Serial.print(code);
       break;
     default:
       code[index] = key;
       index++;
   }
 }
}




HERE IS THE SECOND ONE:

Code: [Select]
#include <SdFat.h>
const int chipSelect = 8;
int incomingByte;
SdFat sd;
ArduinoOutStream cout(Serial);
void setup(){
 digitalWrite(10, HIGH); //We need to set it HIGH immediately on boot
 pinMode(10,OUTPUT);     //We can declare it an output ONLY AFTER it's HIGH
                        // (( HACKHACKHACKHACK ))
 Serial.begin(9600);    //So you can watch the time printed

 char c;
 Serial.begin(9600);

 if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();

 
 // open file in current working directory
 ifstream file("codes.TXT");

 if (!file.is_open()) sd.errorHalt("open failed");

 // copy the file to Serial
 while ((c = file.get()) >= 0) cout << c;

 cout << "Done" << endl;

}

void loop(){

 if (Serial.available() > 0)
  {
    incomingByte = Serial.read();
    if (incomingByte == 'R')
      {
        Serial.println("RESET");
      digitalWrite(10, LOW);
      }
 
  }
}


Moderator edit: I went back to this post, hit "modify", highlighted each section of the code, then hit the "#" icon on the toolbar.
I then hit "save".
It has taken me longer to write this comment than it did to make the amendement.


Bump. Can someone please take a look at this code and tell me where Im going wrong.

Arrch


Bump. Can someone please take a look at this code and tell me where Im going wrong.

I'm sure people will be more than happy to once you start following directions.

Grumpy_Mike


Bump. Can someone please take a look at this code and tell me where Im going wrong.

Do you mean by any chance can some one do it for you? Or do you really want help?

No I really want help. Both sketches work fine but when I combine them it just doesn't work. I think Im having a conflict in the loop section with the (Serial.println). That is just a guess. Because I added each section from one sketch to the other and uploaded and testes each line of code. Once I get down to the
Code: [Select]
Serial.begin(115200);
      if (!myFile.open("codes.txt", O_READ))
      Serial.println("code.txt:");
      int data;
      while ((data = myFile.read()) > 0) Serial.write(data);
      // close the file:
      myFile.close();

Thats when everything stops working. Here is the code Im using. Im sorry I just re-read the posting rules and I know Im suppose to post this way.


Code: [Select]

#include <SdFat.h>
#include <Keypad.h>


const int chipSelect = 8;
SdFat sd;
int incomingByte;
SdFile myFile;


const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] =
{
  {
    '1','2','3'    }
  ,
  {
    '4','5','6'    }
  ,
  {
    '7','8','9'    }
  ,
  {
    '*','0','#'    }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {
  5, 4, 3, 2 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {
  9, 7, 6 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char code[1024];
int index;

#define ledpin 13




void setup()
{

  index = 0;
  Serial.begin(115200);
  if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();




}

void writeToFile()
{
  // open the file for write at end like the Native SD library
  if (!myFile.open("codes.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening codes.txt for write failed");
  }

  code[index] = '\0';
  myFile.println(code);

  // close the file:
  myFile.close();
  Serial.println("done.");
}

void printDigits(byte digits){

}

void loop()
{

  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    Serial.println(key);
    switch (key)
    {
    case '#':
      writeToFile();
      index = 0;
      Serial.print(code);
      break;
    default:
      code[index] = key;
      index++;
    }
  }

  if (Serial.available() > 0)
  {

    incomingByte = Serial.read();
    if (incomingByte == 'R')
    {
      Serial.begin(115200);
      if (!myFile.open("codes.txt", O_READ))
      Serial.println("code.txt:");
      int data;
      while ((data = myFile.read()) > 0) Serial.write(data);
      // close the file:
      myFile.close();
     

      }


    }



  }


I followed the tutorial from the link you posted mike but It still didn't work.

Everyone please disregard the first code I posted in the original post.

AWOL

Quote
but It still didn't work.

What exactly does that mean to you, please?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Grumpy_Mike

Quote
think Im having a conflict in the loop section with the (Serial.println). That is just a guess.

No such thing as a conflict with a serial print.

You should only have serial begin once in the sketch in the setup.
Code: [Select]
if (!myFile.open("codes.txt", O_READ))
Has no braces, do you mean to only skip that first line if the condition is not met?

Well sorry for that. I meant to say is that, none of the sketches work. It just sits there when I input numbers it doesnt write it to serial or save them. And when I send the R command it doesnt print whatever was stores on the SD. Nothing works. The MC is on.

if this is not met
Code: [Select]
incomingByte = Serial.read();
    if (incomingByte == 'R')


Then I want it to skip everything after it. Is that what you are sking Mike?

Grumpy_Mike

What I am saying is that if the condition is not met only that one line will be missed. I would have thought if you can not open the file you do not want to read from it.

Code: [Select]
if (Serial.available() > 0)
  {

    incomingByte = Serial.read();
    if (incomingByte == 'R')
    {
      Serial.begin(115200);
      if (!myFile.open("codes.txt", O_READ))
      Serial.println("code.txt:");  // miss this if the file is not open and do the rest of the lines
      int data;
      while ((data = myFile.read()) > 0) Serial.write(data);
      // close the file:
      myFile.close();
     
      }

Yes that is it Mike. If the the file cant be open then skip everything else.

Grumpy_Mike

Quote
If the the file cant be open then skip everything else.

In which case that code should be:-
Code: [Select]
if (Serial.available() > 0)
  {

    incomingByte = Serial.read();
    if (incomingByte == 'R')
    {
      Serial.begin(115200);
      if (!myFile.open("codes.txt", O_READ)) { // you missed this opening brace
      Serial.println("code.txt:");
      int data;
      while ((data = myFile.read()) > 0) Serial.write(data);
      // close the file:
      myFile.close();
     
      }
  } // this is the matching closing brace

    }

Go Up