Pages: 1 [2]   Go Down
Author Topic: Help with combining 2 sketches together.  (Read 1294 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

Just tried that. Compiles fine but still nothing. Absolutely no reaction from Arduino when  buttons are pressed or R is sent to it.
Logged

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

I didn't say it would fix it, I am looking for errors you made.
Have you made the same error anywhere else?
Logged

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

OP, please post your code.
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: 619
Posts: 33974
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Absolutely no reaction from Arduino when  buttons are pressed or R is sent to it.
So put some debug printing in and see what you get. Post the code with the debug printing in it and say what happens.
Logged

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are using an SD card reader/writer. That is an SPI device. That means that it uses pins 11, 12, and 13. That means you can NOT use the onboard LED for ANYTHING. Or pin 13.
Logged

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

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

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But lets just forget about the pins

Let's not. Start by fixing the issues you already have, then if the issue persists, post the updated code. It's possible that the pin conflict could be causing a hold up in another part of the code.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();
Do you know that you get past this?
Logged

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

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

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

Anyone?
Logged

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

Anyone what?
We seem to be short on output and observations.
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: 619
Posts: 33974
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
If tharpt is truly happening then it is not getting out of setup so see where this hangs.
Logged

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

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

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

Quote
Then it works. It saves the digits.
But there is no call to the write file so how can it save the digits?
Logged

Pages: 1 [2]   Go Up
Jump to: