Problem interfacing with the Grove - Electromagnet.

Hi all, I am trying to make a lock that uses an electromagnet as the locking mechanism. I was able to successfully interface with my 4x4 membrane keypad; however, I am completely stumped on how to interface with my electromagnet. I have tried to look up code that functional with my electromagnet; however, all I could find was from seeed Grove - Electromagnet | Seeed Studio Wiki which was not helpful.:frowning:

If someone could link me to some code or help me figure how to interface with my magnet I greatly appreciate it.

Here is my code:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte ELECTROMAGNET[4] = {13,12,11,10};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
   Serial.begin(9600);
   pinMode(ELECTROMAGNET, OUTPUT);
}
  
void loop(){
  char key = keypad.getKey();
  int pass = 1234;
  
  if (key){
    Serial.println(key);
  }
  if (key == pass)
  {
    digitalWrite(ELECTROMAGNET, LOW);
    delay(2000);
  }
  else 
  {
    digitalWrite(ELECTROMAGNET, HIGH);
  }
  }
}

Here are my errors:

Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\Cyrus\Documents\Arduino\Electromagnet_Lock\Electromagnet_Lock.ino: In function 'void setup()':

C:\Users\Cyrus\Documents\Arduino\Electromagnet_Lock\Electromagnet_Lock.ino:19:33: warning: invalid conversion from 'byte* {aka unsigned char*}' to 'uint8_t {aka unsigned char}' [-fpermissive]

    pinMode(ELECTROMAGNET, OUTPUT);

                                 ^

In file included from sketch\Electromagnet_Lock.ino.cpp:1:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:133:6: note: initializing argument 1 of 'void pinMode(uint8_t, uint8_t)'

 void pinMode(uint8_t, uint8_t);

      ^

C:\Users\Cyrus\Documents\Arduino\Electromagnet_Lock\Electromagnet_Lock.ino: In function 'void loop()':

C:\Users\Cyrus\Documents\Arduino\Electromagnet_Lock\Electromagnet_Lock.ino:31:36: warning: invalid conversion from 'byte* {aka unsigned char*}' to 'uint8_t {aka unsigned char}' [-fpermissive]

     digitalWrite(ELECTROMAGNET, LOW);

                                    ^

In file included from sketch\Electromagnet_Lock.ino.cpp:1:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:134:6: note: initializing argument 1 of 'void digitalWrite(uint8_t, uint8_t)'

 void digitalWrite(uint8_t, uint8_t);

      ^

C:\Users\Cyrus\Documents\Arduino\Electromagnet_Lock\Electromagnet_Lock.ino:36:37: warning: invalid conversion from 'byte* {aka unsigned char*}' to 'uint8_t {aka unsigned char}' [-fpermissive]

     digitalWrite(ELECTROMAGNET, HIGH);

                                     ^

In file included from sketch\Electromagnet_Lock.ino.cpp:1:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:134:6: note: initializing argument 1 of 'void digitalWrite(uint8_t, uint8_t)'

 void digitalWrite(uint8_t, uint8_t);

      ^

C:\Users\Cyrus\Documents\Arduino\Electromagnet_Lock\Electromagnet_Lock.ino: At global scope:

Electromagnet_Lock:39: error: expected declaration before '}' token

 }

 ^

exit status 1
expected declaration before '}' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

You wrote:

   pinMode(ELECTROMAGNET, OUTPUT);

but ELECTROMAGNET is an array. Perhaps you meant

   pinMode(ELECTROMAGNET[0], OUTPUT);
   pinMode(ELECTROMAGNET[1], OUTPUT);
   pinMode(ELECTROMAGNET[2], OUTPUT);
   pinMode(ELECTROMAGNET[3], OUTPUT);

or some equivalent using a "for" loop?

@vaj4088 I made the changes that you suggested, but I still am having issues compiling.

Here is my code:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte ELECTROMAGNET[4] = {13,12,11,10};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
   Serial.begin(9600);
   for(int count = 0; count < 4;count++){
  pinMode(ELECTROMAGNET[count], OUTPUT);
   }
}
  
