Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 19, 2012, 10:57:00 pm
If I just leave this in the loop section
Code:
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')
        {
         
         
        }
         
       
      }

}

Then it works. It saves the digits.

But as soon as I add anything between the curly brackets after
Code:
incomingByte = Serial.read();
         if (incomingByte == 'R'){
}

Then nothing works. So I think it gets passed the setup. But hey I dont know I could be wrong.
2  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 19, 2012, 02:23:10 pm
Anyone?
3  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 04:21:34 pm
I don't have a problem with pin 13. Im not trying to run anything on pin 13. I was just using that to try and see if it will work. I even tried on pin number 10. Which is not connected to anything. So we can forget about those pins. Even if dont try to send a signal out to any of the pins after the if statement and only try to print something to serial it wont print either. So my issue is not connection with the pins.

And paul the answer to your questions is yes I think so. Because if I dont add to the loop of the first sketch, which the line you asked about is in, then it all works great. So I think it passes that point with no issues.
4  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 03:51:50 pm
Ok I get the led on pin 13. I have even tried pin 10. But lets just forget about the pins, why wont it print to serial?
5  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 03:38:06 pm
Let me just start off by saying thank you guys for taking the time to help me. I tried the debugging code, but when I enter it anywhere in the loop sections then everything stops working again. SO nothing really prints for me to tell you where its stopping. So what i did was slowly take out code until it started working again. What I found out is that it all orks fine if I only run one code in the loop section at a time. But if I want to combine them then I can only go so far as this point
Code:
  if (Serial.available() > 0)
     {

         incomingByte = Serial.read();
         if (incomingByte == 'R')
        {
         
         //if I add anything in here then the whole thing stops working
        }
         
       
      }


Ive tried adding a simple LED turn on pin13 Ive tried to just print a simple word to serial. Nothing Just kills the whole thing.





Here is the complete code again.

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()
{
 
  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.println("RESET");   //this code will kill the hole sketch. If i erase it everything works fine.
        }
         
       
      }

}

SInce it all stops working and I cant see what happens when I write a code after the
Code:
incomingByte = Serial.read();
         if (incomingByte == 'R')
I cant really know if that statement is working. Ive tried to add debug code but like I said if I add any serial.print anywhere in the loop section then nothing happens.
6  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 04:59:55 am
Just tried that. Compiles fine but still nothing. Absolutely no reaction from Arduino when  buttons are pressed or R is sent to it.
7  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 04:28:27 am
Yes that is it Mike. If the the file cant be open then skip everything else.
8  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 03:48:11 am
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?
9  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 03:37:58 am
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.
10  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 03:28:42 am
Everyone please disregard the first code I posted in the original post.
11  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 03:27:32 am
I followed the tutorial from the link you posted mike but It still didn't work.
12  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 18, 2012, 03:24:13 am
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();
     

      }


    }



  }

13  Using Arduino / Programming Questions / Re: Help with combining 2 sketches together. on: December 17, 2012, 10:48:39 pm
Bump. Can someone please take a look at this code and tell me where Im going wrong.
14  Using Arduino / Programming Questions / Help with combining 2 sketches together. on: December 06, 2012, 02:12:56 am
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.
Pages: [1]