By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

421:1: error: expected unqualified-id before '{' token Error c

P: 13
Hi I am trying to add the DS1307 to my project via I2C using both a keypad and LCD. So far I have been able to get my project working with a passcode entry, led, keypad input and LCD display. I as soon as I insert the DS1307 code it doesn't want to compile because its expecting an unqualified ID. Even though in the original code there was nothing else before '{'.

I am a begineer to Arduino and programming I am learning fast but hitting a lot of brick walls because of out-dated codes.

I am not quite sure either whether the DS1307 clock code is what I am after. I have added it to display the time in certain places within the future menu and to eventually be used to log times of sensor readings etc. if I ever make it that far.

I also started experimenting with different menu codes with no luck. but at the moment Im not terribly concerned with that, as I think it is more important to get the time and date displayed first. I have attached a hierarchy menu layout tree to see the menu and sub-menu layout process I am after. I am basically wanting use the numbers on the keypad a the selection inputs to take you to the applicable submenus etc.

I have also been experimenting with different sensors which I had significantly more luck implementing, before I got all excited and went out and bought an LCD.

The clock code also compiles by itself, but i can't tell what its doing as there is no LCD code with in.

Any help would be greatly appreciated.

Below is the code and error message:

Error Message:


MENU_CONTROL_CLOCK.ino:421:1: error: expected unqualified-id before '{' token
Error compiling.



Expand|Select|Wrap|Line Numbers
  1. /*
  2. ////////////////////////////////////////////////////////////////////////////////////////////////////MENU_CONTROLLER_UNO /////////////////////////////////////////////////////////////
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////MENU_CONTROLLER_UNO /////////////////////////////////////////////////////////////
  4. ////////////////////////////////////////////////////////////////////////////////////////////////////MENU_CONTROLLER_UNO /////////////////////////////////////////////////////////////
  5. LCD I2C &: Keypad Code: Mike Myers (http://mikemyers.me)  @netnutmike - Let's Make It Episode 6 
  6.            (http://tech-zen.tv/index.php/shows/let-s-make-it/episodes/59-sensor-fun-with-arduino-1-massive-failure-but-4-successes-let-s-make-it-episode-6)
  7. Menu Code: Alexander Brevig (alexanderbrevig@gmail.com) - Menu Library for Arduino
  8.            (http://playground.arduino.cc/Code/Menu)
  9. RTC DS1307: ARDUINO - (http://playground.arduino.cc/code/time)
  10. I2C LCD & RTC DS1307: brainstorm ABC (brainstorm.abc@gmail.com) - Clock using Arduino I2C bus for both RTC and 16x2 LCD display
  11.            (https://www.youtube.com/watch?v=kukV4xrqRl4&feature=share)
  12. Arduino Real Time Clock (DS1307) -  Naren_Murali   
  13.            (http://www.instructables.com/id/Arduino-Real-Time-Clock-DS1307/?ALLSTEPS)
  14.  
  15.  
  16.  
  17.  
  18.  
  19. */
  20. //*-----( Import needed libraries )-----*//
  21. #include <LiquidCrystal_I2C.h>
  22. #include <Wire.h> 
  23. #include <Keypad.h>
  24. #include <Time.h>
  25. #include <RealTimeClockDS1307.h>
  26.  
  27.  
  28. //*-----( Declare Constants )-----*//
  29. const byte rows = 4;
  30. const byte cols = 4;
  31. int redLEDpin = 4;
  32. int greenLEDpin = 5;
  33. int l;
  34.  
  35. //*-----( Declare objects )-----*//
  36.  
  37. // *-----( Declare Variables )-----*//
  38.  
  39.  
  40. /*
  41. ///////////////////////////////////////////////////////////////////////////////////////////////////////REAL-TIME CLOCK MODULE DS1307
  42. */
  43.  
  44. #define Display_Clock_Every_N_Seconds 10           // n.secs to show date/time
  45. #define Display_ShortHelp_Every_N_Seconds 60       // n.secs to show hint for help
  46. //#define TEST_Squarewave
  47. //#define TEST_StopStart
  48. //#define TEST_1224Switch
  49.  
  50. int count=0;
  51. char formatted[] = "00-00-00 00:00:00x";
  52.  
  53.  
  54.  
  55. /*
  56. ///////////////////////////////////////////////////////////////////////////////////////////////////////LIQUID CRYSTAL IC2 DISPLAY
  57. */
  58. #define address 0x27
  59.  
  60. /*
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////////MENU & SUB-MENU
  62. */
  63.  
  64. /* 
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////////4X4 MATRIX KEYPAD
  66. */
  67. char keys[rows][cols] = {
  68.   {'1','2','3','A'},
  69.   {'4','5','6','B'},
  70.   {'7','8','9','C'},
  71.   {'*','0','#','D'}
  72. };
  73.  
  74. byte rowPins[rows] = {13,11,12,10};
  75. byte colPins[cols] = {9,8,7,6};
  76.  
  77. Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
  78.  
  79. LiquidCrystal_I2C lcd(0x27,20,4);              // set the LCD address to 0x27 for a 20 chars and 4 line display
  80.  
  81.  
  82. char* ourCode = "3308";                        //set our code
  83. int currentPosition = 0;
  84. char* Select1 = "1";
  85.  
  86. /*
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VOID SETUP///////////////////////
  88. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VOID SETUP///////////////////////
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VOID SETUP///////////////////////
  90. */
  91.  
  92. void setup()                                    // Write setup function
  93. {
  94. /*  
  95. /////////////////////////////////////////////// REAL-TIME CLOCK MODULE SETUP ///////////////////////////////////////////////
  96. */
  97. {
  98. //  Wire.begin();
  99.   Serial.begin(9600);
  100.     pinMode(A4, OUTPUT);     //*** pin 16 (Analog pin 2) as OUTPUT   ***
  101.   digitalWrite(A4, HIGH);   //*** pin 16 (Analog pin 2) set to LOW  ***
  102.   pinMode(A5, OUTPUT);     //*** pin 17 (Analog pin 3) as OUTPUT   ***
  103.   digitalWrite(A5, LOW);  //*** pin 17 (Analog pin 3) set to HIGH ***
  104.   //*** Analog Pin settings to power RTC module ***
  105. }
  106.  
  107.  
  108.  
  109. /*  
  110. //////////////////////////////////////////////// LIQUID CRYSTAL IC2 SETUP ////////////////////////////////////////////////
  111. */
  112.  
  113.   lcd.init();                                  // initialize the lcd 
  114.   lcd.init();                                  // Print a message to the LCD.
  115.   lcd.backlight();  
  116.  
  117.  
  118.   displayCodeEntryScreen();
  119.  
  120.  
  121.   pinMode(redLEDpin, OUTPUT);                      //setup and turn off both LEDs
  122.   pinMode(greenLEDpin, OUTPUT);
  123.   digitalWrite(redLEDpin, LOW);
  124.   digitalWrite(greenLEDpin, LOW);
  125.  
  126.  
  127. /*   
  128. //////////////////////////////////////////////// MENU & SUB-MENU SETUP ////////////////////////////////////////////////
  129. */
  130.  
  131.  
  132.  
  133. }
  134. /*
  135. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VOID LOOP////////////////////////
  136. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VOID LOOP///////////////////////
  137. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VOID LOOP///////////////////////
  138. */
  139.  
  140. void loop()
  141. /*  
  142. ////////////////////////////////////////////// REAL-TIME CLOCK MODULE LOOP ///////////////////////////////////////////////
  143. */
  144.  {
  145.    if(Serial.available())
  146.   {
  147.     processCommand();
  148.   }
  149.  
  150.   RTC.readClock();
  151.   count++;
  152.   if(count % Display_Clock_Every_N_Seconds == 0){
  153.     Serial.print(count);
  154.     Serial.print(": ");
  155.     RTC.getFormatted(formatted);
  156.     Serial.print(formatted);
  157.     Serial.println();
  158.   }
  159.  
  160.   if(count % Display_ShortHelp_Every_N_Seconds == 0) {
  161.     Serial.println("Send ? for a list of commands.");
  162.   }
  163. #ifdef TEST_Squarewave
  164. if(count%10 == 0)
  165. {
  166.   switch(count/10 % 6)
  167.   {
  168.     case 0:
  169.     Serial.print("Squarewave disabled (low impedance): ");
  170.     RTC.sqwDisable(0);
  171.     Serial.println((int) RTC.readData(7));
  172.     break;
  173.     case 1:
  174.     Serial.print("Squarewave disabled (high impedance): ");
  175.     RTC.sqwDisable(1);
  176.     Serial.println((int) RTC.readData(7));
  177.     break;
  178.     case 2:
  179.     Serial.println("Squarewave enabled at 1 Hz");
  180.     RTC.sqwEnable(RTC.SQW_1Hz);
  181.     break;
  182.     case 3:
  183.     Serial.println("Squarewave enabled at 4.096 kHz");
  184.     RTC.sqwEnable(RTC.SQW_4kHz);
  185.     break;
  186.     case 4:
  187.     Serial.println("Squarewave enabled at 8.192 kHz");
  188.     RTC.sqwEnable(RTC.SQW_8kHz);
  189.     break;
  190.     case 5:
  191.     Serial.println("Squarewave enabled at 32.768 kHz");
  192.     RTC.sqwEnable(RTC.SQW_32kHz);
  193.     break;
  194.     default:
  195.     Serial.println("Squarewave test not defined");
  196.   }//switch
  197. }
  198. #endif
  199.  
  200. #ifdef TEST_StopStart
  201. if(count%10 == 0)
  202. {
  203.   if(!RTC.isStopped()) 
  204.   {
  205.     if(RTC.getSeconds() < 45) 
  206.     {
  207.       Serial.println("Stopping clock for 10 seconds");
  208.       RTC.stop();
  209.     }//if we have enough time
  210.   } else {
  211.     RTC.setSeconds(RTC.getSeconds()+11);
  212.     RTC.start();
  213.     Serial.println("Adding 11 seconds and restarting clock");
  214.   }
  215. }//if on a multiple of 10 counts
  216. #endif
  217.  
  218. #ifdef TEST_1224Switch  
  219.   if(count%10 == 0)
  220.   {
  221.     if(count %20 == 0)
  222.     {
  223.       Serial.println("switching to 12-hour time");
  224.       RTC.switchTo12h();
  225.       RTC.setClock();
  226.     }
  227.     else
  228.     {
  229.       Serial.println("switching to 24-hour time");
  230.       RTC.switchTo24h();
  231.       RTC.setClock();
  232.     }
  233.   }
  234. #endif
  235. }
  236.  
  237. void processCommand() {
  238.   if(!Serial.available()) { return; }
  239.   char command = Serial.read();
  240.   int in,in2;
  241.   switch(command)
  242.   {
  243.     case 'H':
  244.     case 'h':
  245.     in=SerialReadPosInt();
  246.     RTC.setHours(in);
  247.     RTC.setClock();
  248.     Serial.print("Setting hours to ");
  249.     Serial.println(in);
  250.     break;
  251.     case 'I':
  252.     case 'i':
  253.     in=SerialReadPosInt();
  254.     RTC.setMinutes(in);
  255.     RTC.setClock();
  256.     Serial.print("Setting minutes to ");
  257.     Serial.println(in);
  258.     break;
  259.     case 'S':
  260.     case 's':
  261.     in=SerialReadPosInt();
  262.     RTC.setSeconds(in);
  263.     RTC.setClock();
  264.     Serial.print("Setting seconds to ");
  265.     Serial.println(in);
  266.     break;
  267.     case 'Y':
  268.     case 'y':
  269.     in=SerialReadPosInt();
  270.     RTC.setYear(in);
  271.     RTC.setClock();
  272.     Serial.print("Setting year to ");
  273.     Serial.println(in);
  274.     break;
  275.     case 'M':
  276.     case 'm':
  277.     in=SerialReadPosInt();
  278.     RTC.setMonth(in);
  279.     RTC.setClock();
  280.     Serial.print("Setting month to ");
  281.     Serial.println(in);
  282.     break;
  283.     case 'D':
  284.     case 'd':
  285.     in=SerialReadPosInt();
  286.     RTC.setDate(in);
  287.     RTC.setClock();
  288.     Serial.print("Setting date to ");
  289.     Serial.println(in);
  290.     break;
  291.     case 'W':
  292.     Serial.print("Day of week is ");
  293.     Serial.println((int) RTC.getDayOfWeek());
  294.     break;
  295.     case 'w':
  296.     in=SerialReadPosInt();
  297.     RTC.setDayOfWeek(in);
  298.     RTC.setClock();
  299.     Serial.print("Setting day of week to ");
  300.     Serial.println(in);
  301.     break;
  302.  
  303.     case 't':
  304.     case 'T':
  305.     if(RTC.is12hour()) {
  306.       RTC.switchTo24h();
  307.       Serial.println("Switching to 24-hour clock.");
  308.     } else {
  309.       RTC.switchTo12h();
  310.       Serial.println("Switching to 12-hour clock.");
  311.     }
  312.     RTC.setClock();
  313.     break;
  314.  
  315.     case 'A':
  316.     case 'a':
  317.     if(RTC.is12hour()) {
  318.       RTC.setAM();
  319.       RTC.setClock();
  320.       Serial.println("Set AM.");
  321.     } else {
  322.       Serial.println("(Set hours only in 24-hour mode.)");
  323.     }
  324.     break;
  325.  
  326.     case 'P':
  327.     case 'p':
  328.     if(RTC.is12hour()) {
  329.       RTC.setPM();
  330.       RTC.setClock();
  331.       Serial.println("Set PM.");
  332.     } else {
  333.       Serial.println("(Set hours only in 24-hour mode.)");
  334.     }
  335.     break;
  336.  
  337.     case 'q':
  338.     RTC.sqwEnable(RTC.SQW_1Hz);
  339.     Serial.println("Square wave output set to 1Hz");
  340.     break;
  341.     case 'Q':
  342.     RTC.sqwDisable(0);
  343.     Serial.println("Square wave output disabled (low)");
  344.     break;
  345.  
  346.     case 'z':
  347.     RTC.start();
  348.     Serial.println("Clock oscillator started.");
  349.     break;
  350.     case 'Z':
  351.     RTC.stop();
  352.     Serial.println("Clock oscillator stopped.");
  353.     break;
  354.  
  355.     case '>':
  356.     in=SerialReadPosInt();
  357.     in2=SerialReadPosInt();
  358.     RTC.writeData(in, in2);
  359.     Serial.print("Write to register ");
  360.     Serial.print(in);
  361.     Serial.print(" the value ");
  362.     Serial.println(in2);
  363.     break;    
  364.     case '<':
  365.     in=SerialReadPosInt();
  366.     in2=RTC.readData(in);
  367.     Serial.print("Read from register ");
  368.     Serial.print(in);
  369.     Serial.print(" the value ");
  370.     Serial.println(in2);
  371.     break;
  372.  
  373.     default:
  374.     Serial.println("Unknown command. Try these:");
  375.     Serial.println(" h## - set Hours [range 1..12 or 0..24]");
  376.     Serial.println(" i## - set mInutes [range 0..59]");
  377.     Serial.println(" s## - set Seconds [range 0..59]");
  378.     Serial.println(" d## - set Date [range 1..31]");
  379.     Serial.println(" m## - set Month [range 1..12]");
  380.     Serial.println(" y## - set Year [range 0..99]");
  381.     Serial.println(" w## - set arbitrary day of Week [range 1..7]");
  382.     Serial.println(" t   - toggle 24-hour mode");
  383.     Serial.println(" a   - set AM          p   - set PM");
  384.     Serial.println();
  385.     Serial.println(" z   - start clock     Z   - stop clock");
  386.     Serial.println(" q   - SQW/OUT = 1Hz   Q   - stop SQW/OUT");
  387.     Serial.println();
  388.     Serial.println(" >##,###  - write to register ## the value ###");
  389.     Serial.println(" <##      - read the value in register ##");
  390.  
  391.   }//switch on command
  392.  
  393. }
  394.  
  395. //read in numeric characters until something else
  396. //or no more data is available on serial.
  397. int SerialReadPosInt() {
  398.   int i = 0;
  399.   boolean done=false;
  400.   while(Serial.available() && !done)
  401.   {
  402.     char c = Serial.read();
  403.     if (c >= '0' && c <='9')
  404.     {
  405.       i = i * 10 + (c-'0');
  406.     }
  407.     else 
  408.     {
  409.       done = true;
  410.     }
  411.   }
  412.   return i;
  413. }
  414.  
  415.  
  416.  
  417. /*  
  418. //////////////////////////////////////////////////// KEYPAD LOOP /////////////////////////////////////////////////////
  419. */
  420.  
  421. {
  422.   int l;
  423.  
  424.   char key = keypad.getKey();
  425.  
  426.   if (int(key) != 0) {
  427.     lcd.setCursor(14,3);
  428.     lcd.print("    ");
  429.     lcd.setCursor(14,3);
  430.  
  431.     for (l=0; l<=currentPosition; ++l)
  432.     {
  433.       lcd.print("*");
  434.     }
  435.  
  436.     if (key == ourCode[currentPosition])
  437.       {
  438.         ++currentPosition;
  439.         if (currentPosition == 4)
  440.         {
  441.           unlockDoor();
  442.           currentPosition = 0;
  443.         }
  444.       } else {
  445.         invalidCode();
  446.         currentPosition = 0;
  447.       }
  448.   }
  449. }
  450.  
  451. /*  
  452. //////////////////////////////////////////// LIQUID CRYSTAL IC2 LOOP ////////////////////////////////////////////
  453. */
  454.  
  455. void displayCodeEntryScreen()
  456. {
  457.   clearScreen();
  458.   lcd.setCursor(0,0);
  459.   lcd.print("AFFINITY  AQUAPONICS"); 
  460.   lcd.setCursor(1,1);                                                      // Go to the 2nd line
  461.   lcd.print("URBAN FARM SYSTEM");   
  462.   lcd.setCursor(3,3);                                                      // Go to the 3rd line
  463.   lcd.print("Passcode:");
  464.   lcd.setCursor(16,3);
  465. }
  466.  
  467. void invalidCode()
  468. {
  469.   digitalWrite(redLEDpin, HIGH);
  470.   clearScreen();
  471.   lcd.setCursor(0,0);
  472.   lcd.print("********************");
  473.   lcd.setCursor(0,1);
  474.   lcd.print("** ACCESS DENIED! **");
  475.   lcd.setCursor(0,2);
  476.   lcd.print("**  INVALID CODE  **");
  477.   lcd.setCursor(0,3);
  478.   lcd.print("********************");
  479.  
  480.   delay(5000);
  481.   digitalWrite(redLEDpin, LOW);
  482.   displayCodeEntryScreen();
  483. }
  484.  
  485. void unlockDoor()
  486. {
  487.   digitalWrite(greenLEDpin, HIGH);
  488.   clearScreen();
  489.   lcd.setCursor(0,0);
  490.   lcd.print("********************");
  491.   lcd.setCursor(0,1);
  492.   lcd.print("** ACCESS GRANTED **");
  493.   lcd.setCursor(0,2);
  494.   lcd.print("**    WELCOME!!   **");
  495.   lcd.setCursor(0,3);
  496.   lcd.print("********************");
  497.  
  498.   delay(5000);                                                         //add any code to unlock the door here
  499.   digitalWrite(greenLEDpin, LOW);
  500.   menuScreen();
  501. }
  502.  
  503. void menuScreen()
  504. {
  505.   digitalWrite(greenLEDpin, HIGH);
  506.   clearScreen();
  507.   lcd.setCursor(8,0);
  508.   lcd.print("MENU");
  509.   lcd.setCursor(0,1);
  510.   lcd.print("1.SENSORS");
  511.   lcd.setCursor(0,2);
  512.   lcd.print("2.CONTROLS");
  513.   lcd.setCursor(0,3);
  514.   lcd.print("3.SETTINGS");
  515.   lcd.setCursor(11,1);
  516.   lcd.print("4.LOCK");
  517. }
  518.  
  519. void sensorScreen()
  520. {
  521.   digitalWrite(greenLEDpin, HIGH);
  522.   clearScreen();
  523.   lcd.setCursor(0,0);
  524.   lcd.print("SENSORS");
  525.   lcd.setCursor(0,1);
  526.   lcd.print("1.LIGHT");
  527.   lcd.setCursor(0,2);
  528.   lcd.print("2.TEMP/HU");
  529. }
  530.  
  531. void clearScreen()
  532. {
  533.   lcd.setCursor(0,0);
  534.   lcd.print("                    ");
  535.   lcd.setCursor(0,1);
  536.   lcd.print("                    ");
  537.   lcd.setCursor(0,2);
  538.   lcd.print("                    ");
  539.   lcd.setCursor(0,3);
  540.   lcd.print("                    ");
  541. }
  542.  
  543.  
  544. /*  
  545. //////////////////////////////////////////////// MENU & SUB-MENU LOOP ////////////////////////////////////////////////
  546. */
  547.  
Attached Files
File Type: pdf LCD - Menu Tree Layout_DRAFT.pdf (19.3 KB, 146 views)
Apr 10 '15 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You have a function followed by an opening brace at line 421:

Expand|Select|Wrap|Line Numbers
  1. int SerialReadPosInt() {
  2.  
  3. etc...
  4. }
  5.  
  6. {               <<--this is outside a function.
  7.    int l;
  8. etc...
All code in C must be inside a function.
Apr 10 '15 #2

P: 13
Opps I put that there trying to solve the same issue. I still get the same message after deleting it. Is the IDE not after a piece of connecting code or something? It didn't have anything before it in the original coding before I inserted the clock module pieces of code.
Apr 10 '15 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Carefully go through the code and be sure all code is inside a function.

You may still have errors.

You work through these errors by fixing only the first error and then recompiling. The first error may cause other errors which can cost you a lot of time.

There is no connecting code. All you have are functions which are part of your executable program. You can get into issues when functions in one source file call functions in another source file but if you read up on separate compilation and the build process you will see how to handle that.
Apr 10 '15 #4

Post your reply

Sign in to post your reply or Sign up for a free account.