Error codes i cant figure out

Hi all,

Very new to Arduino and while i have understood it a little im stuck on these errors as when i change what i think it might be i get more errors. :frowning: Any help would be greatly appreciated. I have been trying to merge three different codes, I have a 4 wasy relay module where two relays will be controlled by push buttons, one will be controlled by a light sensor module and the 4th to be controlled by RFID.

My code is here
[Edit - code tags added by Moderator. Please use them next time, the </> button on the menu.]

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          12          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
const int pushButton[] = {2, 3}; // define push button inputs
const int relayPin[] = {11, 10}; // output pins where 4 relays will be connected
const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's  pin
const int RELAY_PIN        = 1;  // Arduino pin connected to Relay's pin
const int ANALOG_THRESHOLD = 500;
String relayNames[] = {"CH1", "CH2", "CH3", "CH4"}; // Just put name for 4 relays
String read_rfid;                   // Add how many you need and don't forget to include the UID.
String ok_rfid_1 = "e199312d";      // This is for my main RFID Card. aka. The one I will be using to turn on my PC. Can also be used to shut it down if you want to.
String ok_rfid_2 = "fbecb673";      // This is for the RFID Keyfob. aka. Shutdown Keyfob. Not advisable tho. Just shutdown your PC normally.
int pushed[] = {0, 0, 0, 0}; // status of each buttons
int relayStatus[] = {HIGH, HIGH, HIGH, HIGH}; // initial status of relay
int lock = 8;                       // For the Card.
int lock2 = 8;                      // For the Keyfob.
int analogValue;

void setup() {
 pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
 Serial.begin(9600);// initialize serial monitor
 for (int i = 0; i < 4; i++)
 {
   pinMode(pushButton[i], INPUT_PULLUP);
   pinMode(relayPin[i], OUTPUT);
   digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
   Serial.begin(9600);         // Initialize serial communications with the PC
   while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
   SPI.begin();                // Init SPI bus
   mfrc522.PCD_Init();         // Init MFRC522 card

   //Choose which lock below:
   pinMode(lock, OUTPUT);
   pinMode(lock2, OUTPUT);
 }
 
 void dump_byte_array(byte * buffer, byte bufferSize) {
   read_rfid = "";
   for (byte i = 0; i < bufferSize; i++) {
     read_rfid = read_rfid + String(buffer[i], HEX)
     
     void open_lock() {
       digitalWrite(lock, HIGH);
       delay(500);
       digitalWrite(lock, LOW);
     }

     void close_lock2() {
       digitalWrite(lock2, HIGH);
       delay(5000);
       digitalWrite(lock2, LOW);
     }
     void loop() {
       analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin

       if (analogValue < ANALOG_THRESHOLD)
         digitalWrite(RELAY_PIN, HIGH); // turn on Relay
       else
         digitalWrite(RELAY_PIN, LOW);  // turn off Relay
     }
     // Look for new cards
     if ( ! mfrc522.PICC_IsNewCardPresent())
       return;

     // Select one of the cards
     if ( ! mfrc522.PICC_ReadCardSerial())
       return;

     dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
     Serial.println(read_rfid);
     if (read_rfid == ok_rfid_1) {
       //ok, open the door.
       open_lock();
     }

     Serial.println(read_rfid);
     if (read_rfid == ok_rfid_2) {
       //ok, open the door.
       close_lock2();
     }
     for (int i = 0; i < 4; i++)
     {
       int  val = digitalRead(pushButton[i]);
       if (val == HIGH && relayStatus[i] == LOW) {

         pushed[i] = 1 - pushed[i];
         delay(100);
       }// if

       relayStatus[i] = val;

       if (pushed[i] == HIGH) {
         Serial.print(relayNames[i]);
         Serial.println(" ON");
         digitalWrite(relayPin[i], LOW);

       } else {
         Serial.print(relayNames[i]);
         Serial.println(" OFF");
         digitalWrite(relayPin[i], HIGH);

       }// else

     }// for
     Serial.println("==");
     delay(100);
   }
 }

The errors im getting are

C:\Users\MrA\Desktop\chopper_wiring\chopper_wiring.ino: In function ‚Äėvoid setup()‚Äô:
chopper_wiring:43:56: error: a function-definition is not allowed here before ‚Äė{‚Äô token
void dump_byte_array(byte * buffer, byte bufferSize) {
^
exit status 1
a function-definition is not allowed here before ‚Äė{‚Äô token

Thanks

Look carefully for where your setup function ends.
Or rather, look for where the first for loop in setup ends.

Please remember to use code tags when posting code

Hi,

Thanks for the reply. I have been looking at it and i can't see what I'm missing.

Your setup() is missing a closing }

void setup() {
  pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
  Serial.begin(9600);// initialize serial monitor
  for (int i = 0; i < 4; i++)
  {
    pinMode(pushButton[i], INPUT_PULLUP);
    pinMode(relayPin[i], OUTPUT);
    digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
    Serial.begin(9600);         // Initialize serial communications with the PC
    while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card


    //Choose which lock below:
    pinMode(lock, OUTPUT);
    pinMode(lock2, OUTPUT);
  }
} <<<<<<<<<<<<  add this here

You have other functions that are missing their closing } also.
That makes it look like you function definitions within function definitions, which is a no-go.

Try this for cleaned up { }

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          12          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
const int pushButton[] = {2, 3}; // define push button inputs
const int relayPin[] = {11, 10}; // output pins where 4 relays will be connected
const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's  pin
const int RELAY_PIN        = 1;  // Arduino pin connected to Relay's pin
const int ANALOG_THRESHOLD = 500;
String relayNames[] = {"CH1", "CH2", "CH3", "CH4"}; // Just put name for 4 relays
String read_rfid;                   // Add how many you need and don't forget to include the UID.
String ok_rfid_1 = "e199312d";      // This is for my main RFID Card. aka. The one I will be using to turn on my PC. Can also be used to shut it down if you want to.
String ok_rfid_2 = "fbecb673";      // This is for the RFID Keyfob. aka. Shutdown Keyfob. Not advisable tho. Just shutdown your PC normally.
int pushed[] = {0, 0, 0, 0}; // status of each buttons
int relayStatus[] = {HIGH, HIGH, HIGH, HIGH}; // initial status of relay
int lock = 8;                       // For the Card.
int lock2 = 8;                      // For the Keyfob.
int analogValue;

void setup() {
  pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
  Serial.begin(9600);// initialize serial monitor
  for (int i = 0; i < 4; i++)
  {
    pinMode(pushButton[i], INPUT_PULLUP);
    pinMode(relayPin[i], OUTPUT);
    digitalWrite(relayPin[i], HIGH);// initial relay status to be OFF
    Serial.begin(9600);         // Initialize serial communications with the PC
    while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card

    //Choose which lock below:
    pinMode(lock, OUTPUT);
    pinMode(lock2, OUTPUT);
  }
}

void dump_byte_array(byte * buffer, byte bufferSize) {
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX)
  }
}

