element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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 Arduino project 7 Keyboard 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 9 replies
  • Subscribers 394 subscribers
  • Views 2262 views
  • Users 0 members are here
  • help
  • code
  • bug
  • error
  • arduino
Related

Arduino project 7 Keyboard code

Former Member
Former Member over 11 years ago

Hi All

 

I put together the 7th project in the Arduino project book and unfortunately i cannot get the code to compile, this is the trouble some code:

 

int buttons[6];

int buttons[0] = 2;

 

that's how its written in the book, when i try to verify the code i get this error

 

project_7_keyboard:3: error: conflicting declaration 'int buttons [0]'

project_7_keyboard:1: error: 'buttons' has a previous declaration as 'int buttons [6]'

 

now I can see that the compiler thinks I am trying to declare 2 separate int variables as buttons, but the book teaches that this is how you assign value to the first element of the array.

 

can i please have some advice.

 

Thanks

 

Erny

  • Sign in to reply
  • Cancel

Top Replies

  • Former Member
    Former Member over 11 years ago +1
    I would not consider myself an expert, but the book must be wrong. If those two statements exist in the same program, then yes you are attempting to declare the variable buttons twice. The first statement…
  • mcb1
    mcb1 over 11 years ago in reply to Former Member +1
    Erny There is a discussion here Project 07 Keyboard Instrument - Arduino Forum about it. You do need to drop the int, as you have already defined buttons as a 7 element array (0-6) Just make sure the buttons…
Parents
  • Former Member
    Former Member over 11 years ago

    I would not consider myself an expert, but the book must be wrong.

    If those two statements exist in the same program, then yes you are attempting to declare the variable buttons twice.

     

    The first statement declares an array of 6 integers.

    In the second statement, you want to assign the value 2 to the first element (index 0) of the array.

    The inclusion of the "int" before the assignment causes the problem.

    If you remove the "int" then the statement becomes an assignment statement, instead of a declaration statement.

     

    I hope this helps.

    Larry

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 11 years ago in reply to Former Member

    Hi Larry

     

    thanks for the response, I had a feeling it didn't look correct. also i tried to remove the int from buttons[0] = 2 and i still get an error. this time it tells me it was expecting something before the '='. i would copy and paste the error but i'm currently at work so it would have to wait till i get home.

     

    but if you know what im doing wrong please let me know.

     

    Thanks

     

    Erny

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

    Erny

    There is a discussion here Project 07 Keyboard Instrument - Arduino Forum about it.

     

    You do need to drop the int, as you have already defined buttons as a 7 element array (0-6)

    Just make sure the buttons[0] is a zero not a Ohh by mistake.

     

    Mark

     

    edit: array should be 7 elements 0-6 ....Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 11 years ago in reply to mcb1

    thanks Mark that is very useful!

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

    i'm sorry, but I got the same problem with this 2  fuc**** lines,  and I tried what you have all said (sorry my english isn't very good i'm french. I hope you understand) but it still doesn't work, here is my code:

     

     

    int buttons[6];

    int buttons[0] = 2;

    int notes[] = {262, 294, 330, 349};

    void setup() {

      Serial.begin(9600);

    }

    void loop () {

      int keyVal = analogRead(A0);

      Serial.println(keyVal);

    if (keyVal == 1023){

        tone(8, notes[0]);

      }

      else if (keyVal >= 990 && keyVal <= 1010) {

        tone(8, notes[1]);

      }

      else if(keyVal >= 505 && keyVal <= 515) {

        tone(8, notes[2]);

      }

      else if(keyVal >= 5 && keyVal <= 10) {

        tone(8, notes[3]);

      }

      else{

        noTone(8);

      }

    }

    ive tried tu cut the "int" on the second line, but like erny, it tells me " expected constructor, destructor, or type conversion before '=' token"

    i've made sure that my 0  isn't an o, and it still doesn't work.

    please help me

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

    i'm sorry, but I got the same problem with this 2  fuc**** lines,  and I tried what you have all said (sorry my english isn't very good i'm french. I hope you understand) but it still doesn't work, here is my code:

     

     

    int buttons[6];

    int buttons[0] = 2;

    int notes[] = {262, 294, 330, 349};

    void setup() {

      Serial.begin(9600);

    }

    void loop () {

      int keyVal = analogRead(A0);

      Serial.println(keyVal);

    if (keyVal == 1023){

        tone(8, notes[0]);

      }

      else if (keyVal >= 990 && keyVal <= 1010) {

        tone(8, notes[1]);

      }

      else if(keyVal >= 505 && keyVal <= 515) {

        tone(8, notes[2]);

      }

      else if(keyVal >= 5 && keyVal <= 10) {

        tone(8, notes[3]);

      }

      else{

        noTone(8);

      }

    }

    ive tried tu cut the "int" on the second line, but like erny, it tells me " expected constructor, destructor, or type conversion before '=' token"

    i've made sure that my 0  isn't an o, and it still doesn't work.

    please help me

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

    Reading through the comments on the link mcb1 posted, you could remove the first two lines (or comment out) since these variables are not used anywhere else in the code.

     

    Frederick

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

    pierre

     

    Can you post your entire code.

    use the >> in the top right, choose the Syntax Highlighting and C++

     

    It will format the text as above.

     

    As fvan said have a look at my link (I forgot about that it was sooooo long ago)

     

    Mark


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

    You have declared buttons twice.   I have run this bit of code on my simulator I commented the second int buttons[0] = 2;. What are you using to drive the inputs.  How are you selecting the analog value for A0? As you have such a small window areas to create a tone. Fredrick is correct, buttons array is never used. Also, you are using pin 8, tone is a PWM output, should that not be on a PWM pin, then for a amp and speaker, buzzer device. Which Arduino are you using?

     

    Dave M.

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

    Sorry pierre

    I stupidly replied fom the inbox and missed the code you included.

     

    Have a go with the sugestions made already and let us know how you got on.

     

     

    Mark

    • 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