I'm getting the above error message (ledpin not declared in this scope) from this line of code digitalWrite(ledpin[0], HIGH);
It is from Project 8: Memory Game in the Arduino Project Handbook
Can anyone help me out? Thanks
I'm getting the above error message (ledpin not declared in this scope) from this line of code digitalWrite(ledpin[0], HIGH);
It is from Project 8: Memory Game in the Arduino Project Handbook
Can anyone help me out? Thanks
Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.
[code] Paste sketch here. [/code]
#include <Tone.h>
Tone speakerPin;
int starttune[] = {NOTE_C4, NOTE_F4, NOTE_C4, NOTE_F4, NOTE_C4,
NOTE_F4, NOTE_C4, NOTE_F4, NOTE_G4, NOTE_F4,
NOTE_E4, NOTE_F4, NOTE_G4};
int duration2[] = {100, 200, 100, 200, 100, 400, 100, 100, 100, 100, 200, 100, 500};
int note[] = {NOTE_C4, NOTE_C4, NOTE_G4, NOTE_C5, NOTE_G4, NOTE_C5};
boolean button[] = {2,3,4,5};
boolean ledPin[] = {8,9,10,11};
int turn = 0;
int buttonstate = 0;
int randomArray[100];
int inputArray[100];
void setup() {
Serial.begin(9600);
speakerPin.begin(12);
for (int x = 0; x < 4; x++) {
pinMode(ledpin[X], OUTPUT);
}
for (int x = 0; x < 4; x++) {
pinMode(button[x], INPUT);
digitalWrite(button[x], HIGH);
}
randomSeed(analogRead(0));
for (int thisNote = 0; thisNote < 13; thisNote ++) {
speakerpin.p1ay(starttune[thisNote]);
if (thisNote == 0 || thisNote == 2 || thisNote == 4 || thisNote == 6) {
digitalWrite(ledpin(0), HIGH);
}
if (thisNote == 1 || thisNote == 3 || thisNote == 5 || thisNote == 7 || thisNote == 9 || thisNote == 11) {
digitalWrite(ledpin[1], HIGH);
}
if (thisNote == 8 || thisNote == 12) {
digitalWrite(ledpin[2], HIGH);
}
if (thisNote == 10) {
digitalWrite(ledpin[3], HIGH);
}
delay(duration2[thisNote]);
speakerpin.stop();
digitalWrite(ledpin[0], LOW);
digitalWrite(ledpin[1], LOW);
digitalWrite(ledpin[2], LOW);
digitalWrite(ledpin[3], LOW);
delay(25);
}
delay(1000);
}
void loop() {
for (int y = 0; y <= 99: y++) {
digitalWrite(ledpin[0], HIGH);
digitalWrite(ledpin[1], HIGH);
digitalWrite(ledpin[2], HIGH);
digitalWrite(ledpin[3], HIGH);
for (int thisNote = 0; thisNote < 6; thisNote ++) {
speakerpin.p1ay(note[thisNote]);
delay(duration[thisNote]);
speakerpin.stop();
delay(25);
}
digitalWrite(ledpin[0], LOW);
digitalWrite(ledpin[1], LOW);
digitalWrite(ledpin[2], LOW);
digitalWrite(ledpin[3], LOW);
delay(1000);
for (int y = turn; y <= turn; y++) {
Serial.print1n("");
Serial.print("Turn: ");
Serial.print(y);
Serial.print1n("");
randomArray[y] = random(1, 5);
for (int x = 0; x <= turn; x++) {
Serial.print(randomArray[x]);
for (int y = 0; y < 4; y++) {
if (randomArray[x] == 1 && ledpin[y] == 8) {
digitalWrite(ledpin[y], HIGH);
speakerpin.p1ay(NOTE_G3, 100);
delay(400);
digitalWrite(ledpin[y], LOW);
delay(100);
}
if (randomArray[x] == 2 && ledpin[y] == 9) {
digitalWrite(ledpin[y], HIGH);
speakerpin.p1ay(NOTE_A3, 100);
delay(400);
digitalWrite(ledpin[y], LOW);
delay(100);
}
if (randomArray[x] == 3 && ledpin[y] == 10) {
digitalWrite(ledpin[y], HIGH;
speakerpin.p1ay(NOTE_B3, 100);
delay(400);
digitalWrite(ledpin[y], LOW);
delay(100);
}
if (randomArray[x] == 4 && ledpin[y] == 11) {
digitalWrite(ledpin[y], HIGH);
speakerpin.p1ay(NOTE_C4, 100);
delay(400);
digitalWrite(ledpin[y], LOW);
delay(100);
}
}
}
}
input();
}
}
void input() {
for (int x = 0; x <= turn;) {
for (int y = 0; y < 4; Y++) {
buttonstate = digitalRead(button[y]);
if (buttonstate == LOW && button[y] == 2) {
digitalWrite(ledpin[0], HIGH);
speakerpin.p1ay(NOTE_G3, 100);
delay(200);
digitalWrite(ledpin(0), LOW);
inputArray[x] = 1;
delay(250);
Serial.print("");
Serial.print(1);
if (inputArray[x] != randomArray[x]) {
fail();
}
x++;
}
if (buttonstate == LOW && button[y] == 3) {
digitalWrite(ledpin[1], HIGH);
speakerpin.p1ay(NOTE_A3, 100);
delay(200);
digitalWrite(ledpin[1], LOW);
inputArray[x] = 2;
delay(250);
Serial.print("");
Serial.print(2);
if (inputArray[x] != randomArray[x]) {
fail();
}
x++;
}
if (buttonstate == LOW && button[y] == 4) {
digitalWrite(ledpin[2], HIGH);
speakerpin.p1ay(NOTE_B3, 100);
delay(200);
digitalWrite(ledpin[2], LOW);
inputArray[x] = 3;
delay(250);
Serial.print("");
Serial.print(3);
if (inputArray[x] != randomArray[x]) {
fail();
}
x++;
}
}
}
delay(500);
turn++;
}
void fail() {
for (int y = 0; y <= 2; y++) {
digitalWrite(ledpin[0], HIGH);
digitalWrite(ledpin[1], HIGH);
digitalWrite(ledpin[2], HIGH);
digitalWrite(ledpin[3], HIGH);
speakerpin.p1ay(NOTE_G3, 300);
delay(200);
digitalWrite(ledpin[0], LOW);
digitalWrite(ledpin[1], LOW);
digitalWrite(ledpin[2], LOW);
digitalWrite(ledpin[3], LOW);
speakerpin.p1ay(NOTE_C3, 300);
delay(200);
}
delay(500);
turn = -1;
}
Two problems.
First the code is very hard to read as you missed the instruction on using code tags.
Second,
Do you see anything wrong with this line of the program:
boolean ledPin[] = {8,9,10,11};
Hint,hint. Last I knew, booleans were either 0 or 1.
The code has a number of problems, one of which is that although ledPin is declared as an array (albeit wrongly as pointed out above) it is sometimes given a slightly different name, as in
pinMode(ledpin[X], OUTPUT);
and in other places the array syntax is wrong
digitalWrite(ledpin(0), LOW);
Added to that the code is posted wrongly so has been mangled by the forum software.
@M_Yoder
Lessons to be learned :
You must be very precise when entering code.
Uppercase/lowercase matters when used in variable names. They are not the same
() is not the same as []
Auto Formatting the code in the IDE before posting is helpful and can help point out problems
Always use code tags when posting code. Read the advice stickies at the top of the forum page