I'm pretty new to Arduino coding, but have an intermediate experience with coding in C, so I get the whole #include <library.h> syntax. However, whenever I implement libraries (like for the CAN-bus shield that skpang.co.uk/SparkFun.com sells... http://www.skpang.co.uk/catalog/arduino-canbus-shield-with-usd-card-holder-p-706.html), it seems like the libraries aren't being properly implemented. The IDE kicks back, saying that certain things aren't declared in scope and the like.
I installed the library (unzipped it to the Libraries folder, etc.) and used #include... is there something I'm missing?
The libraries that I'm trying to use doesn't seem too relevant (I've gotten this issue from multiple libraries, all of which seem to be working for other people), but here's an example.
I just chose one at random out of one of the libraries I downloaded.
Here's the code:
/*
* This sketch attempts to initialize a SD card and analyze its structure.
*/
#include <SdFat.h>
#include <SdFatUtil.h>
// offset to partition table
#define PART_OFFSET (512-64-2)
Sd2Card card;
SdVolume vol;
//global for card erase sector size
uint32_t sectorSize;
void sdError(void) {
PgmPrintln("SD error");
PgmPrint("errorCode: ");
Serial.println(card.errorCode(), HEX);
PgmPrint("errorData: ");
Serial.println(card.errorData(), HEX);
return;
}
uint8_t cidDmp(void) {
cid_t cid;
if (!card.readCID(&cid)) {
PgmPrint("readCID failed");
sdError();
return false;
}
PgmPrint("\nManufacturer ID: ");
Serial.println(cid.mid, HEX);
PgmPrint("OEM ID: ");
Serial.print(cid.oid[0]);
Serial.println(cid.oid[1]);
PgmPrint("Product: ");
for (uint8_t i = 0; i < 5; i++) {
Serial.print(cid.pnm[i]);
}
PgmPrint("\nVersion: ");
Serial.print(cid.prv_n, DEC);
Serial.print('.');
Serial.println(cid.prv_m, DEC);
PgmPrint("Serial number: ");
Serial.println(cid.psn);
PgmPrint("Manufacturing date: ");
Serial.print(cid.mdt_month);
Serial.print('/');
Serial.println(2000 + cid.mdt_year_low + (cid.mdt_year_high <<4));
Serial.println();
return true;
}
uint8_t csdDmp(void) {
csd_t csd;
uint8_t eraseSingleBlock;
uint32_t cardSize = card.cardSize();
if (cardSize == 0 || !card.readCSD(&csd)) {
PgmPrintln("readCSD failed");
sdError();
return false;
}
if (csd.v1.csd_ver == 0) {
eraseSingleBlock = csd.v1.erase_blk_en;
sectorSize = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low;
}
else if (csd.v2.csd_ver == 1) {
eraseSingleBlock = csd.v2.erase_blk_en;
sectorSize = (csd.v2.sector_size_high << 1) | csd.v2.sector_size_low;
}
else {
PgmPrintln("csd version error");
return false;
}
sectorSize++;
PgmPrint("cardSize: ");
Serial.print(cardSize);
PgmPrintln(" (512 byte blocks)");
PgmPrint("flashEraseSize: ");
Serial.print(sectorSize, DEC);
PgmPrintln(" blocks");
PgmPrint("eraseSingleBlock: ");
if (eraseSingleBlock) {
PgmPrintln("true");
}
else {
PgmPrintln("false");
}
return true;
}
// print partition table
uint8_t partDmp(void) {
part_t pt;
PgmPrintln("\npart,boot,type,start,length");
for (uint8_t ip = 1; ip < 5; ip++) {
if (!card.readData(0, PART_OFFSET + 16*(ip-1), 16, (uint8_t *)&pt)) {
PgmPrint("read partition table failed");
sdError();
return false;
}
Serial.print(ip, DEC);
Serial.print(',');
Serial.print(pt.boot,HEX);
Serial.print(',');
Serial.print(pt.type, HEX);
Serial.print(',');
Serial.print(pt.firstSector);
Serial.print(',');
Serial.println(pt.totalSectors);
}
return true;
}
void volDmp(void) {
PgmPrint("\nVolume is FAT");
Serial.println(vol.fatType(), DEC);
PgmPrint("blocksPerCluster: ");
Serial.println(vol.blocksPerCluster(), DEC);
PgmPrint("clusterCount: ");
Serial.println(vol.clusterCount());
PgmPrint("fatStartBlock: ");
Serial.println(vol.fatStartBlock());
PgmPrint("fatCount: ");
Serial.println(vol.fatCount(), DEC);
PgmPrint("blocksPerFat: ");
Serial.println(vol.blocksPerFat());
PgmPrint("rootDirStart: ");
Serial.println(vol.rootDirStart());
PgmPrint("dataStartBlock: ");
Serial.println(vol.dataStartBlock());
if (vol.dataStartBlock()%sectorSize) {
PgmPrintln("Data area is not aligned on flash erase boundaries!");
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
PgmPrintln("\ntype any character to start");
while (!Serial.available());
Serial.flush();
uint32_t t = millis();
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
uint8_t r = card.init(SPI_HALF_SPEED);
t = millis() - t;
if (!r) {
PgmPrintln("\ncard.init failed");
sdError();
return;
}
PgmPrint("\ninit time: ");
Serial.println(t);
PgmPrint("\nCard type: ");
switch(card.type()) {
case SD_CARD_TYPE_SD1:
PgmPrintln("SD1");
break;
case SD_CARD_TYPE_SD2:
PgmPrintln("SD2");
break;
case SD_CARD_TYPE_SDHC:
PgmPrintln("SDHC");
break;
default:
PgmPrintln("Unknown");
}
if(!cidDmp()) return;
if(!csdDmp()) return;
if(!partDmp()) return;
if (!vol.init(&card)) {
PgmPrintln("\nvol.init failed");
sdError();
return;
}
volDmp();
}
Here's the error list:
SdFatInfo:10: error: 'Sd2Card' does not name a type
SdFatInfo:11: error: 'SdVolume' does not name a type
SdFatInfo.pde: In function 'void sdError()':
SdFatInfo:17: error: 'PgmPrintln' was not declared in this scope
SdFatInfo:18: error: 'PgmPrint' was not declared in this scope
SdFatInfo:19: error: 'card' was not declared in this scope
SdFatInfo.pde: In function 'uint8_t cidDmp()':
SdFatInfo:26: error: 'cid_t' was not declared in this scope
SdFatInfo:26: error: expected `;' before 'cid'
SdFatInfo:27: error: 'card' was not declared in this scope
SdFatInfo:27: error: 'cid' was not declared in this scope
SdFatInfo:28: error: 'PgmPrint' was not declared in this scope
SdFatInfo:32: error: 'PgmPrint' was not declared in this scope
SdFatInfo:33: error: 'cid' was not declared in this scope
SdFatInfo.pde: In function 'uint8_t csdDmp()':
SdFatInfo:56: error: 'csd_t' was not declared in this scope
SdFatInfo:56: error: expected `;' before 'csd'
SdFatInfo:58: error: 'card' was not declared in this scope
SdFatInfo:59: error: 'csd' was not declared in this scope
SdFatInfo:60: error: 'PgmPrintln' was not declared in this scope
SdFatInfo:64: error: 'csd' was not declared in this scope
SdFatInfo:73: error: 'PgmPrintln' was not declared in this scope
SdFatInfo:77: error: 'PgmPrint' was not declared in this scope
SdFatInfo:79: error: 'PgmPrintln' was not declared in this scope
SdFatInfo.pde: In function 'uint8_t partDmp()':
SdFatInfo:94: error: 'part_t' was not declared in this scope
SdFatInfo:94: error: expected `;' before 'pt'
SdFatInfo:95: error: 'PgmPrintln' was not declared in this scope
SdFatInfo:97: error: 'card' was not declared in this scope
SdFatInfo:97: error: 'pt' was not declared in this scope
SdFatInfo:98: error: 'PgmPrint' was not declared in this scope
SdFatInfo:104: error: 'pt' was not declared in this scope
SdFatInfo.pde: In function 'void volDmp()':
SdFatInfo:116: error: 'PgmPrint' was not declared in this scope
SdFatInfo:117: error: 'vol' was not declared in this scope
SdFatInfo:133: error: 'PgmPrintln' was not declared in this scope
SdFatInfo.pde: In function 'void loop()':
SdFatInfo:142: error: 'PgmPrintln' was not declared in this scope
SdFatInfo:148: error: 'card' was not declared in this scope
SdFatInfo:148: error: 'SPI_HALF_SPEED' was not declared in this scope
SdFatInfo:155: error: 'PgmPrint' was not declared in this scope
SdFatInfo:159: error: 'SD_CARD_TYPE_SD1' was not declared in this scope
SdFatInfo:162: error: 'SD_CARD_TYPE_SD2' was not declared in this scope
SdFatInfo:165: error: 'SD_CARD_TYPE_SDHC' was not declared in this scope
SdFatInfo:174: error: 'vol' was not declared in this scope
It looks like a ton of types and structures that are somehow constructed in the libraries aren't showing up, for some reason.
Nerdenator:
I'm pretty new to Arduino coding, but have an intermediate experience with coding in C, so I get the whole #include <library.h> syntax. However, whenever I implement libraries (like for the CAN-bus shield that skpang.co.uk/SparkFun.com sells... http://www.skpang.co.uk/catalog/arduino-canbus-shield-with-usd-card-holder-p-706.html), it seems like the libraries aren't being properly implemented. The IDE kicks back, saying that certain things aren't declared in scope and the like.
I installed the library (unzipped it to the Libraries folder, etc.) and used #include... is there something I'm missing?
Did you shut down and restart the IDE after installing the library?
The IDE only sees what is there at the time it starts. If you add something while it's running, you won't see it until after you restart the IDE and let it "see" the new things.
There are a few odd things about that. For one thing, it seems to supply various things (like SPI) that already exist as libraries. Second, the SdFat library seems to me to be one level too low. For a third thing, the folder name is different to your screenshot. Also SoftwareSerial is now used in place of NewSoftSerial.
If it's the difference in path I think you're referring to, it's because one was just a shot of the "libraries" view in Windows 7 vs. the old-fashioned way of using "My Documents". They're ultimately the same folder.
I went ahead and cleared out the libraries to get started from a fresh slate; what do you propose to fix the issues you noticed?
Nerdenator:
If it's the difference in path I think you're referring to, it's because one was just a shot of the "libraries" view in Windows 7 vs. the old-fashioned way of using "My Documents". They're ultimately the same folder.
No, they're not. You have SdFat inside CANBus2. It should be inside libraries.
what do you propose to fix the issues you noticed?
Put those folders inside libraries, not inside CANBus2. Also you may not need SPI at all. As for NewSoftSerial, that suggests the whole thing is a bit old, however you could run with it and see what happens.
As you can see from my screenshot I ended up with 2 x CANInterface folders. Assuming you moved all the lower-level ones up a level (which would require renaming the upper CANInterface as "foo" and then deleting it afterwards) then inside libraries/CANInterface you would expect to see various .cpp and .h files, along with an "examples" folder.