So far, I’ve had a lot of great results with both the PS2Keyboard and ReadWrite libraries, however combining the two is proving to be more difficult than I expected. Here is my current code:
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
#include <PS2Keyboard.h>
const int DataPin = 8;
const int IRQpin = 3;
PS2Keyboard keyboard;
File myFile;
void setup()
{
// Open serial communications and wait for port to open:
keyboard.begin(DataPin, IRQpin, PS2Keymap_US);
Serial.begin(9600);
Serial.println("'MURICAN Keyboard Test:");
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
// Let's take the keyboard input...
if (keyboard.available()) {
char c = keyboard.read();
Serial.print(c);
// ...print it in the Serial Monitor...
myFile.println(c);
// ...and print it again in the file.
// NOTE: Make sure to somehow limit the number of characters per line.
}
else {Serial.print("No keyboard detected."};
// close the file:
myFile.close();
Serial.println("done.");
}
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}
I’m barely scratching the surface on this project, I suspect…Any suggestions?
but now I seem to be having trouble with the else statement at the end of this section:
Well, your indenting leaves a lot to be desired. Using Tools + Auto Format just might make it possible to see what the problem is.
I suggest that you take your snippets over to http://snippets-r-us.com. Here, strange as it may seem, we really need to see all of your code and to know just what "trouble" you are having.
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SD.h>
#include <PS2Keyboard.h>
const int DataPin = 8;
const int IRQpin = 3;
PS2Keyboard keyboard;
File myFile;
void setup()
{
// Open serial communications and wait for port to open:
keyboard.begin(DataPin, IRQpin, PS2Keymap_US);
Serial.begin(9600);
Serial.println("'MURICAN Keyboard Test:");
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
// Let's take the keyboard input...
if (keyboard.available()) {
char c = keyboard.read();
Serial.print(c);
// ...print it in the Serial Monitor...
myFile.println(c);
// ...and print it again in the file.
// NOTE: Make sure to somehow limit the number of characters per line.
}
else {Serial.print("No keyboard detected.");
}
// close the file:
myFile.close();
Serial.println("done.");
}
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}
Verifying the code results in this message:
“expected unqualified-id before ‘else’”
Not familiar with Auto Format, but using it brings up the following message:
“Auto Format Canceled: Too many right curly braces.”
Sorry for being unclear, but I’ve got a bunch of other things going on at the moment.
Not familiar with Auto Format, but using it brings up the following message:
"Auto Format Canceled: Too many right curly braces."
You will have to balance the number of open and close curly braces. Since you didn't as you were typing the code, you will need to manually indent the code to figure out where you left the left curly brace out. It will probably be somewhere near where the compiler doesn't like the else statement being placed.