Problems using Adafruit RGB LCD Shield

Hi all!

A couple years ago, I used an Arduino board for a project in school. After re-sparking my interest and looking up some projects, I found one that interested me and seemed relatively easy to do. However, I have very little experience in C++. I didn't do any of the coding on my last project, so when I compiled this code that I got from Github (Adafruit-Programmable-Piggy-Bank/piggybank.ino at master · adafruit/Adafruit-Programmable-Piggy-Bank · GitHub) and ran into some errors, I was quite dumbfounded.

The project that I'm trying to construct is a piggy bank that accepts up to 4 coins (this code was meant for a 1 coin acceptor) and keeps track of the total amount of money along the way. The original code also includes an LED, which I am deciding to leave out. I have all of the necessary components (arduino, LCD shield, 4 coin acceptor, etc.) but when I try to compile the code, I get an error.

This is the original code:

[code]/*********************
* connect the COIN wire to digital 2
* set the side switches to "FAST" "NC"
**********************/
// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
// attach coin wire to digital 2
#define COIN 2
int coins;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int fadebrightness = 0; // how bright the LED is when it's flashing
void setup() {
// declare pin 11 to be an output for LED:
pinMode(11, OUTPUT);
// Debugging output
Serial.begin(9600);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
pinMode(COIN, INPUT);
digitalWrite(COIN, HIGH); // pull up
coins = 0;
}
void loop() {
lcd.setCursor(0,0);
lcd.print(" PLEASE INSERT ");
lcd.setCursor(0,1);
lcd.print(" ONE QUARTER ");
// set the brightness of pin 11:
analogWrite(11, brightness);
// while the coin pin is low (no coin detected), do nothing
while (! digitalRead(COIN)) {
delay(1);
}
// while the pin is high, we'll track the length with a counter
uint8_t counter = 0;
while (digitalRead(COIN)) {
delay(1);
counter++;
}
Serial.print(counter);
Serial.println(" ms long pulse");
if ((counter > 60) || (counter < 20))
return;
coins++;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" OINK OINK! ");
lcd.setCursor(0,1);
lcd.print("YOU HAVE $");
lcd.print(coins*.25);
lcd.print(" ");
// loop through to flash the LED
fadebrightness = brightness;
for (int i = brightness; i < 510; i+=5) {
// turn the pin on:
analogWrite(11, fadebrightness);
fadebrightness = fadebrightness + fadeAmount;
delay(20);
// reverse the direction of the fading at the ends of the fade:
if (fadebrightness == 0 || fadebrightness == 255) {
fadeAmount = -fadeAmount;
}
}
brightness = brightness + 5;
}

[/code]

And these are the errors that I get:

piggy_bank_code:19: error: 'Adafruit_RGBLCDShield' does not name a type
piggy_bank_code.ino: In function 'void setup()':
piggy_bank_code:43: error: 'lcd' was not declared in this scope
piggy_bank_code.ino: In function 'void loop()':
piggy_bank_code:51: error: 'lcd' was not declared in this scope
Adafruit_RGBLCDShield.ino: At global scope:
Adafruit_RGBLCDShield:106: error: 'Adafruit_MCP23017' does not name a type

Leaving out the code for the LED is simple enough, and I could probably tinker with the coding for 1 coin to allow for 4 different coins, but these errors are really confusing me. I've googled each of the error messages but like I said, I have very little C++ experience, so I'm not sure if the little information that I am finding is helpful at all. Any help would be extremely appreciated. Thank you!

-Rex

Hi all!

A couple years ago, I used an Arduino board for a project in school. After re-sparking my interest and looking up some projects, I found one that interested me and seemed relatively easy to do. However, I have very little experience in C++. I didn't do any of the coding on my last project, so when I compiled this code that I got from Github (Adafruit-Programmable-Piggy-Bank/piggybank.ino at master · adafruit/Adafruit-Programmable-Piggy-Bank · GitHub) and ran into some errors, I was quite dumbfounded.

The project that I'm trying to construct is a piggy bank that accepts up to 4 coins (this code was meant for a 1 coin acceptor) and keeps track of the total amount of money along the way. The original code also includes an LED, which I am deciding to leave out. I have all of the necessary components (arduino, LCD shield, 4 coin acceptor, etc.) but when I try to compile the code, I get an error.

This is the original code:

