Go Down

Topic: Need help with two errors (Read 516 times) previous topic - next topic

ve3joc

I managed to eliminate a number of errors in my code but I have two left.  I have been working on them for a while now but I just can't figure them out.

Here are the errors:

Code: [Select]
E:\My 60 Pixel Ring Clock Version 3\My_60_Pixel_Ring_Clock_Version_3\My_60_Pixel_Ring_Clock_Version_3.ino: In function 'void loop()':

My_60_Pixel_Ring_Clock_Version_3:129:71: error: 'getTimeDate' was not declared in this scope

   getTimeDate(yearval, monthval, dayval, hourval, minuteval, secondval) {

                                                                       ^

My_60_Pixel_Ring_Clock_Version_3:129:73: error: expected ';' before '{' token

   getTimeDate(yearval, monthval, dayval, hourval, minuteval, secondval) {

                                                                         ^

exit status 1
'getTimeDate' was not declared in this scope



Here is the code:

Code: [Select]


//My 60 Pixel Ring Clock Version 3
//March 15, 2019 9:23p.m.
//#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFi.h>
#include <WiFiUdp.h>

#define PIN 4
#define PIXEL 60
#define getNTPtime

int LED = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL, PIN, NEO_GRB + NEO_KHZ800);

int hourval, minuteval, secondval, yearval, monthval, dayval;

//Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;

//Assign SSID and Password to a constant
const char* ssid = "mySSID";  //  your network SSID (name)
const char* password = "myPassword";    // your network password

//Define NTP client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

void setup() {
  //Initialize the serial monitor
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  //Initialize an NTPClient to get time
  timeClient.begin();
  //Set offset time in seconds to adjust for current time zone
  timeClient.setTimeOffset(18000);
  while (timeClient.update()) {
    timeClient.forceUpdate();
  }

  //Call the function to get time and date
  getTimeDate();

  //Used to verify values of variables holding time and date.
  //Comment out before uploading permanently
  Serial.print("The year is: ");
  Serial.println(yearval);
  Serial.print("Month is: ");
  Serial.println(monthval);
  Serial.print("Day is: ");
  Serial.println(dayval);
  Serial.println("");
  Serial.print("The hour is: ");
  Serial.println(hourval);
  Serial.print("Minute is: ");
  Serial.println(minuteval);
  Serial.print("Second is: ");
  Serial.println(secondval);

  strip.begin();
  strip.show();    // Initialize all pixels to 'off'
  strip.setBrightness(20);
}

void loop() {

  switch (hourval) {     //Synchronize the LEDs for 12 hour clock.
    case 1: case 13:
      LED = 5;
      break;
    case 2: case 14:
      LED = 10;
      break;
    case 3: case 15:
      LED = 15;
      break;
    case 4: case 16:
      LED = 20;
      break;
    case 5: case 17:
      LED = 25;
      break;
    case 6: case 18:
      LED = 30;
      break;
    case 7: case 19:
      LED = 35;
      break;
    case 8: case 20:
      LED = 40;
      break;
    case 9: case 21:
      LED = 45;
      break;
    case 10: case 22:
      LED = 50;
      break;
    case 11: case 23:
      LED = 55;
      break;
    case 12: case 0:
      LED = 0;
      break;
  }

  strip.setPixelColor(minuteval, 0x008000);      // Turn on LED for minutes.
  strip.setPixelColor(secondval, 0x0000ff);      // Turn on LED for seconds.
  strip.setPixelColor(LED, 0xff0000);            // Turn on LED for hours.
  strip.show();

  strip.setPixelColor(LED, 0x000000);            // Turn off LED for minutes.
  strip.setPixelColor(minuteval, 0x000000);      // Turn off LED for seconds.
  strip.setPixelColor(secondval, 0x000000);      // Turn off LED for hours.

  //Function to separate time and date from formattedDate string and assign to variables
  getTimeDate(yearval, monthval, dayval, hourval, minuteval, secondval) {

    /*The formattedDate comes with the following format:
      2019-03-28T16:00:13Z
      We need to extract the date and time
    */
    formattedDate = timeClient.getFormattedDate();

    //Extract date
    int splitT = formattedDate.indexOf("T");
    dayStamp = formattedDate.substring(0, splitT);

    //Extract time
    timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1);

    char theyear = dayStamp.substring(0, 4);
    char themonth = dayStamp.substring(5, 7);
    char theday = dayStamp.substring(9, 11);
    char thehour = timeStamp(0, 2);
    char theminute = timeStamp.substring(3, 5);
    char thesecond = timeStamp.substring(6, 8);


    //Convert string to integer and assign date to variables theyear, themonth, theday
    yearval = theyear.toint();
    monthval = themonth.toint();
    dayval = theday.toint();

    ///Convert string to integer and assign time to variables hourval, minuteval, secondval
    hourval = thehour.toint();
    minuteval = theminute.toint();
    secondval = thesecond.toint();
  }
}






