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 3824 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
  • billabott
    billabott over 12 years ago in reply to Former Member

    I'll bet if you try to translate this PIC C code into 'Duino C you will learn a lot; you can even do it in the Simulator.  (Oh, oh, the source code I wrote for the hpLED chaser display in the video was over written. Sorry)  This one works in a similar fashion:

     

    This even points out an excellant method of handling the array implementation for super efficiency.

    This is the second version which is rewritten to take advantage of

      Arrays to reduce the amount of space required by the application.

     

    myke predko

    04.09.12

     

    */

     

    __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \

      & UNPROTECT & BORDIS & IESODIS & FCMDIS);

     

     

    int i, j, n;

    int Value = 0;

    int Dlay = 65;                  //  LED Time on Delay Variable

    const char PORTAValue[8] = {0b010000, 0b100000, 0b010000, 0b000100,

                                0b100000, 0b000100, 0b000100, 0b000010};

    const char TRISAValue[8] = {0b001111, 0b001111, 0b101011, 0b101011,

                                0b011011, 0b011011, 0b111001, 0b111001};

                // only two of the six bits will be 0 for output at a time

    const char NOTPORTA[8] = {0, 0, 0, 0, 0, 0, 0, 0};

     

    main()

    {

     

        PORTA = 0;

     

        CMCON0 = 7;                //  Turn off Comparators

        ANSEL = 0;                  //  Turn off ADC

                                           // details that the 'Duino IDE auto-magically handles for us

     

        j = 0;                      //  Reset the Display Counter

     

        while(1 == 1)               //  Loop Forever

        {

            for (i = 0; i < 8; i++ )

            {                       //  Loop through Each of the 8 LEDS

                for (n = 0; n < Dlay; n++);

                if ((Value & (1 << i)) == 0)

                    PORTA = NOTPORTA[i];

                else

                    PORTA = PORTAValue[i];

                TRISA = TRISAValue[i];

            }  //  rof

     

            j = j + 1;              //  Increment the Counter every 1/2s

            if (j >= 50)

            {

                Value = Value + 1;  //  Increment Display Counter

                j = 0;              //  Reset the Counter

            }  //  fi

        }  //  elihw

    }  //  End cLEDDisp

    Source: Myke Predko's "123 PIC Microcontroller Experiments for the Evil Genius"

     

    image

    Example: If you were to try this using only 3 digital i/o pins, each pin has a current limiting Resistor whose value is R/2.  A good number for R is 10K, therefore R/2 = 5K.  Attached to the R/2s there are hetero-paired LEDs (1 anode and 1 cathode on each node) for a total of 6 LEDs.  The first hpLED is connected to R1-R2, the second hpLED is connected to R1-R3, and the third hpLED is connected to R2-R3.  Initialize by setting all 3 pins to INPUT (high impedance /.: not enough current flows to light up any LED).  We can select any two pins and make them OUTPUTs hence when one pin  is sourcing current (0 or LOW) and the other pin is sinking current (1 or HIGH) the direct conversion of electrical energy to a photonic emission will occur (... that means the LED will light UP).   Question: If P1 is High and P2 is Low; which P(out) should we toggle to make the LED blink and at what speed? Answer: Either one (it doesn't matter) and slower that 48 Hz.

     

    You don't have permission to edit metadata of this video.
    Edit media
    x
    image
    Upload Preview
    image

     

    In this video you can see a PICKIT1 Flash Starter Kit running the code I wrote for hpLED_chaser.   If the wait loop time(s) were very much shorter, all the LEDs would appear to be ON simultaneously; and that, my friends, is Persistance Of Vision at work.  Check out some of these POV videos on YouTube.

     

    It is great that you are trying to get a better understanding of how to apply these concepts.  For more details please see:  http://en.wikipedia.org/wiki/Charlieplexing

    Warning: Hetero pairing leads to photonic emissions.

    • 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 will look at the code and the videos as well.

     

    John

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

    Hi, John.

    That's right you cannot reverse potential on a node connected to ground.  Ground is a source of electrons that flow to nodes with a positive potential.  The thing to think about is the an output pin that is set to 1 or High is more positive than an output pin that is set to 0 or Low.  The current will flow between those pins if a path exists.  Any output pin can be set to High or Low under the control of the software.  Whatever path for current flow that exists will activate. 

     

    P.S.  I have found a great book for you to learn C++ with: Simple C++: Featuring Robodog and the Profound Object-Oriented Programming Method 

     

    Oh, heck, Jose Pino has taken almost all the fun out of this project by documenting his PIC design at http://www.josepino.com/?anp-1224hr-led-clock1.

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

    John

    Don't go to the dark side and borow someone elses work, stick with yours.

    You will learn a lot more.

    Keep your sketchs that you create along the way, they will serve you later.

     

    Billabot

    billabott wrote:

    when one pin  is sourcing current (0 or LOW) and the other pin is sinking current (1 or HIGH)

     

    I always find begineers get confused over true current flow.

    I found that using terms such as "..the HIGH causes current to flow to the LOW..", which is not right but helps when they use the water comparison.

     

    I recall a NZ comedian (Fred Dagg) once describing the wind along the lines of "... the big Highs rush around trying to fill the Lows and make them all even....."

     

    Cheers

    mark

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

    Really?  I tried to be careful not start the silly waste of  keystrokes about which way current flows.  So you are saying the surplus electrons and the excess holes meet in the middle somewhere and cancel each other out?  image   No?  You said, "I always find begineers get confused over true current flow."  I am not sure what you are saying exactly.  No matter.  Aal izz well.  No worries, mate. 

     

    I know that the convention by historical error ( and 100 years later, they didn't want to correct the mistake when it was better understood ) is for "Current" to "Flow" from positive to negative, but, that is not what is really happening is it?  

     

    I declare myself to be a life long learner.  I am neither a begineer or a beginner.  Thank you.  I love New Zealand, except for the earthquakes and I thank all the people there for helping to make films such as The Lord of the Rings Trilogy and The Hobbit Trilogy.

     

    We must accept Atmel as the Authority on the issue in this case.  In the pertinent document(1) it states:

     

    Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit).  The Port D output buffers have symmetrical drive characteristics with both high sink and source capability.  As inputs, Port D pins that are externally pulled low will source current if the pull-up resistors are activated.

     

    That clearly says that something, presumably holes because it cannot be electrons in my opinion, flows into the low/0/ground.  So Atmel adheres to the conventional view of current flow.  That's fine.   It's just not the way my brain understands the truth of electron flow. 

     

    The only visualization of conventional current flow I can think of is a film of a freshly opened 3 liter bottle of 7Up being played in reverse - then you can actually see the holes(bubbles) being filled up with electrons and disappearing from existance.  Silly, isn't it.

     

     

    (1) http://www.atmel.com/Images/8271S.pdf

    • 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 am aware that electrons flow from negative to positive, (had to do some electronic stuff to get my ham radio license), but I wasn't thinking in terms of positive and negative when thinking about a pin being HIGH (positive), and a pin being LOW (less positive).  I generally think of negative as being ground as opposed to being "less positive". Since the Arduino has ground pins, that's what I was thinking was being used and could not understand how you could reverse that pin to be positive.

     

    As for books, as I mentioned prviously, I bought that book that you recommended by Simon Monk.  While it was helpful I found it to be lacking in a number of areas as far as learning 'C' goes.  I have since ordered another book called Beginning Arduino Programming. I am expecting it to arrive shortly.

     

    Thanks again.

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

    Thanks Mark,

     

    I have some knowledge about electronics but little about the Arduino and the 'C' programming language although I think I am getting a bit of a handle on the 'C' now.  I keep all my code snippets to review periodically and also in the event that I can use them in future projects. The BCD clock that I am working on uses 20 LEDs that I am controlling with Port manipulation however, the UNO does not have enough pins so I am trying to figure out how this multiplexing, or Charlieplexing works to reduce the number of pins required

     

    Thanks..

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

    Hi John.  Is it safe to say the you are also finding the learning curve a little steeper due to the choice to transition from the analog world into the digital world.  Did you do any digital modes with the HAM equipment?

     

    At your service.

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

    Yes, digital is quite different than analog but I am doing a lot of reading, testing, and of course, asking questions on this forum.  As I mentioned in a previous post, it is great to see that there are so many people who are both willing, and have the patience to help those who are new to Arduino and the 'C' language such as myself.

     

    I did very little in terms of digital with my ham exams.  Most of it is basic electronics, antenna theory, IC circuits, filters etc.

     

    My main hobby is designing and building antennas. My wife says I have so many antennas up that people will think we are terrorists or something.  image   She just doesn't understand Amateur Radio.  When she asks me why I like ham radio and I say I enjoy talking to people around the world, she says:  "Why don't you just use the telephone?"

     

    I have been reading up on this "Charleplexing" and I am finding that controlling 20 LEDs to sometimes have a single LED on and sometimes multiple LEDs on is quite complicated.  Not sure that I am getting my head wrapped around this at all.

     

    John

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

    Billabott

    Thanks for your kindness re NZ.

    Yes living just out of Chch,  we are all proably world experts in earthquakes/aftershocks after 13,000 of them.

     

     

    You are quite correct about electron flow, and I do realise that about electron flow (learnt that wayyyyy back during training).

     

    I thought  that John might not, and asummed he was a beginner (mistakely....sorry and apologies) (and yes the extra e was a typo ..sorry).

    For those struggling to comprehend electricity, electon flow isn't something that helps....

     

     

    John

    Years ago I did some multiple output work using latches, and I wonder if adding some latches for your hour and minute leds might not reduce your pin count.

    There are also some I2C devices that might also reduce your pin count, and make the code a little easier for the leds that don't need to change often.

     

    Mark

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

    Billabott

    Thanks for your kindness re NZ.

    Yes living just out of Chch,  we are all proably world experts in earthquakes/aftershocks after 13,000 of them.

     

     

    You are quite correct about electron flow, and I do realise that about electron flow (learnt that wayyyyy back during training).

     

    I thought  that John might not, and asummed he was a beginner (mistakely....sorry and apologies) (and yes the extra e was a typo ..sorry).

    For those struggling to comprehend electricity, electon flow isn't something that helps....

     

     

    John

    Years ago I did some multiple output work using latches, and I wonder if adding some latches for your hour and minute leds might not reduce your pin count.

    There are also some I2C devices that might also reduce your pin count, and make the code a little easier for the leds that don't need to change often.

     

    Mark

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

    Hi Mark,

     

    Actually I never really thought about latches or flip-flops, (almost the same thing) or 12C devices.  I was really trying to find a way of doing it with a standard UNO.  As I mentioned before, I could use the Mega if I can find some PORT to Pin information. I could just test for them I guess but thought it would be far less time if I was able to find it on the internet.  I know PORTD controls pins 0-7 and PORTB handles pins 8-13 but I can't find anything like that for the Mega2560.  I think I will try a few more things with the bare UNO and if I can't come up with any ideas I'll look at maybe using a 12C device or perhaps latches.

     

    Appreciate the suggestions Mark.

     

    John

    • 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