void open_lock() {
  digitalWrite(lock, HIGH);
  delay(500);
  digitalWrite(lock, LOW);
}

void close_lock2() {
  digitalWrite(lock2, HIGH);
  delay(5000);
  digitalWrite(lock2, LOW);
}

void loop() {
  analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin

  if (analogValue < ANALOG_THRESHOLD) {
    digitalWrite(RELAY_PIN, HIGH); // turn on Relay
  }
  else {
    digitalWrite(RELAY_PIN, LOW);  // turn off Relay
  }
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);

  Serial.println(read_rfid);
  if (read_rfid == ok_rfid_1) {
    //ok, open the door.
    open_lock();
  }

  Serial.println(read_rfid);
  if (read_rfid == ok_rfid_2) {
    //ok, open the door.
    close_lock2();
  }
  for (int i = 0; i < 4; i++)
  {
    int  val = digitalRead(pushButton[i]);
    if (val == HIGH && relayStatus[i] == LOW) {

      pushed[i] = 1 - pushed[i];
      delay(100);
    }// if

    relayStatus[i] = val;

    if (pushed[i] == HIGH) {
      Serial.print(relayNames[i]);
      Serial.println(" ON");
      digitalWrite(relayPin[i], LOW);
    }
    else {
      Serial.print(relayNames[i]);
      Serial.println(" OFF");
      digitalWrite(relayPin[i], HIGH);
    }// else
  }// for
  Serial.println("==");
  delay(100);
}

Hi,

Thank you so much for your help. I realized that it would be better for the card to close the relay when touched and then open it again when touched again. Currently it closes the relay for a certain amount of time and then automatrically opens. How would i make it so it closes when the card is used and then opened when the card it used again. Kind of like an on off thing.

Thanks