/*********************
* connect the COIN wire to digital 2
* set the side switches to "FAST" "NC"
**********************/
// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
// attach coin wire to digital 2
#define COIN 2
int coins;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int fadebrightness = 0; // how bright the LED is when it's flashing
void setup() {
// declare pin 11 to be an output for LED:
pinMode(11, OUTPUT);
// Debugging output
Serial.begin(9600);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
pinMode(COIN, INPUT);
digitalWrite(COIN, HIGH); // pull up
coins = 0;
}
void loop() {
lcd.setCursor(0,0);
lcd.print(" PLEASE INSERT ");
lcd.setCursor(0,1);
lcd.print(" ONE QUARTER ");
// set the brightness of pin 11:
analogWrite(11, brightness);
// while the coin pin is low (no coin detected), do nothing
while (! digitalRead(COIN)) {
delay(1);
}
// while the pin is high, we'll track the length with a counter
uint8_t counter = 0;
while (digitalRead(COIN)) {
delay(1);
counter++;
}
Serial.print(counter);
Serial.println(" ms long pulse");
if ((counter > 60) || (counter < 20))
return;
coins++;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" OINK OINK! ");
lcd.setCursor(0,1);
lcd.print("YOU HAVE $");
lcd.print(coins*.25);
lcd.print(" ");
// loop through to flash the LED
fadebrightness = brightness;
for (int i = brightness; i < 510; i+=5) {
// turn the pin on:
analogWrite(11, fadebrightness);
fadebrightness = fadebrightness + fadeAmount;
delay(20);
// reverse the direction of the fading at the ends of the fade:
if (fadebrightness == 0 || fadebrightness == 255) {
fadeAmount = -fadeAmount;
}
}
brightness = brightness + 5;
}

And these are the errors that I get:

piggy_bank_code:19: error: 'Adafruit_RGBLCDShield' does not name a type
piggy_bank_code.ino: In function 'void setup()':
piggy_bank_code:43: error: 'lcd' was not declared in this scope
piggy_bank_code.ino: In function 'void loop()':
piggy_bank_code:51: error: 'lcd' was not declared in this scope
Adafruit_RGBLCDShield.ino: At global scope:
Adafruit_RGBLCDShield:106: error: 'Adafruit_MCP23017' does not name a type

Leaving out the code for the LED is simple enough, and I could probably tinker with the coding for 1 coin to allow for 4 different coins, but these errors are really confusing me. I've googled each of the error messages but like I said, I have very little C++ experience, so I'm not sure if the little information that I am finding is helpful at all. Any help would be extremely appreciated. Thank you!

-Rex

I'm going to guess that you downloaded and installed the file, "Adafruit_RGBLCDShield.h," but didn't do the same with "Adafruit_RGBLCDShield.cpp." Your error messages show a reference to Adafruit_RGBLCDShield.h, which makes me think that particular library is available. The error messages say that the compiler doesn't know how to build an "Adafruit_RGBLCDShield" object, and that makes me think that the corresponding .cpp file is absent. I can't verify it, but I'll bet that "Adafruit_MCP23017.cpp" isn't available on your computer, either.

Interesting. Well I have a "Adafruit_MCP23017.h", so I took your advice and changed it to "Adafruit_MCP23017.cpp". The original errors went away, but now there are four (I guess technically two) other errors:

Adafruit_MCP23017.cpp:20: error: 'uint8_t' has not been declared
Adafruit_MCP23017.cpp:21: error: 'uint8_t' does not name a type

Adafruit_MCP23017.cpp:22: error: 'uint16_t' has not been declared
Adafruit_MCP23017.cpp:23: error: 'uint16_t' does not name a type

Any idea on how to solve this? You seem to be heading in the right direction, I just wish I could make sense of any of this to be able to help myself better as well.

Something is peculiar about those library files. I'm not sure what they were thinking, and I certainly didn't intend that you delete the #include directive for "Adafruit_MCP23017.h" and replace it with "Adafruit_MCP23017.cpp". But, looking at the library, that seems to be how you have to do it. I still can't see anything that includes "Adafruit_RGBLCDShield.cpp," so you may ultimately have to include it explicitly. But, try what I suggest below, and we'll see what happens after that.

The errors that you report - the compiler seems to be unaware of the definitions of some fundamental data types - seem to be related to a quirk of the IDE. Try moving this statementint coins;to the top of the sketch. I think that there's something peculiar about the way that the IDE processes its own included files - that it puts them right after the first statement in the sketch that isn't a comment or a compiler directive. So, the code that defines uint8_t and uint16_t is included somewhere below the earlier included files, and, wherever they reference those types, the compiler throws an error.

At least, I think that's what's happening. There are others in this forum that can describe the issue in detail, and I hope they chime in.

OK, when I try your code, as posted in the original post, with the library properly installed, it compiles without complaint. So, I think that there's some quirk in the way the library is installed on your system. Mine has all of the *.h and *.cpp files, and keywords.txt. That's this list:

  • Adafruit_MCP23017.cpp
  • Adafruit_MCP23017.h
  • Adafruit_RGBLCDShield.cpp
  • Adafruit_RGBLCDShield.h
  • keywords.txt

That's everything except the example, the ReadMe file, which seems to be some kind of an advertisement, and the license file. To install the library, I followed the instructions, quite precisely, that are linked from Adafruit's product page for installing libraries. I'm using Ubuntu 14.04.

Yea, I had all those files downloaded but I guess just not correctly. I followed your advice and started over from scratch, downloading the library correctly, and it worked! Thank you all so much!!

Although no one responded, I figured this out. Turns out I just didn't have the library downloaded correctly.

Topics merged. Please post in only 1 place to avoid duplicate conversations.