need a little help with some code

I came across an led puzzle box project today which i thought looked really good and got it working first time with no issues, I'm trying to add a buzzer to the code which plays at the end when all led's are lit but no matter how i try to write the code in I get similar error messages and i cant work out what I'm missing ( I know its something simple like a comma or a bracket in the wrong place but i now cant see for looking).

if anyone can help it would be greatly appreciated as I've scoured the web and come up with nothing.

error code...
Arduino: 1.8.13 (Windows 10), TD: 1.53, Board: "Arduino Uno"
C:\Users\duncan\Documents\Arduino\ledpuzzlewith_buzzer\ledpuzzlewith_buzzer.ino: In function 'void toggle3(int, int, int)':
ledpuzzlewith_buzzer:130:19: error: expected primary-expression before ']' token
if (LED_State[] = ){0, 1,1,1,1,1,1}; //d
^
ledpuzzlewith_buzzer:130:23: error: expected primary-expression before ')' token
if (LED_State[] = ){0, 1,1,1,1,1,1}; //d
^
ledpuzzlewith_buzzer:130:39: error: expected ';' before '}' token
if (LED_State[] = ){0, 1,1,1,1,1,1}; //d
^
ledpuzzlewith_buzzer:131:19: error: 'thisNote' was not declared in this scope
tone(11, melody[thisNote], duration); //d
^~~~~~~~
ledpuzzlewith_buzzer:134:3: error: 'else' without a previous 'if'
else
^~~~
exit status 1
expected primary-expression before ']' token

#include <IRremote.h>  //Infrared Remote Library
 #include "pitches.h"  // For Passive Buzzer (dunk)

int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);  //Setup the IR remote for initilisation
decode_results results;   // set a decode variable, called results


const int LED [] = {0, 7, 6, 5, 4, 3}; // Set LED's as Digital Pins 7 thru 3
const int buzzer = 11; //(dunk)


int LED_State[] = {0, 0, 0, 0, 0, 0, 0}; // Inlcude one more than the number of LED's
// you have, first val must be zero. Sets all
// LED's to low/off

int melody[] = {
  NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6};
int duration = 500;  // 500 miliseconds // tones for buzzer (dunk)

unsigned int val = 0; // A variable to store the IR reading

const int code1 = 12495;   //Set the values recieved
const int code2 = 6375;  //from the IR remote
const int code3 = 31365;  //for your chosen buttons
const int code4 = 4335;
const int code5 = 14535;
const int code6 = 41565;  //all leds off (dunk)


void setup() {

  Serial.begin(9600); // Turn on to allow you to check your IR remote values

  irrecv.enableIRIn(); // Start the IR receiver

  for (int i = 1; i < 6; i++) { // Set all LED's as OUTPUTS
    pinMode(LED[i], OUTPUT);
    pinMode(buzzer,OUTPUT);
  }
}

void loop() {

  if (irrecv.decode(&results)) {  //If the IR receiver gets a signal
    val = results.value;          //Save that signal as a value to 'val'
    Serial.println(val);          //To check the values from your remote


    switch (val) {
      case code1:                   //If the IR reciever picks up code1
        toggle2(1, 4);              //Toggle these LED's on/off
        break;
      case code2:                   //If the IR reciever picks up code2
        toggle2(2, 3);              //Toggle these LED's on/off
        break;
      case code3:                   //If the IR reciever picks up code3
        toggle3(3, 4, 5);              //Toggle these LED's on/off
        break;
      case code4:                   //If the IR reciever picks up code4
        toggle3(1, 4, 5);              //Toggle these LED's on/off
        break;
      case code5:                   //If the IR reciever picks up code5
        toggle3(1, 3, 4);              //Toggle these LED's on/off
        break ;
      case code6 :
        for (int i = 1; i < 6; i++) { // If Code 6 is received, all LED's are
          digitalWrite(LED[i], LOW);   // turned off
          LED_State[i] = 0;
        }
        break ;
    }
    irrecv.resume(); // Recieve the Next value
  }
}

//Create a function to toggle two LED's on/off
void toggle2(int x, int y) {    //Set x and y as two of the LED's (mix it up!)
  if (LED_State[x] == 0) {       //check to see if LED is currently on low/off
    digitalWrite(LED[x], HIGH);   // if its off, turn it on
    LED_State[x] = 1;
  } else {
    digitalWrite(LED[x], LOW);    // if its on, turn it off
    LED_State[x] = 0;
  }
  if (LED_State[y] == 0) {       //check to see if LED is currently on low/off
    digitalWrite(LED[y], HIGH);   // if its off, turn it on
    LED_State[y] = 1;
  } else {
    digitalWrite(LED[y], LOW);    // if its on, turn it off
    LED_State[y] = 0;
  }
}

//Create a function to toggle three LED's on/off
void toggle3(int x, int y, int z) {     //Set x and y as two of the LED's (mix it up!)
  if (LED_State[x] == 0) {              //check to see if LED is currently on low/off
    digitalWrite(LED[x], HIGH);         // if its off, turn it on
    LED_State[x] = 1;
  } else {
    digitalWrite(LED[x], LOW);    // if its on, turn it off
    LED_State[x] = 0;
  }
  if (LED_State[y] == 0) {       //check to see if LED is currently on low/off
    digitalWrite(LED[y], HIGH);   // if its off, turn it on
    LED_State[y] = 1;
  } else {
    digitalWrite(LED[y], LOW);    // if its on, turn it off
    LED_State[y] = 0;
  }
  if (LED_State[z] == 0) {       //check to see if LED is currently on low/off
    digitalWrite(LED[z], HIGH);   // if its off, turn it on
    LED_State[z] = 1;
  } else {
    digitalWrite(LED[z], LOW);    // if its on, turn it off
    LED_State[z] = 0;
  } if {
    (LED_State[] = ){0, 1,1,1,1,1,1}; //d
  tone(11, melody[thisNote], duration); //d
  delay(1000); //(dunk)
  }
  else
  {
    noTone(11);
  }
  
}

The code you have posted is not the same as the code that produced the error.

The compiler tells you the line and the colum where the error occurs

edpuzzlewith_buzzer:130:19:

The first number is the line.
Though in the code you have posted there is no line 130

good that the compiler quote the text in the line

ledpuzzlewith_buzzer:130:19: error: expected primary-expression before ']' token
if (LED_State[] = ){0, 1,1,1,1,1,1}; //d

So what difference do you see in the lines that were not complained and the one the compile-error occurs?

  if (LED_State[z] == 0) {       //check to see if LED is currently on low/off
    digitalWrite(LED[z], HIGH);   // if its off, turn it on
    LED_State[z] = 1;

  } if {
    (LED_State[] = ){0, 1,1,1,1,1,1}; //d

best regards Stefan

This makes no sense. Can you describe what this is supposed to do?

if {
    (LED_State[] = ){0, 1,1,1,1,1,1}; //d
  tone(11, melody[thisNote], duration); //d
  delay(1000); //(dunk)
  }
  else
  {
    noTone(11);
  }

its definitely the same code im trying to get to work. as i said at the top its supposed to set the buzzer off when all five leds are lit and not before.

Arduino: 1.8.13 (Windows 10), TD: 1.53, Board: "Arduino Uno"

C:\Users\duncan\Documents\Arduino\ledpuzzlewith_buzzer\ledpuzzlewith_buzzer.ino: In function 'void toggle3(int, int, int)':

ledpuzzlewith_buzzer:128:8: error: expected '(' before '{' token

} if {

^

ledpuzzlewith_buzzer:133:3: error: 'else' without a previous 'if'

else

^~~~

Multiple libraries were found for "IRremote.h"

Used: C:\Users\duncan\Documents\Arduino\libraries\IRremote

Not used: C:\Users\duncan\Documents\Arduino\libraries\Arduino-IRremote-master

exit status 1

expected '(' before '{' token

 #include <IRremote.h>  //Infrared Remote Library
 #include "pitches.h"  // For Passive Buzzer (dunk)

int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);  //Setup the IR remote for initilisation
decode_results results;   // set a decode variable, called results


const int LED [] = {0, 7, 6, 5, 4, 3}; // Set LED's as Digital Pins 7 thru 3
const int buzzer = 11; //(dunk)


int LED_State[] = {0, 0, 0, 0, 0, 0, 0}; // Inlcude one more than the number of LED's
// you have, first val must be zero. Sets all
// LED's to low/off

int melody[] = {
  NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6};
int duration = 500;  // 500 miliseconds // tones for buzzer (dunk)

unsigned int val = 0; // A variable to store the IR reading

const int code1 = 12495;   //Set the values recieved
const int code2 = 6375;  //from the IR remote
const int code3 = 31365;  //for your chosen buttons
const int code4 = 4335;
const int code5 = 14535;
const int code6 = 41565;  //all leds off (dunk)


void setup() {

  Serial.begin(9600); // Turn on to allow you to check your IR remote values

  irrecv.enableIRIn(); // Start the IR receiver

  for (int i = 1; i < 6; i++) { // Set all LED's as OUTPUTS
    pinMode(LED[i], OUTPUT);
    pinMode(buzzer,OUTPUT);
  }
}

void loop() {

  if (irrecv.decode(&results)) {  //If the IR receiver gets a signal
    val = results.value;          //Save that signal as a value to 'val'
    Serial.println(val);          //To check the values from your remote


    switch (val) {
      case code1:                   //If the IR reciever picks up code1
        toggle2(1, 4);              //Toggle these LED's on/off
        break;
      case code2:                   //If the IR reciever picks up code2
        toggle2(2, 3);              //Toggle these LED's on/off
        break;
      case code3:                   //If the IR reciever picks up code3
        toggle3(3, 4, 5);              //Toggle these LED's on/off
        break;
      case code4:                   //If the IR reciever picks up code4
        toggle3(1, 4, 5);              //Toggle these LED's on/off
        break;
      case code5:                   //If the IR reciever picks up code5
        toggle3(1, 3, 4);              //Toggle these LED's on/off
        break ;
      case code6 :
        for (int i = 1; i < 6; i++) { // If Code 6 is received, all LED's are
          digitalWrite(LED[i], LOW);   // turned off
          LED_State[i] = 0;
        }
        break ;
    }
    irrecv.resume(); // Recieve the Next value
  }
}

//Create a function to toggle two LED's on/off
void toggle2(int x, int y) {    //Set x and y as two of the LED's (mix it up!)
  if (LED_State[x] == 0) {       //check to see if LED is currently on low/off
    digitalWrite(LED[x], HIGH);   // if its off, turn it on
    LED_State[x] = 1;
  } else {
    digitalWrite(LED[x], LOW);    // if its on, turn it off
    LED_State[x] = 0;
  }
  if (LED_State[y] == 0) {       //check to see if LED is currently on low/off
    digitalWrite(LED[y], HIGH);   // if its off, turn it on
    LED_State[y] = 1;
  } else {
    digitalWrite(LED[y], LOW);    // if its on, turn it off
    LED_State[y] = 0;
  }
}

