element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Error in sketch code
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 38 replies
  • Subscribers 403 subscribers
  • Views 3840 views
  • Users 0 members are here
Related

Error in sketch code

Former Member
Former Member over 12 years ago

I am trying to learn to write some code for my BCD clock project.  Before doing the actual clock code I wired up 7 LEDs on my breadboard with 7 resistors.

Then I ran wires from each LED to a digital pin on the Arduino UNO. Specifically pins 7 to 13.

 

Then I wrote some code and tried to upload it but it is flagging 3 errors and I can't see where it is wrong. Probably something that I am missing as a newbie. The errors are as follows:

 

BoardTest:57: error: expected `;' before '{' token

BoardTest:64: error: expected `;' before '{' token

BoardTest:68: error: expected `;' before '{' token

 

The code is as follows:

 

 

// Short sketch to see if breadboard and UNO is properly wired.

 

int led1=7;

int led2=8;

int led3=9;

int led4=10;

int led5=11;

int led6=12;

int led7=13;

 

int seconds=0

 

void setup(){

  ledMode(led1, OUTPUT);

  ledMode(led2, OUTPUT);

  ledMode(led3, OUTPUT);

  ledMode(led4, OUTPUT);

  ledMode(led5, OUTPUT);

  ledMode(led6, OUTPUT);

  ledMode(led7 OUTPUT);

}

 

void loop(){

  seconds++;

    If(seconds == 1){

      digitalWrite(led1, HIGH);

      delay(1000);

    }

    If(seconds == 2){

      digitalWrite(led2, HIGH);

      digitalWrite(led1, LOW);

      delay(1000);

    }

  If(seconds == 3){

      digitalWrite(led1, HIGH);

      delay(1000);

    }

    If(seconds == 4){

      digitalWrite(led3, HIGH);

      digitalWrite(led2, LOW);

      digitalWrite(led1, LOW);

      delay(1000);

    }

    If(seconds == 5){

      digitalWrite(led1, HIGH);

      delay(1000);

    }

    If(seconds == 6){

      digitalWrite(led2, HIGH);

      digitalWrite(led1, LOW);

      delay(1000);

    }

    If(seconds == 7){

      digitalWrite(led1, HIGH);

      delay(1000);

    }

    If(seconds == 8){

      digitalWrite(led4, HIGH);

      digitalWrite(led3, LOW);

      digitalWrite(led2, LOW);

      digitalWrite(led1, LOW);

      delay(1000);

    }

    If(seconds == 9){

      digitalWrite(led1, HIGH);

      delay(1000);

    }

    If(seconds == 10){

      digitalWrite(led2, HIGH);

      digitalWrite(led1, LOW);

      delay(1000);

      digitalWrite(led1, LOW);

      digitalWrite(led2, LOW);

      digitalWrite(led3, LOW);

      digitalWrite(led4, LOW);

  seconds=0;

    }

}

 

 

 

Any help appreciated.

 

Thanks

  • Sign in to reply
  • Cancel

Top Replies

  • Former Member
    Former Member over 12 years ago +1
    John, Are those the only errors you're seeing? It looks to me that on line 11, you are missing a semicolon at the end of the line: int seconds=0 and on line 20, you are missing a comma after led7 ledMode…
  • Former Member
    Former Member over 12 years ago in reply to Former Member +1
    John, A table is just a fancy name for array, although in some languages it might be a fancy type of array such as an associative array. If you have 10 digits, each involving 4 led settings, you might…
Parents
  • billabott
    billabott over 12 years ago

    Hi JohnC.  Good attempt.  In respect to the current limits of each I/O pin,  I would suggest the need to multiplex the LEDs (only one on at a time.)  As a more correct example:

    void setup(){

    ledMode(led1, OUTPUT);

    ledMode(led2, INPUT);   // look up about writing a 1 to the input pin to put it in high impedance mode.

    ledMode(led3, INPUT);

    ledMode(led4, INPUT);   // it was stated that pinMode is preferred;  I don't know

    ledMode(led5, INPUT);   // and don't want to look it up right now.  sorry. 

    ledMode(led6, INPUT);

    ledMode(led7, OUTPUT);   //  this is the source of one of your errors  led7,

    }

     

    void loop(){

    seconds++;

     

    while(1){

       if (seconds == 1){

          digitalWrite(led1, HIGH);

          digitalWrite(led7, LOW);

          delay(10);

          digitalWrite(led7, HIGH);    //  in theory this will probably work

                                                     // use at your own risk since it is being adapted from PIC techniques

          delay(10);

          }  // fi     * really?  I am the first to note/share this technique

             //                  of marking what this closing brace matches  !?!

    ...

    if (timeToUpdateDisplay) break;

    }  // elihw     * ditto

     

    }  // pool       * ICNBI

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to billabott

    Thanks billabott,

     

    When it comes to the actual BCD clock code it will have to have more than 1 led on at a time so I'm not sure how that would work with multiplexing. 

    You say that ledMode(led7, OUTPUT); is the problem but I don't see what the problem is?  It is writen the same as all of the other ledMode statements.

    Should I be using pinMode() rather than ledMode()?

     

     

    I was also wondering if it would be better to use the switch statement rather than multiple if statements?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to Former Member

    John,

      Yes, it's almost always better to use a switch statement than a long

    list of if statements that test consecutive values like you have.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to Former Member

    Ok,  that's great!  I'll change them to the switch statement and see how that works.

     

    In my code, should I have used the pinMode() statement rather than ledMode() statement?

     

    Thanks for your help and patience.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to Former Member

    John,

      Yes, I believe pinMode() is what you want.

    (also be sure to remember the break statement in each branch

    of the switch statement).

     

    I would also note that sometimes there is a better way than

    a switch statement.  If all the branches of a switch statement

    are similar, or if they are all computing the value of the same

    variable, it may be better to use a table lookup rather than a

    switch statement.  This doesn't seem to apply in your example,

    but it may be helpful in the future.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • billabott
    billabott over 12 years ago in reply to Former Member

    JohnC,

    Couple of things since you are not using a MAX7219EWGMAX7219EWG or the like, which is what most 'Duino projects will tell you to use.  I happen to have a bag of 10 of those ICs in front of me today.  To drive the LEDs the ATMEGA328 has to do all the work that the display driver chip would normally do.  You can do this without the driver chip or over heating the UNO due to excess current demands thanks to an effect called Persistance of Vision.  Have you seen those spinning LED sticks on a bicycle wheel spoke that make readable displays by turning the LEDs on and off in a controlled sequence?  Check on YouTube if you need to.

     

    You may want to familiarize yourself with the barGraph.pde example in the IDE and its tutorial on arduino.cc.

     

    The way I envision it is that the loop() does the following things:

    1. determine/acquire the digit values to be displayed,

    2. configures the pins that are required to be ON,

    3. go into a loop turning the I/O pins on and off in sequence to provide the appropriate display on the output.

    Item #3 is were a switch statements would be of use.   An interrupt (or conditional break;) upon new data available would allow the execution sequence to escape from the while(1) code block and the new digit values to be processed.  I assume you are going for a clock type output display  and that is what will cause multiple switch statements within the while(1) code block.  There are many choices to make about how best to arrange the process so it runs efficiently with the goal to minimize observable flicker.  If you don't like the nesting of code blocks then move the while(1) block into a new void DisplayDigits() function and call it.  Your choice.

     

    PS  When you want to make your project more permanent after the bugs are worked out, I recommend using strip board.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • billabott
    billabott over 12 years ago in reply to Former Member

    JohnC,

    Couple of things since you are not using a MAX7219EWGMAX7219EWG or the like, which is what most 'Duino projects will tell you to use.  I happen to have a bag of 10 of those ICs in front of me today.  To drive the LEDs the ATMEGA328 has to do all the work that the display driver chip would normally do.  You can do this without the driver chip or over heating the UNO due to excess current demands thanks to an effect called Persistance of Vision.  Have you seen those spinning LED sticks on a bicycle wheel spoke that make readable displays by turning the LEDs on and off in a controlled sequence?  Check on YouTube if you need to.

     

    You may want to familiarize yourself with the barGraph.pde example in the IDE and its tutorial on arduino.cc.

     

    The way I envision it is that the loop() does the following things:

    1. determine/acquire the digit values to be displayed,

    2. configures the pins that are required to be ON,

    3. go into a loop turning the I/O pins on and off in sequence to provide the appropriate display on the output.

    Item #3 is were a switch statements would be of use.   An interrupt (or conditional break;) upon new data available would allow the execution sequence to escape from the while(1) code block and the new digit values to be processed.  I assume you are going for a clock type output display  and that is what will cause multiple switch statements within the while(1) code block.  There are many choices to make about how best to arrange the process so it runs efficiently with the goal to minimize observable flicker.  If you don't like the nesting of code blocks then move the while(1) block into a new void DisplayDigits() function and call it.  Your choice.

     

    PS  When you want to make your project more permanent after the bugs are worked out, I recommend using strip board.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • billabott
    billabott over 12 years ago in reply to billabott

    FYI

     

    ArduinoX: IDE for Arduino

    ArduinoX (pending a better name) is an alternative IDE for Arduino. It focuses on having a better out of the box experience than the standard IDE. It is more attractive, easier to use, and has better support for 3rd party boards.

    ArduinoX is still very alpha, but it can edit, compile, and download to several standard boards already.

    Current features include:

    • new code editor with line numbers, better color scheme, anti-aliasing, and a better default font   
    • !!!  I can't wait until everyone is using it.

     

     

    -=SyntaxMatters=-      image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to billabott

    Thanks billabott,

     

    I grabbed the code for that bargraph.pde and will take a look at it.  I did, in fact, notice the line counter at the bottom left and I have downloaded the ArduinoX IDE that you recommended.  I will install it tomorrow.  As I mentioned in my reply to Coder27, now that I have fugured out how to get these LEDs working in proper sequence, I am going to move it over to my ArduinoMega2560.

     

    Before going too far with it though, I am going to see if there is a way to shorten the code a bit. I'm sure that since I am just learning to coding in 'C', my sketch is probably larger than an experienced 'C' programmer would have.

     

    Thanks for all your help billabott, much appreciated.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • billabott
    billabott over 12 years ago in reply to Former Member

    You are welcome.  You seem to be adapting and advancing quite quickly.  Feel free to read my blog  "BP 0x0004".

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to billabott

    Thanks billabott,

     

    I find that choosing a project and actually trying to get it to work is a lot easier for me to learn rather than just reading a book.  Books and tutorials are very helpful of course, but hands-on gets your mind working and understanding it better.  I'm going to make a number of errors along the way but that's part of the learning process,... as long as you learn from your mistakes.  image  Certainly having some experienced people here on Element14 who are so willing to take the time and patience to help others is priceless.

     

    Merry Christmas to you and yours from my house.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube