Pages: [1] 2   Go Down
Author Topic: Help with combining 2 sketches together.  (Read 1115 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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.
« Last Edit: December 18, 2012, 03:34:56 am by AWOL » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use code tags

Read
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
#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();
     

      }


    }



  }

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24449
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but It still didn't work.
What exactly does that mean to you, please?
Logged

"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.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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();
     
      }
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31541
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If the the file cant be open then skip everything else.
In which case that code should be:-
Code:
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

    }
Logged

Pages: [1] 2   Go Up
Jump to: