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.
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.