Help with Lesson 7

i need help on lesson 7

Lesson 7 of what?
Start a new topic

1 Like

When you need help, you must supply necessary information to get help. Supply (1) a drawing of your issue, (2) the programming code, (3) a description of how you expect the program to work, and (4) how you observe the program working (or not working). For example...

  1. I am creating an electronic keyboard using Arduino Projects book, Lesson 7. Here is the schematic.
  2. Here is the code from the Project Book
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);
  }
}
  1. I should be able to compile the program, then press a button to make a tone.
  2. My problem is; the program does not compile, giving me this error:
sketch.ino:2:14: error: conflicting declaration 'int buttons [0]'
 int buttons[0] = 2;
              ^
sketch.ino:1:5: note: previous declaration as 'int buttons [6]'
 int buttons[6];

The error you see is because "int buttons[0]" was already declared and initialized (to 0), so to change the first element of buttons[] to 2 you must place that line inside setup() because setup() runs only one time, where loop() repeats itself. Although buttons[] is not used, the resistor network changes the input to analog pin A0 to the ranges noted inside notes[] in the program, making the buzzer sound.

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

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

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 topic was automatically closed 180 days after the last reply. New replies are no longer allowed.