I have written a library, but have problem with error does not name a type. I’ve tryed everything, searched for couple of hours and no luck. Library is placed in the «libraries» folder of the arduino sketch folder. Please help!!! I am using OSX, but the same problem occurs on Windows also.
This is header file of the library:
#ifndef OpticalSensor_h
#define OpticalSensor_h
#include <Arduino.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include <String.h>
class OpticalSensor
{
public:
OpticalSensor(int analogPort);
void LCDInit(int columns, int rows);
void SerialInit(int bitRate);
void SDInit();
double& ReadFromAnalogPort();
void SDCreateFile(String fileName);
void SDDeleteFile(String fileName);
void SDWriteToFile(String fileName);
void SDStreamToFile(String Text);
void SDOpenFileToStream(String fileName);
private:
int _analogPort;
bool _displayFlag;
Adafruit_RGBLCDShield _lcd;
File _MainRecFile;
double _voltage;
void _LCDClearAll();
void _LCDWriteInTwoRows(String row1, String row2);
void _DelayAndClearLCD(bool returnStatus);
};
#endif
This is .cpp file of the library:
#include <OpticalSensor.h>
Adafruit_RGBLCDShield _lcd;
File _MainRecFile;
double _voltage;
OpticalSensor::OpticalSensor(int analogPort)
{
_analogPort = analogPort;
}
void OpticalSensor::LCDInit(int columns, int rows)
{
_lcd = Adafruit_RGBLCDShield();
_lcd.begin(columns,rows);
}
void OpticalSensor::SerialInit(int bitRate)
{
Serial.begin(bitRate);
_bitRate = bitRate;
while(!Serial) {
//wait until serial is not open
}
}
void OpticalSensor::SDInit()
{
// 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);
//check if SD can be found and initialized. Print also message to
//Serial if initialized and to _lcd if initialized.
if(!SD.begin(4)) {
if(Serial){
Serial.println("Initialization failed!");
}
if(_lcd){
_lcd.print("Init failed!");
}
_DelayAndClearLCD(true);
}
else {
if(Serial) {
Serial.println("Initialization done!");
}
if(_lcd) {
lcd.print("Init done!");
}
_DelayAndClearLCD(false);
}
}
void OpticalSensor::SDCreateFile(String fileName)
{
//check if file allready exists, if not it creates one
//and writes apropriate response to
//lcd and Serial if they are initialized.
if(SD.exists(fileName)) {
if(Serial) {
Serial.println(fileName + " already exists!");
}
if(_lcd) {
_LCDWriteInTwoLines(fileName,"already exists!");
}
_DelayAndClearLCD(false);
}
else
{
if(Serial) {
Serial.println(fileName + "Creating file " + fileName + "...");
}
if(_lcd) {
_LCDWriteInTwoLines("Creating file", fileName);
}
_MainRecFile = SD.open(fileName + ".txt", FILE_WRITE);
_MainRecFile.close();
_DelayAndClearLCD(false);
//check if file was created successffully and print apropriate response
//to lcd and Serial if they are initialized
if(SD.exists(fileName + ".txt")) {
if(Serial) {
Serial.println(fileName + ".txt" + " created successffully!");
}
if(_lcd) {
_LCDWriteInTwoLines(fileName + ".txt", "created!");
}
_DelayAndClearLCD(false);
}
else {
if(Serial) {
Serial.println("error: failed to create file!");
}
if(_lcd) {
_LCDWriteInTwoLines("error: failed to","create file!");
}
_DelayAndClearLCD(false);
}
}
}
//delete file from SD card
void OpticalSensor::SDDeleteFile(String fileName)
{
}
//open file, write data to it, and close file after.
void OpticalSensor::SDWriteToFile(String fileName, String Text)
{
_MainRecFile = SD.open(fileName + ".txt", FILE_WRITE);
_MainRecFile.println(Text);
_MainRecFile.close();
}
//Open file to stream data to it.
void OpticalSensor::SDOpenFileToStream(String fileName)
{
_MainRecFile = SD.open(fileName + ".txt", FILE_WRITE);
}
//Write data to file while file is open.
//Notice that you can stream data only to one file at a time!!!
//For instance, if you have two sensors that you want to
//write data to two different files, you have to use SDWriteToFile
//function!!!
void OpticalSensor::SDStreamToFile(String Text)
{
if(_MainRecFile) {
_MainRecFile.println(Text);
}
}
//close file that you streamed data too.
void OpticalSensor::SDCloseStreaming(String fileName)
{
_MainRecFile.close();
}
//clear entire LCD
void OpticalSensor::_LCDClearAll()
{
_lcd.clear();
_lcd.setCursor(0,0);
}
void OpticalSensor::_LCDWriteInTwoRows(String row1, String row2)
{
//write first String in row1
_lcd.print(row1);
//set cursor to the beginning of row 2
_lcd.setCursor(0,1);
//write second String to row 2
_lcd.print(row2);
}
void OpticalSensor::_DelayAndClearLCD(bool returnStatus)
{
//if Serial or _lcd are initialized, delay for 2 seconds
//and clear LCD
if(Serial || _lcd) {
delay(2000);
if(_lcd)
_LCDClearAll();
}
//terminate
if(bool == true) {
return;
}
}
double& ReadFromAnalogPort()
{
_voltage = analogRead(_analogPort);
return _voltage;
}
And this is the .ino file where library is included:
#include <OpticalSensor.h>
OpticalSensor sensor(0);
void setup() {
sensor.LCDInit(16,2);
sensor.SerialInit(9600);
sensor.SDInit();
sensor.SDCreateFile("test1");
sensor.SDOpenFileToStream("test1");
}
void loop() {
}
this is the error:
In file included from Test_OpticalSensorLib.ino:1:
/Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:34:
error: ‘Adafruit_RGBLCDShield’ does not name a type
/Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:35:
error: ‘File’ does not name a type
I’m getting a "'Serial' does not name a type" error when this program is compiled. I need for the 8 channels of voltage to be displayed. I’m sure it’s a simple fix, however I’m still learning Arduino programming. I thank you for your help.
int pin_Out_S0 = 8;
int pin_Out_S1 = 9;
int pin_Out_S2 = 10;
int pin_In_Mux1 = A0;
int Mux1_State[8] = {0};
//float Mux1_State[i] =0;
int RawValue=0;
float Voltage = 0;
void setup() {
pinMode(pin_Out_S0, OUTPUT);
pinMode(pin_Out_S1, OUTPUT);
pinMode(pin_Out_S2, OUTPUT);
pinMode(pin_In_Mux1, INPUT);
Serial.begin(9600);
}
void loop() {
RawValue = analogRead(pin_In_Mux1);
Voltage = (RawValue * 5.0) / 1024; //scale the ADC
updateMux1();
//Serial.println(Mux1_State);
for(int i = 0; i < 8; i ++) {
if(i == 7) {
Serial.println(Mux1_State[i]);
} else {
Serial.print(Mux1_State[i]);
Serial.print(",");
//vout = (value * 5.0) / 1024.0;
}
}
}
void updateMux1 () {
for (int i = 0; i < 8; i++){
digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
Mux1_State[i] = analogRead(pin_In_Mux1);
}
}
Serial.print("Raw Value = " );
Serial.print(RawValue);
asked Mar 4, 2016 at 3:45
2
Your last two lines are not in any function. This confuses the compiler. You need to move those two statements inside a function.
Because those statements refer to RawValue, I’m guessing you want to put them in loop(), where RawValue is being updated.
Also, remember that the IDE will tell you on which line the error is, which is helpful when debugging compilation errors.
answered Mar 4, 2016 at 4:01
uint128_tuint128_t
8185 silver badges14 bronze badges
Please put these lines inside the loop() function block:
Serial.print("Raw Value = " );
Serial.print(RawValue);
Should look something like:
void loop() {
RawValue = analogRead(pin_In_Mux1);
Voltage = (RawValue * 5.0) / 1024; //scale the ADC
updateMux1();
//Serial.println(Mux1_State);
for(int i = 0; i < 8; i ++) {
if(i == 7) {
Serial.println(Mux1_State[i]);
} else {
Serial.print(Mux1_State[i]);
Serial.print(",");
//vout = (value * 5.0) / 1024.0;
}
}
Serial.print("Raw Value = " );
Serial.print(RawValue);
}
That should at least remove the first issue with your code.
Cheers!
answered Mar 4, 2016 at 21:27
Mikael PatelMikael Patel
7,9192 gold badges12 silver badges21 bronze badges
In addition to the good answers by Mikael and Uint, everything needs to be inside {curly braces} otherwise the IDE does not know where it belongs. If you didn’t want the last 2 lines in your loop segment you could put them in a separate void xxxx () section to be called when the time is right (by using an ISR with an enableinterrupt function for example).
![]()
gre_gor
1,6564 gold badges18 silver badges28 bronze badges
answered Aug 24, 2018 at 16:02
MicrokMicrok
1159 bronze badges
I am continuously getting error message
‘mqtt’ does not name a type
I am using Arduino 1.8.1, i tried using following libraries
#include <ESP8266WiFi.h>
#include «Adafruit_MQTT.h»
#include «Adafruit_MQTT_Client.h»
#include <ESP8266Ping.h>
#include <PubSubClient.h>
Still facing the issue, kindly help
Error Message:
Arduino: 1.8.10 (Windows 10), Board: «NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled, All SSL ciphers (most compatible), 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200»
t4:120:3: error: ‘mqtt’ does not name a type
mqtt.subscribe(&R1);
^
t4:121:3: error: ‘mqtt’ does not name a type
mqtt.subscribe(&R2);
^
t4:122:3: error: ‘mqtt’ does not name a type
mqtt.subscribe(&R3);
^
t4:123:3: error: ‘mqtt’ does not name a type
mqtt.subscribe(&R4);
^
t4:124:3: error: ‘mqtt’ does not name a type
mqtt.subscribe(&R5);
^
t4:125:3: error: ‘mqtt’ does not name a type
mqtt.subscribe(&R6);
^
t4:126:3: error: ‘mqtt’ does not name a type
mqtt.subscribe(&R7);
^
t4:127:3: error: ‘mqtt’ does not name a type
mqtt.subscribe(&R8);
^
t4:129:1: error: expected declaration before ‘}’ token
}
^
Multiple libraries were found for «ESP8266WiFi.h»
Used: C:UsersprjadAppDataLocalArduino15packagesesp8266hardwareesp82662.5.2librariesESP8266WiFi
Multiple libraries were found for «Adafruit_MQTT.h»
Used: C:UsersprjadDocumentsArduinolibrariesAdafruit_MQTT_Library
Not used: C:UsersprjadDocumentsArduinolibrariesAdafruit_MQTT_Library-master
Multiple libraries were found for «ESP8266Ping.h»
Used: C:UsersprjadDocumentsArduinolibrariesESP8266Ping-master
Multiple libraries were found for «PubSubClient.h»
Used: C:UsersprjadDocumentsArduinolibrariespubsubclient-master
Not used: C:UsersprjadDocumentsArduinolibrariesPubSubClient-2.7.0
exit status 1
‘mqtt’ does not name a type
This report would have more information with
«Show verbose output during compilation»
option enabled in File -> Preferences.
Code M using
/***************************************************
Adafruit MQTT Library ESP8266 Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit’s Huzzah ESP board & Feather
—-> https://www.adafruit.com/product/2471
—-> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include «Adafruit_MQTT.h»
#include «Adafruit_MQTT_Client.h»
#include <ESP8266Ping.h>
#include <PubSubClient.h>
/************************* WiFi Access Point *********************************/
#define WLAN_SSID «xxxxxxxxx»
#define WLAN_PASS «xxxxxxxxx»
#define REMOTE_HOST «www.google.com»
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER «io.adafruit.com»
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME «xxxxxx»
#define AIO_KEY «xxxxxxx»
/************ Global State (you don’t need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or… use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup a feed called ‘photocell’ for publishing.
// Notice MQTT paths for AIO follow the form: /feeds/
//Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME «/feeds/photocell»);
// Setup a feed called ‘onoff’ for subscribing to changes.
Adafruit_MQTT_Subscribe R1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME «/feeds/R1»);
Adafruit_MQTT_Subscribe R2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME «/feeds/R2»);
Adafruit_MQTT_Subscribe R3 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME «/feeds/R3»);
Adafruit_MQTT_Subscribe R4 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME «/feeds/R4»);
Adafruit_MQTT_Subscribe R5 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME «/feeds/R5»);
Adafruit_MQTT_Subscribe R6 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME «/feeds/R6»);
Adafruit_MQTT_Subscribe R7 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME «/feeds/R7»);
Adafruit_MQTT_Subscribe R8 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME «/feeds/R8»);
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
pinMode(14, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(15, OUTPUT);
Serial.println(F(«Adafruit MQTT demo»));
// Connect to WiFi access point.
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println(«Connecting to WiFi»);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(«.»);
}
Serial.println();
Serial.print(«WiFi connected with ip «);
Serial.println(WiFi.localIP());
Serial.print(«Pinging host «);
Serial.println(REMOTE_HOST);
if(Ping.ping(REMOTE_HOST)) {
Serial.println(«Success!!»);
} else {
Serial.println(«Error :(«);
}
}
// Setup MQTT subscription for onoff feed.
mqtt.subscribe(&R1);
mqtt.subscribe(&R2);
mqtt.subscribe(&R3);
mqtt.subscribe(&R4);
mqtt.subscribe(&R5);
mqtt.subscribe(&R6);
mqtt.subscribe(&R7);
mqtt.subscribe(&R8);
}
uint32_t x=0;
void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our ‘wait for incoming subscription packets’ busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &R1) {
Serial.print(F(«Got_R1: «));
Serial.println((char *)R1.lastread);
int num = atoi((char *)R1.lastread);
digitalWrite (5,num);
}
if (subscription == &R2) {
Serial.print(F("Got_R2: "));
Serial.println((char *)R2.lastread);
int num = atoi((char *)R2.lastread);
digitalWrite (4,num);
}
if (subscription == &R3) {
Serial.print(F("Got_R3: "));
Serial.println((char *)R3.lastread);
int num = atoi((char *)R3.lastread);
digitalWrite (0,num);
}
if (subscription == &R4) {
Serial.print(F("Got_R4: "));
Serial.println((char *)R4.lastread);
int num = atoi((char *)R4.lastread);
digitalWrite (2,num);
}
if (subscription == &R5) {
Serial.print(F("Got_R5: "));
Serial.println((char *)R5.lastread);
int num = atoi((char *)R5.lastread);
digitalWrite (14,num);
}
if (subscription == &R6) {
Serial.print(F("Got_R6: "));
Serial.println((char *)R6.lastread);
int num = atoi((char *)R6.lastread);
digitalWrite (12,num);
}
if (subscription == &R7) {
Serial.print(F("Got_R7: "));
Serial.println((char *)R7.lastread);
int num = atoi((char *)R7.lastread);
digitalWrite (13,num);
}
if (subscription == &R8) {
Serial.print(F("Got_R8: "));
Serial.println((char *)R8.lastread);
int num = atoi((char *)R8.lastread);
digitalWrite (15,num);
}
}
// Now we can publish stuff! — If you want publishing use the following code
// Serial.print(F(«nSending photocell val «));
// Serial.print(x);
//Serial.print(«…»);
// if (! photocell.publish(x++)) {
// Serial.println(F(«Failed»));
// } else {
// Serial.println(F(«OK!»));
// }
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t retries=3;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print(«Connecting to MQTT… «);
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println(«Retrying MQTT connection in 5 seconds…»);
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries—;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println(«MQTT Connected!»);
}
Serial does not name a type error
— Thu Mar 03, 2016 10:08 pm
#188511
When this sketch is executed this error results. What needs to be accomplished with this code is for 8 voltage readings from the 74HC4015 to be displayed on the serial monitor. I’m new to programming.
Thank you
AnalogMultiplexing:43: error: ‘Serial’ does not name a type
Serial.print(«Raw Value = » );
AnalogMultiplexing:44: error: ‘Serial’ does not name a type
Serial.print(RawValue);
^
exit status 1
‘Serial’ does not name a type
Code: Select all
int pin_Out_S0 = 8;
int pin_Out_S1 = 9;
int pin_Out_S2 = 10;
int pin_In_Mux1 = A0;
int Mux1_State[8] = {0};
//float Mux1_State[i] =0;
int RawValue=0;
float Voltage = 0;
void setup() {
pinMode(pin_Out_S0, OUTPUT);
pinMode(pin_Out_S1, OUTPUT);
pinMode(pin_Out_S2, OUTPUT);
pinMode(pin_In_Mux1, INPUT);
Serial.begin(9600);
}
void loop() {
RawValue = analogRead(pin_In_Mux1);
Voltage = (RawValue * 5.0) / 1024; //scale the ADC
updateMux1();
//Serial.println(Mux1_State);
for(int i = 0; i < 8; i ++) {
if(i == 7) {
Serial.println(Mux1_State[i]);
} else {
Serial.print(Mux1_State[i]);
Serial.print(",");
// vout = (value * 5.0) / 1024.0;
}
}
}
void updateMux1 () {
for (int i = 0; i < 8; i++){
digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
Mux1_State[i] = analogRead(pin_In_Mux1);
}
}
Serial.print("Raw Value = " );
Serial.print(RawValue);
— Fri Mar 04, 2016 9:38 am
#188513
That’s because it’s outside of Loop().
Code: Select all
int pin_Out_S0 = 8;
int pin_Out_S1 = 9;
int pin_Out_S2 = 10;
int pin_In_Mux1 = A0;
int Mux1_State[8] = {0};
//float Mux1_State[i] =0;
int RawValue=0;
float Voltage = 0;
void setup() {
pinMode(pin_Out_S0, OUTPUT);
pinMode(pin_Out_S1, OUTPUT);
pinMode(pin_Out_S2, OUTPUT);
pinMode(pin_In_Mux1, INPUT);
Serial.begin(9600);
}
void loop() {
RawValue = analogRead(pin_In_Mux1);
Voltage = (RawValue * 5.0) / 1024; //scale the ADC
updateMux1();
//Serial.println(Mux1_State);
for(int i = 0; i < 8; i ++) {
if(i == 7) {
Serial.println(Mux1_State[i]);
} else {
Serial.print(Mux1_State[i]);
Serial.print(",");
// vout = (value * 5.0) / 1024.0;
}
}
Serial.print("Raw Value = " );
Serial.print(RawValue);
}
void updateMux1 () {
for (int i = 0; i < 8; i++){
digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
Mux1_State[i] = analogRead(pin_In_Mux1);
}
}
————————————————————————————————————————————————————
//codlink
Available for circuit and PCB design, just PM me!