//Create a function to toggle three LED's on/off
void toggle3(int x, int y, int z) {     //Set x and y as two of the LED's (mix it up!)
  if (LED_State[x] == 0) {              //check to see if LED is currently on low/off
    digitalWrite(LED[x], HIGH);         // if its off, turn it on
    LED_State[x] = 1;
  } else {
    digitalWrite(LED[x], LOW);    // if its on, turn it off
    LED_State[x] = 0;
  }
  if (LED_State[y] == 0) {       //check to see if LED is currently on low/off
    digitalWrite(LED[y], HIGH);   // if its off, turn it on
    LED_State[y] = 1;
  } else {
    digitalWrite(LED[y], LOW);    // if its on, turn it off
    LED_State[y] = 0;
  }
  if (LED_State[z] == 0) {       //check to see if LED is currently on low/off
    digitalWrite(LED[z], HIGH);   // if its off, turn it on
    LED_State[z] = 1;
  } else {
    digitalWrite(LED[z], LOW);    // if its on, turn it off
    LED_State[z] = 0;
  } if {
    (LED_State[] = ){0, 1,1,1,1,1,1}; //d
  tone(11, melody[thisNote], duration); //d
  delay(1000); //(dunk)
  }
  else
  {
    noTone(11);
  }
  
}

I don't understand why you are declaring your arrays with one extra element and then using 1-relative accesses. Anyhow, below is an example that corresponds to how you have it coded with 1-relative accesses. I would recommend changing that because it serves no purpose except to waste memory.

Here is one way to do it:

  bool allLedSet = true;
  for (int i = 1; i < 6; i++) {
    if (!LED_State[i]) {
       allLedSet = false;
       break;
    }
  }

  if (allLedSet) {
    // play melody
  } else {
    noTone(11);
  }

ToddL1962:
I don't understand why you are declaring your arrays with one extra element and then using 1-relative accesses. Anyhow, below is an example that corresponds to how you have it coded with 1-relative accesses. I would recommend changing that because it serves no purpose except to wate memory.

Here is one way to do it:

  bool allLedSet = true;

for (int i = 1; i < 6; i++) {
   if (!LED_State[i]) {
      allLedSet = false;
      break;
   }
 }

if (allLedSet) {
   // play melody
 } else {
   noTone(11);
 }

the reason i was doing it that way was because it was the only way i could see to do it ( im really new at this)
thanks for the help, it really is appreciated. ive tried to add it into the code but now im getting this instead...
Tone.cpp.o (symbol from plugin): In function timer0_pin_port': (.text+0x0): multiple definition of __vector_7'
libraries\IRremote\IRremote.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Multiple libraries were found for "IRremote.h"
Used: C:\Users\duncan\Documents\Arduino\libraries\IRremote
Not used: C:\Users\duncan\Documents\Arduino\libraries\Arduino-IRremote-master
exit status 1
Error compiling for board Arduino Uno.

There is a conflict because both libraries use the same timer. You can change the timer used in the library source(.cpp and .h files) or use the Newtone library that uses a different timer.

To switch timer for IRremote library, just edit the file IRremoteInt.h in Documents/Arduino/Libraries/IRremote

search for line:
#define IR_USE_TIMER2

comment this line and uncomment the line for another timer.

With regard to arrays, if you define your LED array like this:

const int LED [] = {7, 6, 5, 4, 3}; // Set LED's as Digital Pins 7 thru 3

You can set all of the outputs like this:

  for (int i = 0; i < 5; i++) { // Set all LED's as OUTPUTS
    pinMode(LED[i], OUTPUT);
  }

Now just follow suit with the other arrays.

ToddL1962:
There is a conflict because both libraries uses the same timer. You can change the timer used in the library source(.cpp and .h files) or use the Newtone library that uses a different timer.

To switch timer for IRremote library, just edit the file IRremoteInt.h in Documents/Arduino/Libraries/IRremote

search for line:
#define IR_USE_TIMER2

comment this line and uncomment the line for another timer.

With regard to arrays, if you define your LED array like this:

const int LED [] = {7, 6, 5, 4, 3}; // Set LED's as Digital Pins 7 thru 3

You can set all of the outputs like this:

  for (int i = 0; i < 5; i++) { // Set all LED's as OUTPUTS

pinMode(LED[i], OUTPUT);
 }




Now just follow suit with the other arrays.

thanks ToddL1962, ill give that a go. im about ready to give up on the buzzer though as my soldering iron has just packed up lol
ToddL1962
ToddL1962

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.