pert

It appears that you're attempting to define the getTimeDate function inside of the loop function. You can't do that. You need to move the getTimeDate function definition outside of loop and also specify the return type of void:
Code: [Select]
void getTimeDate(yearval, monthval, dayval, hourval, minuteval, secondval) {

More information:
http://www.cplusplus.com/doc/tutorial/functions/

UKHeliBob

Auto Format your code in the IDE

I can't actually see where the getTimeDate() function is defined in the code.

If this is meant to be it

Code: [Select]
//Function to separate time and date from formattedDate string and assign to variables
  getTimeDate(yearval, monthval, dayval, hourval, minuteval, secondval)
  {
etc

then it has no return type and the parameters do not have data types specified.  Mind you, as they are global variables they don't need to be passed to the function anyway.
Moreover, after the Auto Format the function definition should start on the left margin and it doesn't.  This strongly suggests that there is at least one } missing somewhere before there or that those lines are inside another function.  Where does loop() end ?

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ve3joc

#3
Mar 17, 2019, 04:55 pm Last Edit: Mar 17, 2019, 04:58 pm by ve3joc
Thank you.  I moved the function to the bottom of the code, outside of the loop function and I added return type 'void' to it.  Now I get these errors:

Code: [Select]


My_60_Pixel_Ring_Clock_Version_3:125:18: error: variable or field 'getTimeDate' declared void

   void getTimeDate(yearval, monthval, dayval, hourval, minuteval, secondval) {

                  ^

E:\My 60 Pixel Ring Clock Version 3\My_60_Pixel_Ring_Clock_Version_3\My_60_Pixel_Ring_Clock_Version_3.ino: In function 'void setup()':

My_60_Pixel_Ring_Clock_Version_3:50:15: error: 'getTimeDate' was not declared in this scope

   getTimeDate();

               ^

E:\My 60 Pixel Ring Clock Version 3\My_60_Pixel_Ring_Clock_Version_3\My_60_Pixel_Ring_Clock_Version_3.ino: At global scope:

My_60_Pixel_Ring_Clock_Version_3:125:20: error: variable or field 'getTimeDate' declared void

   void getTimeDate(yearval, monthval, dayval, hourval, minuteval, secondval) {

                    ^

exit status 1
variable or field 'getTimeDate' declared void



Here is the revised code:

Code: [Select]


//My 60 Pixel Ring Clock Version 3
//March 15, 2019 9:23p.m.
//#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFi.h>
#include <WiFiUdp.h>

#define PIN 4
#define PIXEL 60
#define getNTPtime

int LED = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL, PIN, NEO_GRB + NEO_KHZ800);

int hourval, minuteval, secondval, yearval, monthval, dayval;

//Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;

//Assign SSID and Password to a constant
const char* ssid = "jcoones";  //  your network SSID (name)
const char* password = "VE3JOCb/a!";    // your network password

//Define NTP client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

void setup() {
  //Initialize the serial monitor
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  //Initialize an NTPClient to get time
  timeClient.begin();
  //Set offset time in seconds to adjust for current time zone
  timeClient.setTimeOffset(18000);
  while (timeClient.update()) {
    timeClient.forceUpdate();
  }

  //Call the function to get time and date
  getTimeDate();

  //Used to verify values of variables holding time and date.
  //Comment out before uploading permanently
  Serial.print("The year is: ");
  Serial.println(yearval);
  Serial.print("Month is: ");
  Serial.println(monthval);
  Serial.print("Day is: ");
  Serial.println(dayval);
  Serial.println("");
  Serial.print("The hour is: ");
  Serial.println(hourval);
  Serial.print("Minute is: ");
  Serial.println(minuteval);
  Serial.print("Second is: ");
  Serial.println(secondval);

  strip.begin();
  strip.show();    // Initialize all pixels to 'off'
  strip.setBrightness(20);
}

void loop() {

  switch (hourval) {     //Synchronize the LEDs for 12 hour clock.
    case 1: case 13:
      LED = 5;
      break;
    case 2: case 14:
      LED = 10;
      break;
    case 3: case 15:
      LED = 15;
      break;
    case 4: case 16:
      LED = 20;
      break;
    case 5: case 17:
      LED = 25;
      break;
    case 6: case 18:
      LED = 30;
      break;
    case 7: case 19:
      LED = 35;
      break;
    case 8: case 20:
      LED = 40;
      break;
    case 9: case 21:
      LED = 45;
      break;
    case 10: case 22:
      LED = 50;
      break;
    case 11: case 23:
      LED = 55;
      break;
    case 12: case 0:
      LED = 0;
      break;
  }

  strip.setPixelColor(minuteval, 0x008000);      // Turn on LED for minutes.
  strip.setPixelColor(secondval, 0x0000ff);      // Turn on LED for seconds.
  strip.setPixelColor(LED, 0xff0000);            // Turn on LED for hours.
  strip.show();

  strip.setPixelColor(LED, 0x000000);            // Turn off LED for minutes.
  strip.setPixelColor(minuteval, 0x000000);      // Turn off LED for seconds.
  strip.setPixelColor(secondval, 0x000000);      // Turn off LED for hours.
}

//Function to separate time and date from formattedDate string and assign to variables
void getTimeDate(yearval, monthval, dayval, hourval, minuteval, secondval) {

  /*The formattedDate comes with the following format:
    2019-03-28T16:00:13Z
    We need to extract the date and time
  */
  formattedDate = timeClient.getFormattedDate();

  //Extract date
  int splitT = formattedDate.indexOf("T");
  dayStamp = formattedDate.substring(0, splitT);

  //Extract time
  timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1);

  char theyear = dayStamp.substring(0, 4);
  char themonth = dayStamp.substring(5, 7);
  char theday = dayStamp.substring(9, 11);
  char thehour = timeStamp(0, 2);
  char theminute = timeStamp.substring(3, 5);
  char thesecond = timeStamp.substring(6, 8);


  //Convert string to integer and assign date to variables theyear, themonth, theday
  yearval = theyear.toint();
  monthval = themonth.toint();
  dayval = theday.toint();

  ///Convert string to integer and assign time to variables hourval, minuteval, secondval
  hourval = thehour.toint();
  minuteval = theminute.toint();
  secondval = thesecond.toint();
}



AWOL

#4
Mar 17, 2019, 05:16 pm Last Edit: Mar 17, 2019, 05:17 pm by AWOL
Code: [Select]
void getTimeDate(yearval, monthval, dayval, hourval, minuteval, secondval) {

You're still missing the parameter types

Code: [Select]
getTimeDate();...and not using them

ve3joc

The parameters I used were global variables, as pointed out previously,  so I guess I don't need to pass them so I removed them.
Now I am getting these errors:

Code: [Select]


My_60_Pixel_Ring_Clock_Version_3:140:41: error: cannot convert 'String' to 'char' in initialization

   char theyear = dayStamp.substring(0, 4);

                                         ^

My_60_Pixel_Ring_Clock_Version_3:141:42: error: cannot convert 'String' to 'char' in initialization

   char themonth = dayStamp.substring(5, 7);

                                          ^

My_60_Pixel_Ring_Clock_Version_3:142:41: error: cannot convert 'String' to 'char' in initialization

   char theday = dayStamp.substring(9, 11);

                                         ^

My_60_Pixel_Ring_Clock_Version_3:143:32: error: no match for call to '(String) (int, int)'

   char thehour = timeStamp(0, 2);

                                ^

My_60_Pixel_Ring_Clock_Version_3:144:44: error: cannot convert 'String' to 'char' in initialization

   char theminute = timeStamp.substring(3, 5);

                                            ^

My_60_Pixel_Ring_Clock_Version_3:145:44: error: cannot convert 'String' to 'char' in initialization

   char thesecond = timeStamp.substring(6, 8);

                                            ^

My_60_Pixel_Ring_Clock_Version_3:149:21: error: request for member 'toint' in 'theyear', which is of non-class type 'char'

   yearval = theyear.toint();

                     ^

My_60_Pixel_Ring_Clock_Version_3:150:23: error: request for member 'toint' in 'themonth', which is of non-class type 'char'

   monthval = themonth.toint();

                       ^

My_60_Pixel_Ring_Clock_Version_3:151:19: error: request for member 'toint' in 'theday', which is of non-class type 'char'

   dayval = theday.toint();

                   ^

My_60_Pixel_Ring_Clock_Version_3:154:21: error: request for member 'toint' in 'thehour', which is of non-class type 'char'

   hourval = thehour.toint();

                     ^

My_60_Pixel_Ring_Clock_Version_3:155:25: error: request for member 'toint' in 'theminute', which is of non-class type 'char'

   minuteval = theminute.toint();

                         ^

My_60_Pixel_Ring_Clock_Version_3:156:25: error: request for member 'toint' in 'thesecond', which is of non-class type 'char'

   secondval = thesecond.toint();

                         ^

exit status 1
cannot convert 'String' to 'char' in initialization




I would guess that I am not using the proper way to convert the string to an integer.  The formatted date and time comes in a string like 2019-03-28T16:00:13Z.  What I am trying to do is to separate the year, month, day, hour, minute and second using substrings and then converting each one to an integer.




UKHeliBob

If you insist on using Strings take a look at toInt function
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MorganS

The sub string from 0 to 4 has 5 characters. You can't put that into a single character. Maybe you want to add the .toint() method to convert to an integer?

While months and days will fit into chars (bytes), years won't. Choose a bigger integer type.
"The problem is in the code you didn't post."

ve3joc

I had changed the char to String and I was using the toint() to try to convert the substrings to an integer and I am getting these errors:

Code: [Select]


: error: 'class String' has no member named 'toint'

   yearval = theyear.toint();




I get the same error for themonth, theday, thehour, theminute and thesecond.

Here is my current code:

Code: [Select]


//My 60 Pixel Ring Clock Version 3
//March 15, 2019 9:23p.m.
//#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFi.h>
#include <WiFiUdp.h>

#define PIN 4
#define PIXEL 60
#define getNTPtime

int LED = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL, PIN, NEO_GRB + NEO_KHZ800);

int hourval, minuteval, secondval, yearval, monthval, dayval;

//Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;

//Assign SSID and Password to a constant
const char* ssid = "mySSID";                 //  your network SSID (name)
const char* password = "myPassword";    // your network password

//Define NTP client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

void setup() {
  //Initialize the serial monitor
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  //Initialize an NTPClient to get time
  timeClient.begin();
  //Set offset time in seconds to adjust for current time zone
  timeClient.setTimeOffset(18000);
  while (timeClient.update()) {
    timeClient.forceUpdate();
  }

  //Call the function to get time and date
  getTimeDate();

  //Used to verify values of variables holding time and date.
  //Comment out before uploading permanently
  Serial.print("The year is: ");
  Serial.println(yearval);
  Serial.print("Month is: ");
  Serial.println(monthval);
  Serial.print("Day is: ");
  Serial.println(dayval);
  Serial.println("");
  Serial.print("The hour is: ");
  Serial.println(hourval);
  Serial.print("Minute is: ");
  Serial.println(minuteval);
  Serial.print("Second is: ");
  Serial.println(secondval);

  strip.begin();
  strip.show();    // Initialize all pixels to 'off'
  strip.setBrightness(20);
}

void loop() {

  switch (hourval) {     //Synchronize the LEDs for 12 hour clock.
    case 1: case 13:
      LED = 5;
      break;
    case 2: case 14:
      LED = 10;
      break;
    case 3: case 15:
      LED = 15;
      break;
    case 4: case 16:
      LED = 20;
      break;
    case 5: case 17:
      LED = 25;
      break;
    case 6: case 18:
      LED = 30;
      break;
    case 7: case 19:
      LED = 35;
      break;
    case 8: case 20:
      LED = 40;
      break;
    case 9: case 21:
      LED = 45;
      break;
    case 10: case 22:
      LED = 50;
      break;
    case 11: case 23:
      LED = 55;
      break;
    case 12: case 0:
      LED = 0;
      break;
  }

  strip.setPixelColor(minuteval, 0x008000);      // Turn on LED for minutes.
  strip.setPixelColor(secondval, 0x0000ff);      // Turn on LED for seconds.
  strip.setPixelColor(LED, 0xff0000);            // Turn on LED for hours.
  strip.show();

  strip.setPixelColor(LED, 0x000000);            // Turn off LED for minutes.
  strip.setPixelColor(minuteval, 0x000000);      // Turn off LED for seconds.
  strip.setPixelColor(secondval, 0x000000);      // Turn off LED for hours.
}

//Function to separate time and date from formattedDate string and assign to variables
void getTimeDate() {

  /*The formattedDate comes with the following format:
    2019-03-28T16:00:13Z
    We need to extract the date and time
  */
  formattedDate = timeClient.getFormattedDate();

  //Extract date
  int splitT = formattedDate.indexOf("T");
  dayStamp = formattedDate.substring(0, splitT);

  //Extract time
  timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1);

  String theyear = dayStamp.substring(0, 3);
  String themonth = dayStamp.substring(5, 6);
  String theday = dayStamp.substring(8, 9);
  String thehour = timeStamp.substring(0, 1);
  String theminute = timeStamp.substring(3, 4);
  String thesecond = timeStamp.substring(6, 7);

  //Convert string to integer and assign date to variables theyear, themonth, theday
  yearval = theyear.toint();
  monthval = themonth.toint();
  dayval = theday.toint();

  ///Convert string to integer and assign time to variables hourval, minuteval, secondval
  hourval = thehour.toint();
  minuteval = theminute.toint();
  secondval = thesecond.toint();
}


MorganS

"The problem is in the code you didn't post."

ve3joc

I changed the 'i' in toInt() to a capital and now I am getting a whole whack of new errors.  I think it is something to do with the libraries.

I had to attach the erros this time because it is too big for the editor here.

Seems like I'll never get this thing to compile.  Sometimes I think I should start over on this project but as a beginner I don't know what I would do differently.



MorganS

When I get tied up like this, I go back to the simplest example. Just blink a single LED.

Then get other related examples working. Get the Neopixels blinking or something. Get a simple WiFi request out. Get a NTP response back...
"The problem is in the code you didn't post."

RayLivingston

#12
Mar 18, 2019, 04:45 pm Last Edit: Mar 18, 2019, 04:46 pm by RayLivingston
I changed the 'i' in toInt() to a capital and now I am getting a whole whack of new errors.  I think it is something to do with the libraries.

I had to attach the erros this time because it is too big for the editor here.

Seems like I'll never get this thing to compile.  Sometimes I think I should start over on this project but as a beginner I don't know what I would do differently.
With c compilers, it is often not useful to look beyond the first few errors.  The first error will often cause the compiler to get "confused", and create many spurious following errors.  So, focus on fixing the first error or two, then re-compile.

Regards,
Ray L.

ve3joc

Thanks Morgan S.  I already had the neopixel clock working perfectly but I was using a RTC module. I wanted to add the NTP server to automatically set the time and eliminate the RTC module and the need to manually set the time.

And thanks to RayLivingston.  The problem is for a beginner in Arduino and no knowledge of the C language, most of these error messages are cryptic  to me and hard to fix if you don't know what they mean or what is causing them.



pert

Here's the cause of the errors:
Code: [Select]

#include <WiFi.h>

The ESP8266 equivalent of the WiFi library is called ESP8266WiFi, which you already have an #include directive for. When you added the #include directive for WiFi.h, it causes the WiFi library included with the Arduino IDE to be compiled. That library is for the discontinued Arduino WiFi Shield, which is completely different than your ESP8266.

I find it quite curious that you added that line to your code, since you didn't have it in the last thread. You need to learn to work very methodically if you want to have success with programming and electronics. If you do decide to experiment by making random changes to your sketch, you should first make sure that the code compiles and works, and then immediately after adding making only a single change, test again to determine what the effects of it were.

Go Up