void loop(){
  char key = keypad.getKey();
  int pass = 1234;
  
  if (key){
    Serial.println(key);
  }
  if (key == pass)
  {
    digitalWrite(ELECTROMAGNET, LOW);
    delay(2000);
  }
  else 
  {
    digitalWrite(ELECTROMAGNET, HIGH);
  }
  }

Here is my errors:

C:\Users\Cyrus\Documents\Arduino\Electromagnet_Lock\Electromagnet_Lock.ino: In function 'void loop()':

C:\Users\Cyrus\Documents\Arduino\Electromagnet_Lock\Electromagnet_Lock.ino:33:36: warning: invalid conversion from 'byte* {aka unsigned char*}' to 'uint8_t {aka unsigned char}' [-fpermissive]

     digitalWrite(ELECTROMAGNET, LOW);

                                    ^

In file included from sketch\Electromagnet_Lock.ino.cpp:1:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:134:6: note: initializing argument 1 of 'void digitalWrite(uint8_t, uint8_t)'

 void digitalWrite(uint8_t, uint8_t);

      ^

C:\Users\Cyrus\Documents\Arduino\Electromagnet_Lock\Electromagnet_Lock.ino:38:37: warning: invalid conversion from 'byte* {aka unsigned char*}' to 'uint8_t {aka unsigned char}' [-fpermissive]

     digitalWrite(ELECTROMAGNET, HIGH);

                                     ^

In file included from sketch\Electromagnet_Lock.ino.cpp:1:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:134:6: note: initializing argument 1 of 'void digitalWrite(uint8_t, uint8_t)'

 void digitalWrite(uint8_t, uint8_t);

      ^

Sketch uses 3788 bytes (1%) of program storage space. Maximum is 253952 bytes.
Global variables use 337 bytes (4%) of dynamic memory, leaving 7855 bytes for local variables. Maximum is 8192 bytes.

same problem here:

  if (key == pass)
  {
    digitalWrite(ELECTROMAGNET, LOW);

That link clearly shows the pinout of the electromagnet module is [ground, Vcc, NC, signal]
It needs 400mA 5V Vcc supply, just within the reach of USB.

You only need one arduino pin to control the electromagnet.

You fixed ELECTROMAGNET in the setup() function, now you need to do something similar in the loop() function, but FIRST perhaps you should think about what you are trying to accomplish and why ELECTROMAGNET is declared the way that it is.

@MarkT Thanks so much. I think I have fixed my mistakes. can you show me where to plug in my 4 pins? I am using a Arduino Mega 2560 (I am new to microelectronics).

Here is my code:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
int ELECTROMAGNET = 14;
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
   Serial.begin(9600);
  pinMode(ELECTROMAGNET,OUTPUT);
}
  
void loop(){
  char key = keypad.getKey();
  int pass = 1234;
  
  if (key){
    Serial.println(key);
  }
  if (key == pass)
  {
    digitalWrite(ELECTROMAGNET, LOW);
    delay(2000);
  }
  else 
  {
    digitalWrite(ELECTROMAGNET, HIGH);
  }
  }

I am having problems turning off the magnet with the pass code

if (keyIn[0] == pass[0] && keyIn[1] == pass[1] && keyIn[2] == pass[2] && keyIn[3] == pass[3])
  {
    digitalWrite(ELECTROMAGNET, LOW);
    delay(2000);
  }

Here is my updated code:

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
int ELECTROMAGNET = 10;
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
   Serial.begin(9600);
  pinMode(ELECTROMAGNET,OUTPUT);
}
  
void loop(){
  char key = keypad.getKey();
  int pass[4] = {1,2,3,4};
  int keyIn[4];
  for (int count = 0; count < 4; count++)
  {
     keyIn[count] = key;
  }
  for (int val : keyIn)
  Serial.print(val);
  if (key){
    Serial.println(key);
    
  }
 if (keyIn[0] == pass[0] && keyIn[1] == pass[1] && keyIn[2] == pass[2] && keyIn[3] == pass[3])
  {
    digitalWrite(ELECTROMAGNET, LOW);
    delay(2000);
  }
  
  else 
  {
    digitalWrite(ELECTROMAGNET, HIGH);
  }
  }