Go Down

Topic: Project 7 Issues (Read 1 time) previous topic - next topic

I'm having many issues with this project.

First off, it says to make these global variables:

Code: [Select]

int buttons[6];

int buttons[0] = 2;

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


But, when I upload it gives me an error:

sketch_aug30a:2: error: conflicting declaration 'int buttons
  • '
    sketch_aug30a:1: error: 'buttons' has a previous declaration as 'int buttons [6]'

    The second thing is that even when I press the buttons connected to the Analog pin, nothing but "0" comes up on the serial monitor.

    And lastly, no noise comes out.

    Here's the code:

    Code: [Select]

    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);
      }
    }


    It said since resistors are alway different, that I can use the serial monitor to check the values when buttons are pressed and use them for the code, but the only value I get is "0".

    Help would be greatly appreciated. Thanks!
Code: [Select]
#include <cheesy.quote>

Grumpy_Mike

You simply can not do this:-
Code: [Select]

int buttons[6];
int buttons[0] = 2;

Because you are defining a variable twice. You can do:-
Code: [Select]

int buttons[6];
buttons[0] = 2;

the int bit says this is a variable I will use as an integer.

Quote
but the only value I get is "0".

Then you have wired it up wrong.


You simply can not do this:-
Code: [Select]

int buttons[6];
int buttons[0] = 2;

Because you are defining a variable twice. You can do:-
Code: [Select]

int buttons[6];
buttons[0] = 2;

the int bit says this is a variable I will use as an integer.


If you can't do that, why does it tell you to in the instructions?
I tried taking out int but then I get:
"sketch_aug30a:2: error: expected constructor, destructor, or type conversion before '=' token"

Quote
Quote
but the only value I get is "0".

Then you have wired it up wrong.

I'll check again but it is wired exactly to the instructions...
Code: [Select]
#include <cheesy.quote>

Grumpy_Mike

If it says that it is wrong.
However unless you post all your code I can not correct the other mistakes you have made that removing that incorrect int then shows up.


If it says that it is wrong.
However unless you post all your code I can not correct the other mistakes you have made that removing that incorrect int then shows up.


I already have it in the OP but here it is again:
Code: [Select]

int buttons[6];
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);
  }
}
 

This code has your change in it.
Code: [Select]
#include <cheesy.quote>

Grumpy_Mike

Quote
I already have it in the OP but here it is again:

No you didn't you posted two code fragments.
The reason you got an error was because you attempted to assign a value to an initialized variable outside a function. If you move it into the setup function it will compile like this:-
Code: [Select]

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

void setup() {
  Serial.begin(9600);
  buttons[0] = 2;
}


However what is this array? You do not use it anywhere else in the code so it is pointless.

Are you sure you are copying this code correctly?

Chlyxu

Thanks so much. I had the same problem but your fix completely did the job. Now I can play a little tune with mine. lol..

Uploaded it :smiley-red::?https://www.youtube.com/watch?v=Tex9JEgLAcg&list=UU_ShHfkdoGZnRDefbCB_RfQ

I suppose this should be noted for the next edition.


Thanks so much. I had the same problem but your fix completely did the job. Now I can play a little tune with mine. lol..

Uploaded it :smiley-red::?https://www.youtube.com/watch?v=Tex9JEgLAcg&list=UU_ShHfkdoGZnRDefbCB_RfQ

I suppose this should be noted for the next edition.


I copied the book word for word...
Code: [Select]
#include <cheesy.quote>

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy